VentoyWebDeepin.sh 4.24 KB
Newer Older
longpanda's avatar
longpanda committed
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
#!/bin/sh

print_usage() {    
    echo 'Usage:  VentoyWebDeepin.sh [ OPTION ]'   
    echo '  OPTION: (optional)'
    echo '   -H x.x.x.x  http server IP address (default is 127.0.0.1)'
    echo '   -p PORT     http server PORT (default is 24680)'
    echo '   -h          print this help'
    echo ''
}

print_err() {
    echo ""
    echo "$*"
    echo ""
}


get_user() {
    name=$(logname)
    if [ -n "$name" -a "$name" != "root" ]; then
        echo $name; return
    fi
    
    name=${HOME#/home/}
    if [ -n "$name" -a "$name" != "root" ]; then
        echo $name; return
    fi
}

longpanda's avatar
longpanda committed
31
32
33
34
35
36
37
get_browser_cmd() {
    if uos-browser --version > /dev/null 2>&1; then
        echo "uos-browser"; return
    else
        echo "browser"
    fi    
}
longpanda's avatar
longpanda committed
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

uid=$(id -u)
if [ $uid -ne 0 ]; then
    exec sudo sh $0 $*
fi

OLDDIR=$(pwd)

if uname -m | egrep -q 'aarch64|arm64'; then
    TOOLDIR=aarch64
elif uname -m | egrep -q 'x86_64|amd64'; then
    TOOLDIR=x86_64
elif uname -m | egrep -q 'mips64'; then
    TOOLDIR=mips64el
else
    TOOLDIR=i386
fi

if [ ! -f ./tool/$TOOLDIR/V2DServer ]; then
    if [ -f ${0%VentoyWebDeepin.sh}/tool/$TOOLDIR/V2DServer ]; then
        cd ${0%VentoyWebDeepin.sh}
    fi
fi

PATH=./tool/$TOOLDIR:$PATH

if [ ! -f ./boot/boot.img ]; then
    if [ -d ./grub ]; then
        echo "Don't run VentoyWebDeepin.sh here, please download the released install package, and run the script in it."
    else
        echo "Please run under the correct directory!" 
    fi
    exit 1
fi

HOST="127.0.0.1"
PORT=24680

while [ -n "$1" ]; do
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        print_usage
        exit 0
    elif [ "$1" = "-H" ]; then
        shift
        if echo $1 | grep -q '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'; then
            HOST="$1"
        else
            print_err "Invalid host $1"
            exit 1
        fi        
    elif [ "$1" = "-p" ]; then
        shift
        if [ $1 -gt 0 -a $1 -le 65535 ]; then
            PORT="$1"
        else
            print_err "Invalid port $1"
            exit 1
        fi
    fi
    
    shift
done


if ps -ef | grep "V2DServer.*$HOST.*$PORT" | grep -q -v grep; then
    print_err "Another ventoy server is running now, please close it first."
    exit 1
fi

VUSER=$(get_user)

LOGFILE=log.txt
if [ -e $LOGFILE ]; then
longpanda's avatar
longpanda committed
111
112
113
114
115
    LogGroup=$(stat -c '%G' $LOGFILE)
    if [ "$LogGroup" = "root" ]; then
        rm -f $LOGFILE
        su $VUSER -c "touch $LOGFILE"
    fi
longpanda's avatar
longpanda committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
else
    su $VUSER -c "touch $LOGFILE"
fi

#delete the log.txt if it's more than 8MB
if [ -f $LOGFILE ]; then
    logsize=$(stat -c '%s' $LOGFILE)
    if [ $logsize -gt 8388608 ]; then
        rm -f $LOGFILE
    fi
fi


if [ -f ./tool/$TOOLDIR/V2DServer.xz ]; then
    xz -d ./tool/$TOOLDIR/V2DServer.xz
    chmod +x ./tool/$TOOLDIR/V2DServer
fi

rm -rf ./*_VTMPDIR
longpanda's avatar
longpanda committed
135
vtWebTmpDir=$(su $VUSER -c "mktemp -d -p ./ --suffix=_VTMPDIR")
longpanda's avatar
longpanda committed
136
137
138
139
140

V2DServer "$HOST" "$PORT" &
V2DPid=$!
sleep 1

longpanda's avatar
longpanda committed
141
142
browsercmd=$(get_browser_cmd)
su $VUSER -c "$browsercmd --window-size=550,400 --app=\"http://${HOST}:${PORT}/index.html?chrome-app\"  --user-data-dir=$vtWebTmpDir >> $LOGFILE 2>&1" &
longpanda's avatar
longpanda committed
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
WebPid=$!


vtoy_trap_exit() {

    [ -d /proc/$V2DPid ] && kill -2 $V2DPid
    [ -d /proc/$WebPid ] && kill -9 $WebPid

    while [ -n "1" ]; do
        curPid=$(ps -ef | grep -m1 "$vtWebTmpDir" | egrep -v '\sgrep\s' | awk '{print $2}')
        if [ -z "$curPid" ]; then
            break
        fi
        
        if [ -d /proc/$curPid ]; then
            kill -9 $curPid
        fi
    done

    [ -d $vtWebTmpDir ] && rm -rf $vtWebTmpDir

    if [ -n "$OLDDIR" ]; then 
        CURDIR=$(pwd)
        if [ "$CURDIR" != "$OLDDIR" ]; then
            cd "$OLDDIR"
        fi
    fi

    exit 1
}

trap vtoy_trap_exit HUP INT QUIT TSTP
sleep 1


vtVer=$(cat ventoy/version)
echo ""
echo "=================================================="
if [ "$LANG" = "zh_CN.UTF-8" ]; then
    echo "  Ventoy Server $vtVer 已经启动 ..."
else
    echo "  Ventoy Server $vtVer is running ..."
fi
echo "=================================================="
echo ""
echo "########### Press Ctrl + C to exit ###############"
echo ""

wait $WebPid

[ -d /proc/$V2DPid ] && kill -2 $V2DPid

[ -d $vtWebTmpDir ] && rm -rf $vtWebTmpDir

if [ -n "$OLDDIR" ]; then 
    CURDIR=$(pwd)
    if [ "$CURDIR" != "$OLDDIR" ]; then
        cd "$OLDDIR"
    fi
fi