]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/VentoyWeb.sh
update languages.ini
[Ventoy.git] / INSTALL / VentoyWeb.sh
1 #!/bin/sh
2
3 print_usage() {
4 echo 'Usage: VentoyWeb.sh [ OPTION ]'
5 echo ' OPTION: (optional)'
6 echo ' -H x.x.x.x http server IP address (default is 127.0.0.1)'
7 echo ' -p PORT http server PORT (default is 24680)'
8 echo " -n don't start web browser"
9 echo ' -h print this help'
10 echo ''
11 }
12
13 print_err() {
14 echo ""
15 echo "$*"
16 echo ""
17 }
18
19 check_option() {
20 app="$1"
21 $app --help 2>&1 | grep -q "$2"
22 }
23
24 get_user() {
25 name=$(logname)
26 if [ -n "$name" -a "$name" != "root" ]; then
27 echo $name; return
28 fi
29
30 name=${HOME#/home/}
31 if [ -n "$name" -a "$name" != "root" ]; then
32 echo $name; return
33 fi
34 }
35
36 chromium_proc() {
37 app="$1"
38
39 url="http://${HOST}:${PORT}/index.html"
40
41 if check_option "$app" '[-][-]app='; then
42 su $VUSER -c "$app --app=$url >> $LOGFILE 2>&1"
43 elif check_option "$app" '[-][-]new[-]window='; then
44 su $VUSER -c "$app --new-window $url >> $LOGFILE 2>&1"
45 else
46 su $VUSER -c "$app $url >> $LOGFILE 2>&1"
47 fi
48 }
49
50 uid=$(id -u)
51 if [ $uid -ne 0 ]; then
52 print_err "Please use sudo or run the script as root."
53 exit 1
54 fi
55
56 OLDDIR=$(pwd)
57
58 if uname -a | egrep -q 'aarch64|arm64'; then
59 TOOLDIR=aarch64
60 elif uname -a | egrep -q 'x86_64|amd64'; then
61 TOOLDIR=x86_64
62 else
63 TOOLDIR=i386
64 fi
65
66 if [ ! -f ./tool/$TOOLDIR/V2DServer ]; then
67 if [ -f ${0%VentoyWeb.sh}/tool/$TOOLDIR/V2DServer ]; then
68 cd ${0%VentoyWeb.sh}
69 fi
70 fi
71
72 PATH=./tool/$TOOLDIR:$PATH
73
74 if [ ! -f ./boot/boot.img ]; then
75 if [ -d ./grub ]; then
76 echo "Don't run VentoyWeb.sh here, please download the released install package, and run the script in it."
77 else
78 echo "Please run under the correct directory!"
79 fi
80 exit 1
81 fi
82
83 HOST="127.0.0.1"
84 PORT=24680
85
86 while [ -n "$1" ]; do
87 if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
88 print_usage
89 exit 0
90 elif [ "$1" = "-n" ]; then
91 NOWEB=1
92 elif [ "$1" = "-H" ]; then
93 shift
94 if echo $1 | grep -q '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'; then
95 HOST="$1"
96 else
97 print_err "Invalid host $1"
98 exit 1
99 fi
100 elif [ "$1" = "-p" ]; then
101 shift
102 if [ $1 -gt 0 -a $1 -le 65535 ]; then
103 PORT="$1"
104 else
105 print_err "Invalid port $1"
106 exit 1
107 fi
108 fi
109
110 shift
111 done
112
113
114 if ps -ef | grep "V2DServer.*$HOST.*$PORT" | grep -q -v grep; then
115 print_err "Another ventoy server is running now, please close it first."
116 exit 1
117 fi
118
119 VUSER=$(get_user)
120 LOGFILE=log.txt
121 #delete the log.txt if it's more than 8MB
122 if [ -f $LOGFILE ]; then
123 logsize=$(stat -c '%s' $LOGFILE)
124 if [ $logsize -gt 8388608 ]; then
125 rm -f $LOGFILE
126 su $VUSER -c "touch $LOGFILE"
127 fi
128 else
129 su $VUSER -c "touch $LOGFILE"
130 fi
131
132
133
134 if [ -f ./tool/$TOOLDIR/V2DServer.xz ]; then
135 xz -d ./tool/$TOOLDIR/V2DServer.xz
136 chmod +x ./tool/$TOOLDIR/V2DServer
137 fi
138
139 V2DServer "$HOST" "$PORT" &
140
141 vtVer=$(cat ventoy/version)
142 echo ""
143 echo "=================================================================="
144 echo " Ventoy Server $vtVer is running at http://${HOST}:${PORT} ..."
145 echo "=================================================================="
146 echo ""
147 echo "################ Press Ctrl + C to exit ######################"
148 echo ""
149
150 if [ "$NOWEB" = "1" ]; then
151 echo "Please open your web browser and visit http://${HOST}:${PORT}"
152 else
153 if which -a google-chrome-stable >> $LOGFILE 2>&1; then
154 chromium_proc google-chrome-stable
155 elif which -a google-chrome >> $LOGFILE 2>&1; then
156 chromium_proc google-chrome
157 elif which -a chrome >> $LOGFILE 2>&1; then
158 chromium_proc chrome
159 elif which -a browser >> $LOGFILE 2>&1; then
160 chromium_proc browser
161 elif which -a firefox >> $LOGFILE 2>&1; then
162 su $VUSER -c "firefox --no-remote \"http://${HOST}:${PORT}/index.html\""
163 else
164 echo "Please open your web browser and visit http://${HOST}:${PORT}"
165 fi
166 fi
167
168 if ps -ef | grep "V2DServer.*$HOST.*$PORT" | grep -q -v grep; then
169 echo ""
170 else
171 print_err "Ventoy Server Error! Please check log.txt."
172 fi
173
174 wait $!
175
176
177 if [ -n "$OLDDIR" ]; then
178 CURDIR=$(pwd)
179 if [ "$CURDIR" != "$OLDDIR" ]; then
180 cd "$OLDDIR"
181 fi
182 fi