-
Notifications
You must be signed in to change notification settings - Fork 3
/
menubox.sh
266 lines (238 loc) · 8.53 KB
/
menubox.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
ver="1.20"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
pyconfigfile="./settings.py"
filename_conf="./settings.conf"
filename_temp="./settings.conf.temp"
#------------------------------------------------------------------------------
function do_anykey ()
{
echo ""
echo "######################################"
echo "# Review Output #"
echo "######################################"
read -p " Press Enter to Return to Main Menu"
}
#------------------------------------------------------------------------------
function init_status ()
{
if [ -z "$( pgrep -f webserver.py )" ]; then
WEB_1="START"
WEB_2="webserver.py in background"
else
myip=$( ifconfig -a | grep 'inet ' | grep -v 127.0.0 | head -n 1 | tr -s " " | cut -d " " -f 3 )
myport=$( grep "web_server_port" settings.py | cut -d "=" -f 2 | cut -d "#" -f 1 | awk '{$1=$1};1' )
webserver_pid=$( pgrep -f webserver.py )
WEB_1="STOP"
WEB_2="webserver.py - PID is $webserver_pid http://$myip:$myport"
fi
}
#------------------------------------------------------------------------------
function do_webserver ()
{
if [ -z "$( pgrep -f $DIR/webserver.py )" ]; then
./webserver.sh start
if [ -z "$( pgrep -f $DIR/webserver.py )" ]; then
whiptail --msgbox "Failed to Start webserver.py Please Investigate Problem." 20 70
else
myip=$( ifconfig -a | grep 'inet ' | grep -v 127.0.0 | head -n 1 | tr -s " " | cut -d " " -f 3 )
myport=$( grep "web_server_port" settings.py | cut -d "=" -f 2 | cut -d "#" -f 1 | awk '{$1=$1};1' )
whiptail --msgbox --title "Webserver Access" "Access web server from another network computer web browser using url http://$myip:$myport" 15 60
fi
else
$DIR/webserver.sh stop
if [ ! -z "$( pgrep -f $DIR/webserver.py )" ]; then
whiptail --msgbox "Failed to Stop webserver.py Please Investigate Problem." 20 70
fi
fi
do_main_menu
}
#------------------------------------------------------------------------------
function do_webserver_config ()
{
if [ -e $DIR/settings.py ] ; then
/bin/nano $DIR/settings.py
else
whiptail --msgbox "ERROR - $DIR/settings.py File Not Found. Please Investigate." 20 65 1
fi
}
#--------------------------------------------------------------------
function do_edit_save ()
{
if (whiptail --title "Save $var=$newvalue" --yesno "$comment\n $var=$newvalue was $value" 8 65 --yes-button "Save" --no-button "Cancel" ) then
value=$newvalue
rm $filename_conf # Initialize new conf file
while read configfile ; do
if echo "${configfile}" | grep --quiet "${var}" ; then
echo "$var=$value #$comment" >> $filename_conf
else
echo "$configfile" >> $filename_conf
fi
done < $pyconfigfile
cp $filename_conf $pyconfigfile
fi
do_settings_menu
}
#--------------------------------------------------------------------
function do_edit_variable ()
{
choice=$(cat $filename_temp | grep $SELECTION)
var=$(echo $choice | cut -d= -f1)
value=$(echo $choice | cut -d= -f2)
comment=$( cat $filename_conf | grep $var | cut -d# -f2 )
echo "${value}" | grep --quiet "True"
# Exit status 0 means anotherstring was found
# Exit status 1 means anotherstring was not found
if [ $? = 0 ] ; then
newvalue=" False"
do_edit_save
else
echo "${value}" | grep --quiet "False"
if [ $? = 0 ] ; then
newvalue=" True"
do_edit_save
elif [ $? = 1 ] ; then
newvalue=$(whiptail --title "Edit $var (Enter Saves or Tab)" \
--inputbox "$comment\n $var=$value" 10 65 "$value" \
--ok-button "Save" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ ! "$newvalue" = "" ] ; then # Variable was changed
if [ $exitstatus -eq 1 ] ; then # Check if Save selected otherwise it was cancelled
do_edit_save
elif [ $exitstatus -eq 0 ] ; then
echo "do_edit_variable - Cancel was pressed"
if echo "${value}" | grep --quiet "${newvalue}" ; then
do_settings_menu
else
do_edit_save
fi
fi
fi
fi
fi
do_settings_menu
}
#--------------------------------------------------------------------
function do_edit_menu ()
{
clear
echo "Copy $filename_conf from $pyconfigfile Please Wait ...."
cp $pyconfigfile $filename_conf
echo "Initialize $filename_temp Please Wait ...."
cat $filename_conf | grep = | cut -f1 -d# | tr -s [:space:] >$filename_temp
echo "Initializing Settings Menu Please Wait ...."
menu_options=()
while read -r number text; do
menu_options+=( ${number//\"} "${text//\"}" )
done < $filename_temp
SELECTION=$( whiptail --title "webserver Settings Menu" \
--menu "Arrow/Enter Selects or Tab" 0 0 0 "${menu_options[@]}" --ok-button "Edit" 3>&1 1>&2 2>&3 )
RET=$?
if [ $RET -eq 1 ] ; then
do_settings_menu
elif [ $RET -eq 0 ]; then
cp $pyconfigfile $filename_conf
cat $filename_conf | grep = | cut -f1 -d# | tr -s [:space:] >$filename_temp
do_edit_variable
fi
}
#------------------------------------------------------------------------------
function do_nano_main ()
{
cp $pyconfigfile $filename_conf
nano $filename_conf
if (whiptail --title "Save Nano Edits" --yesno "Save nano changes to $filename_conf\n or cancel all changes" 8 65 --yes-button "Save" --no-button "Cancel" ) then
cp $filename_conf $pyconfigfile
fi
}
#------------------------------------------------------------------------------
function do_settings_menu ()
{
SET_SEL=$( whiptail --title "Settings Menu" --menu "Arrow/Enter Selects or Tab Key" 0 0 0 --ok-button Select --cancel-button Back \
"a " "Menu Edit webserver settings.py" \
"b " "nano Edit settings.py" \
"c " "View settings.py" \
"d " "Back to Main Menu" 3>&1 1>&2 2>&3 )
RET=$?
if [ $RET -eq 1 ]; then
clear
rm -f $filename_temp
rm -f $filename_conf
do_main_menu
elif [ $RET -eq 0 ]; then
case "$SET_SEL" in
a\ *) do_edit_menu
do_settings_menu ;;
b\ *) do_nano_main
do_settings_menu ;;
c\ *) more -d settings.py
do_anykey
do_settings_menu ;;
d\ *) clear
rm -f $filename_temp
rm -f $filename_conf
do_main_menu ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running selection $SELECTION" 20 60 1
fi
}
#------------------------------------------------------------------------------
function do_upgrade ()
{
if (whiptail --title "GitHub Upgrade webserver" --yesno "Upgrade webserver files from GitHub" 8 65 --yes-button "upgrade" --no-button "Cancel" ) then
curl -L https://raw.github.com/pageauc/webserver/master/webserver-install.sh | bash
do_anykey
fi
}
#------------------------------------------------------------------------------
function do_about ()
{
whiptail --title "About" --msgbox " \
webserver.py
Manage webserver operation and settings
written by Claude Pageau
for more information
View Readme.md (use menubox.sh help option)
or
visit web repo at https://github.com/pageauc/webserver
Raise a github issue for issues or questions
\
" 0 0 0
}
#------------------------------------------------------------------------------
function do_main_menu ()
{
init_status
SELECTION=$(whiptail --title "Webserver Main Menu" --menu "Arrow/Enter Selects or Tab Key" 0 0 0 --cancel-button Quit --ok-button Select \
"a $WEB_1" "$WEB_2" \
"b SETTINGS" "Edit/View webserver Settings" \
"c HELP" "View Readme.md" \
"d UPGRADE" "Files From GitHub.com" \
"e ABOUT" "This Program" \
"q QUIT" "Exit menubox.sh" 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
exit 0
elif [ $RET -eq 0 ]; then
case "$SELECTION" in
a\ *) do_webserver ;;
b\ *) do_settings_menu ;;
c\ *) more -d Readme.md
do_anykey ;;
d\ *) do_upgrade ;;
e\ *) do_about ;;
q\ *) clear
exit 0 ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running selection $SELECTION" 20 60 1
fi
}
#------------------------------------------------------------------------------
# Main Script
#------------------------------------------------------------------------------
if [ $# -eq 0 ] ; then
while true; do
do_main_menu
done
fi