-
Notifications
You must be signed in to change notification settings - Fork 2
/
firestarter.sh
executable file
·193 lines (165 loc) · 5.14 KB
/
firestarter.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
#note: the 'start' of the script is at the bottom. main() is ran which checks for lock files and if there are none, Firefox is executed.
trap ctrl_c INT
weAreRunning=false
interactive=false
FirefoxPID=0
PROFILE_PATH="ffprofile"
firefoxexe=$(which firefox)
LOCKFILE=".lock"
KILLFILE=".kill"
function ctrl_c() {
if $weAreRunning ;
then
echo -n "Ctrl+C Caught. "
cleanup
else
echo "Ctrl+C Caught. Terminating script (leaving lock files in tact)"
exit
fi
}
function request_lock_removal() {
if ! $interactive ; then
echo "Cannot get user input. exiting.."
exit
fi
read -r -p "Lock exists. Do you want to issue a remote kill request? (WARNING Remote open data will be lost!)[y/N]" response
case "$response" in
[yY][eE][sS]|[yY])
Time=`date`
echo "Issued by $USER@$HOSTNAME at $Time" > ./.kill
echo -n "Kill command issued. Please wait a short while to give the sync application and remote machine a chance to terminate the session (Press Ctrl+C at any time to abort and clean up)."
waiting=true
while $waiting; do
if check_for_lock; then
echo -n "."
else
waiting=false
echo "!"
main
fi
sleep 2; done
exit
;;
*)
echo "Please close the remote session and make sure there's no more lock file."
exit
;;
esac
}
function create_profile() {
if [ -e $PROFILE_PATH ] ; then
echo "Profile path string is empty!"
exit
fi
mkdir $PROFILE_PATH
read -r -p "New profile created. do you want to copy the template user.js into the new profile for some default settings? (enable usercontexts, dark-compact theme, etc, check template-user.js for more details) [y/N]" response
case "$response" in
[yY][eE][sS]|[yY])
cp template-user.js ./$PROFILE_PATH/user.js
;;
*)
main
;;
esac
}
function check_for_lock() {
if [ -f $LOCKFILE ] ; then
return 0
else
return 1
fi
}
function remove_lock() {
if [ "$FirefoxPID" -eq "0" ] ; then
return 1 #Firefox hasn't been succesfully started
fi
rm $LOCKFILE
}
function create_lock() {
if [ "$FirefoxPID" -eq "0" ] ; then
return 1 #Firefox hasn't been succesfully started
fi
touch $LOCKFILE
}
function update_lock() {
if [ "$FirefoxPID" -eq "0" ] ; then
return 1 #Firefox hasn't been succesfully started
fi
echo $(whoami) > $LOCKFILE
echo " on machine " >> $LOCKFILE
echo $(hostname) >> $LOCKFILE
}
# Really this is the core of the script. The rest is just making sure this works well across machines and such :v.
function run_firefox() {
$firefoxexe --no-remote --profile ./$PROFILE_PATH > console.log 2>&1 &
FirefoxPID=$!
}
function cleanup() {
if [ "$FirefoxPID" -eq "0" ] ; then
echo "Firefox process ID is 0, not performing cleanup"
fi
echo "Cleaning up Firefox PID $FirefoxPID"
if ps -p $FirefoxPID > /dev/null
then
echo "Firefox PID $FirefoxPID still running, killing it..."
kill $FirefoxPID || echo "Unable to kill firefox"
fi
echo deleting lockfiles..
rm -f $LOCKFILE
rm -f $KILLFILE
exit
}
function monitor_firefox() {
while RunCheck; do
if [ -f $KILLFILE ]; then
MSG=`cat $KILLFILE`
echo -n "Kill request found with message $MSG. "
RunCheck=false
cleanup
fi
if ! ps -p $FirefoxPID > /dev/null
then
echo "Firefox($FirefoxPID) is not running. Did it crash? "
cleanup
fi
if [ ! check_for_lock ] ; then
echo -n "Lock file removed. Killing firefox $FirefoxPID.. "
cleanup
fi
update_lock
sleep 2;
done
}
function RunCheck() {
#This should return true if the process exists
printf "\rCurrent process ID: $FirefoxPID (Press ctrl+C to kill or close the app)"
if [ "$FirefoxPID" -eq "0" ] ; then
echo "firefox isnt started ($FirefoxPID)"
return 1 #no firefox has been started yet
else
kill -0 $FirefoxPID
fi
}
function main() {
if [ ! -d "$PROFILE_PATH" ] ; then
create_profile
fi
if check_for_lock ; then
request_lock_removal
fi
weAreRunning=true
run_firefox
create_lock
echo "Started Firefox on $FirefoxPID"
monitor_firefox
cleanup #in case shit happens, clean up
}
if [ -t 1 ] ; then
echo "Interactive terminal detected.."
interactive=true
else
echo "WARNING: This shell is not interactive! Will exit if requiring user input.."
fi
main # main part of the script starts here, after the function definitions.
echo "Something went wrong.. This part of the script isn't meant to be run. Did spacetime collapse?"