Skip to content

Commit 489c69a

Browse files
committed
lang: Cleanup language install scripts
1 parent f428bc4 commit 489c69a

4 files changed

Lines changed: 155 additions & 83 deletions

File tree

data/lang_install.sh

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,69 @@
11
#!/bin/sh
22

33
DIR_PATCH=/etc/crontabs/patches
4+
DIR_BACKUP=$DIR_PATCH/lang_backup
45

5-
if [ `ls /tmp/base.*.lmo |wc -l` -eq 0 ]; then
6+
if [ `find /tmp -maxdepth 1 -name 'base.*.lmo' | wc -l` -eq 0 ]; then
67
return 1
78
fi
89

9-
if [ "$(mount| grep '/usr/lib/lua/luci')" != "" ]; then
10-
if [ -e "/tmp/lang_uninstall.sh" ]; then
11-
sh /tmp/lang_uninstall.sh
12-
fi
13-
fi
14-
15-
# delete old patch
16-
rm -f /etc/rc.lang
17-
rm -f /etc/lang_patch.sh
18-
19-
# global firmware may contain a file "base.en.lmo"
20-
[ -f /usr/lib/lua/luci/i18n/base.en.lmo ] && rm -f /tmp/base.en.lmo
21-
2210
if [ ! -d $DIR_PATCH ]; then
23-
mkdir $DIR_PATCH
11+
mkdir -p $DIR_PATCH
2412
chown root $DIR_PATCH
2513
chmod 0755 $DIR_PATCH
2614
fi
2715

16+
CLEAN_INSTALL=0
17+
if [ ! -d $DIR_BACKUP ]; then
18+
CLEAN_INSTALL=1
19+
mkdir -p $DIR_BACKUP
20+
MAIN_LANG=`uci get luci.main.lang`
21+
echo "$MAIN_LANG" > $DIR_BACKUP/main_lang
22+
fi
23+
24+
if [ $CLEAN_INSTALL = 1 ]; then
25+
NEED_RESTORE_MNT=0
26+
if mount | grep -q ' on /usr/lib/lua/luci' ; then
27+
umount -l /usr/lib/lua/luci
28+
NEED_RESTORE_MNT=1
29+
fi
30+
if [ -f /usr/lib/lua/luci/i18n/base.en.lmo ]; then
31+
# INT firmware may contain a file "base.en.lmo"
32+
echo "1" > $DIR_BACKUP/skip_base_en
33+
fi
34+
cp -f /usr/lib/lua/luci/view/web/inc/sysinfo.htm $DIR_BACKUP/
35+
cp -f /usr/lib/lua/luci/i18n/base.*.lmo $DIR_BACKUP/
36+
[ $NEED_RESTORE_MNT = 1 ] && mount --bind /tmp/_usr_lib_lua_luci /usr/lib/lua/luci
37+
fi
38+
if [ $CLEAN_INSTALL = 1 ]; then
39+
NEED_RESTORE_MNT=0
40+
if mount | grep -q ' on /usr/share/xiaoqiang' ; then
41+
umount -l /usr/share/xiaoqiang
42+
NEED_RESTORE_MNT=1
43+
fi
44+
if grep -q "option CHANNEL 'stable'" /usr/share/xiaoqiang/xiaoqiang_version ; then
45+
echo '1' > $DIR_BACKUP/fw_stable
46+
fi
47+
[ $NEED_RESTORE_MNT = 1 ] && mount --bind /tmp/_usr_share_xiaoqiang /usr/share/xiaoqiang
48+
fi
49+
50+
if [ -f $DIR_BACKUP/skip_base_en ]; then
51+
# INT firmware may contain a file "base.en.lmo"
52+
rm -f /tmp/base.en.lmo
53+
fi
2854
mv -f /tmp/base.*.lmo $DIR_PATCH/
2955
mv -f /tmp/lang_patch.sh $DIR_PATCH/
3056
chmod +x $DIR_PATCH/lang_patch.sh
31-
if [ -e "/tmp/lang_patch1.sh" ]; then
32-
mv -f /tmp/lang_patch1.sh $DIR_PATCH/
33-
chmod +x $DIR_PATCH/lang_patch1.sh
34-
fi
3557

36-
INSTALL_METHOD=1
37-
if [ -e "/usr/lib/os-release" ]; then
38-
INSTALL_METHOD=2
58+
INSTALL_METHOD=2
59+
if [ ! -e "/usr/lib/os-release" ]; then
60+
# older routers
61+
INSTALL_METHOD=0
3962
fi
4063

4164
FILE_PATCHED=0
4265

43-
if [ $INSTALL_METHOD == 1 ]; then
66+
if [ $INSTALL_METHOD = 0 ]; then
4467
FILE_PATCHED=1
4568
FILE_FOR_EDIT=/etc/init.d/boot
4669
NEW_CMD="\[ -f \/etc\/crontabs\/patches\/lang_patch.sh \] && sh \/etc\/crontabs\/patches\/lang_patch.sh"
@@ -60,19 +83,28 @@ if [ $INSTALL_METHOD == 1 ]; then
6083
fi
6184
fi
6285

63-
if [ $INSTALL_METHOD == 2 ]; then
86+
if [ $INSTALL_METHOD = 1 ]; then
6487
FILE_FOR_EDIT=/etc/crontabs/root
6588
grep -v "/lang_patch.sh" $FILE_FOR_EDIT > $FILE_FOR_EDIT.new
6689
echo "*/1 * * * * $DIR_PATCH/lang_patch.sh >/dev/null 2>&1" >> $FILE_FOR_EDIT.new
6790
mv $FILE_FOR_EDIT.new $FILE_FOR_EDIT
6891
/etc/init.d/cron restart
69-
FILE_PATCHED=4
92+
FILE_PATCHED=10
93+
fi
94+
95+
if [ $INSTALL_METHOD = 2 ]; then
96+
uci set firewall.auto_lang_patch=include
97+
uci set firewall.auto_lang_patch.type='script'
98+
uci set firewall.auto_lang_patch.path="$DIR_PATCH/lang_patch.sh"
99+
uci set firewall.auto_lang_patch.enabled='1'
100+
uci commit firewall
101+
FILE_PATCHED=20
70102
fi
71103

72104
# set main lang
73105
uci set luci.main.lang=en
74106
#uci commit luci
75107

76108
# run patch
77-
sh $DIR_PATCH/lang_patch.sh
109+
$DIR_PATCH/lang_patch.sh
78110

data/lang_patch.sh

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,59 @@
33
[ -e "/tmp/lang_patch.log" ] && return 0
44

55
DIR_PATCH=/etc/crontabs/patches
6+
DIR_BACKUP=$DIR_PATCH/lang_backup
7+
SYNC_OBJ=/tmp/_patch_sync
68

7-
if [ `ls $DIR_PATCH/base.*.lmo |wc -l` -eq 0 ]; then
9+
if [ `find $DIR_PATCH -maxdepth 1 -name 'base.*.lmo' | wc -l` -eq 0 ]; then
810
return 0
911
fi
1012

11-
#if [ -e "/etc/xiaoqiang_version" ]; then
12-
# return 0
13-
#fi
14-
15-
if [ -e "/usr/lib/lua/luci/i18n/base.en.lmo" ]; then
16-
return 0
13+
while true; do
14+
STATE=`mkdir $SYNC_OBJ 2>&1`
15+
if [[ "$STATE" != *"can't create"* ]]; then
16+
break
17+
fi
18+
sleep 1
19+
done
20+
21+
if ! mount | grep -q ' on /usr/lib/lua/luci' ; then
22+
mkdir -p /tmp/_usr_lib_lua_luci
23+
cp -rf /usr/lib/lua/luci/* /tmp/_usr_lib_lua_luci/
24+
mount --bind /tmp/_usr_lib_lua_luci /usr/lib/lua/luci
25+
fi
26+
if ! mount | grep -q ' on /usr/lib/lua/luci' ; then
27+
rm -rf $SYNC_OBJ
28+
return 1 # error
29+
fi
30+
if [ ! -f /tmp/_usr_lib_lua_luci/i18n.lua ]; then
31+
rm -rf $SYNC_OBJ
32+
return 1 # error
1733
fi
1834

19-
mkdir -p /tmp/_usr_lib_lua_luci
20-
cp -rf /usr/lib/lua/luci/* /tmp/_usr_lib_lua_luci/
21-
mount --bind /tmp/_usr_lib_lua_luci /usr/lib/lua/luci
22-
23-
cp $DIR_PATCH/base.*.lmo /usr/lib/lua/luci/i18n
24-
25-
# save original file
26-
cp -f /usr/share/xiaoqiang/xiaoqiang_version /etc/xiaoqiang_version
35+
if ! mount | grep -q ' on /usr/share/xiaoqiang' ; then
36+
mkdir -p /tmp/_usr_share_xiaoqiang
37+
cp -rf /usr/share/xiaoqiang/* /tmp/_usr_share_xiaoqiang/
38+
mount --bind /tmp/_usr_share_xiaoqiang /usr/share/xiaoqiang
39+
fi
40+
if ! mount | grep -q ' on /usr/share/xiaoqiang' ; then
41+
rm -rf $SYNC_OBJ
42+
return 1 # error
43+
fi
44+
if [ ! -f /tmp/_usr_share_xiaoqiang/xiaoqiang_version ]; then
45+
rm -rf $SYNC_OBJ
46+
return 1 # error
47+
fi
2748

28-
mkdir -p /tmp/_usr_share_xiaoqiang
29-
cp -rf /usr/share/xiaoqiang/* /tmp/_usr_share_xiaoqiang/
30-
mount --bind /tmp/_usr_share_xiaoqiang /usr/share/xiaoqiang
49+
cp -f $DIR_PATCH/base.*.lmo /usr/lib/lua/luci/i18n/
3150

3251
# unlock WEB lang menu
3352
sed -i 's/ and features\["system"\]\["i18n"\] == "1" //' /usr/lib/lua/luci/view/web/inc/sysinfo.htm
3453

3554
# unlock change luci.main.lang
3655
sed -i "s/option CHANNEL 'stable'/option CHANNEL 'release'/g" /usr/share/xiaoqiang/xiaoqiang_version
3756

38-
if [ -e "$DIR_PATCH/lang_patch1.sh" ]; then
39-
sh $DIR_PATCH/lang_patch1.sh
40-
fi
41-
4257
echo "lang patched" > /tmp/lang_patch.log
58+
rm -rf $SYNC_OBJ
4359

4460
MAIN_LANG=$( uci -q get luci.main.lang )
4561
[ "$MAIN_LANG" == "" ] && uci set luci.main.lang=en
@@ -48,5 +64,7 @@ uci set luci.languages.en=English
4864
uci commit luci
4965

5066
# reload luci
51-
luci-reload & rm -f /tmp/luci-indexcache & luci-reload
67+
luci-reload
68+
rm -f /tmp/luci-indexcache
69+
luci-reload
5270

data/lang_uninstall.sh

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
#!/bin/sh
22

33
DIR_PATCH=/etc/crontabs/patches
4+
DIR_BACKUP=$DIR_PATCH/lang_backup
45

5-
if [ "$(mount| grep '/usr/lib/lua/luci')" != "" ]; then
6-
umount -l /usr/lib/lua/luci
6+
if [ -d $DIR_BACKUP ]; then
7+
if [ -f $DIR_BACKUP/fw_stable ]; then
8+
sed -i "s/option CHANNEL 'release'/option CHANNEL 'stable'/g" /usr/share/xiaoqiang/xiaoqiang_version
9+
fi
10+
if [ -f $DIR_BACKUP/main_lang ]; then
11+
MAIN_LANG=`cat $DIR_BACKUP/main_lang`
12+
uci set luci.main.lang="$MAIN_LANG"
13+
uci commit luci
14+
fi
15+
cp -f $DIR_BACKUP/base.*.lmo /usr/lib/lua/luci/i18n/
16+
cp -f $DIR_BACKUP/sysinfo.htm /usr/lib/lua/luci/view/web/inc/
717
fi
8-
rm -rf /tmp/_usr_lib_lua_luci
918

10-
if [ "$(mount| grep '/usr/share/xiaoqiang')" != "" ]; then
11-
umount -l /usr/share/xiaoqiang
19+
if grep -q '/lang_patch.sh' /etc/crontabs/root ; then
20+
# remove older version of patch
21+
grep -v "/lang_patch.sh" /etc/crontabs/root > /etc/crontabs/root.new
22+
mv /etc/crontabs/root.new /etc/crontabs/root
23+
/etc/init.d/cron restart
1224
fi
13-
rm -rf /tmp/_usr_share_xiaoqiang
25+
uci delete firewall.auto_lang_patch
26+
uci commit firewall
1427

15-
grep -v "/lang_patch.sh" /etc/crontabs/root > /etc/crontabs/root.new
16-
mv /etc/crontabs/root.new /etc/crontabs/root
17-
/etc/init.d/cron restart
18-
19-
rm -f /etc/rc.lang
20-
rm -f /etc/lang_patch.sh
28+
rm -rf $DIR_BACKUP
2129
rm -f $DIR_PATCH/lang_patch.sh
22-
rm -f $DIR_PATCH/lang_patch1.sh
2330
rm -f $DIR_PATCH/base.*.lmo
2431
rm -f /tmp/lang_patch.log
2532

26-
luci-reload & rm -f /tmp/luci-indexcache & luci-reload
33+
luci-reload
34+
rm -f /tmp/luci-indexcache
35+
luci-reload
2736

install_lang.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
import os
55
import sys
6+
import time
67
import types
78
import re
89

910
import xmir_base
1011
import ssh2
11-
import gateway
12-
from gateway import die
12+
from gateway import *
1313

1414

1515
class www_lmo():
@@ -119,7 +119,7 @@ def gen_sed(w):
119119
v.sed = "sed -i '{}s/{}/{}/g' {}".format(prefix, orig, new, w.fn_remote)
120120

121121

122-
gw = gateway.Gateway()
122+
gw = Gateway()
123123

124124
fn_dir = 'data/'
125125
fn_local = 'data/lang_patch.sh'
@@ -128,11 +128,16 @@ def gen_sed(w):
128128
fn_remote_i = '/tmp/lang_install.sh'
129129
fn_local_u = 'data/lang_uninstall.sh'
130130
fn_remote_u = '/tmp/lang_uninstall.sh'
131-
fn_www_local = 'data/lang_patch1.sh'
132-
fn_www_remote = '/tmp/lang_patch1.sh'
131+
fn_www_local = f'tmp/lang_patch_www.sh'
132+
fn_www_remote = '/tmp/lang_patch_www.sh'
133133

134+
os.makedirs('tmp', exist_ok = True)
135+
136+
full_install = False
134137
action = 'install'
135138
if len(sys.argv) > 1:
139+
if sys.argv[1] == 'full':
140+
full_install = True
136141
if sys.argv[1].startswith('u') or sys.argv[1].startswith('r'):
137142
action = 'uninstall'
138143

@@ -143,17 +148,25 @@ def gen_sed(w):
143148
gw.upload(fn_local_u, fn_remote_u)
144149

145150
if action == 'install':
146-
patch1_installed = True
147-
fn = 'data/lang_patch.log'
151+
patch_installed = 0
152+
fn = 'tmp/lang_patch.log'
148153
if os.path.exists(fn):
149154
os.remove(fn)
150155
try:
151156
gw.download('/tmp/lang_patch.log', fn, verbose = 0)
152157
except ssh2.exceptions.SCPProtocolError:
153-
patch1_installed = False
154-
if patch1_installed:
155-
print("Uninstall lang_patch...")
156-
gw.run_cmd("sh " + fn_remote_u)
158+
patch_installed = 0
159+
txt = ''
160+
if os.path.exists(fn):
161+
with open(fn, 'r') as file:
162+
txt = file.read()
163+
if txt:
164+
patch_installed = 2 if 'www_patch' in txt else 1
165+
if patch_installed >= 2:
166+
die("Full lang patch already installed!")
167+
#if patch_installed:
168+
# print("Uninstall lang_patch...")
169+
# gw.run_cmd(f"chmod +x {fn_remote_u} ; {fn_remote_u}")
157170

158171
if action == 'install':
159172
import po2lmo
@@ -167,6 +180,7 @@ def gen_sed(w):
167180
lmo.save_to_bin(fn_dir + lmo_fname)
168181
gw.upload(fn_dir + lmo_fname, '/tmp/' + lmo_fname)
169182

183+
if action == 'install' and full_install:
170184
dn_www = "tmp/www"
171185
os.makedirs(dn_www, exist_ok = True)
172186
wwwlst = [ "/usr/lib/lua/luci/view/web/index.htm",
@@ -205,14 +219,13 @@ def gen_sed(w):
205219
print("All files uploaded!")
206220

207221
print("Run scripts...")
208-
if action == 'install':
209-
gw.run_cmd("sh " + fn_remote_i)
210-
else:
211-
gw.run_cmd("sh " + fn_remote_u)
212-
213-
gw.run_cmd("rm -f " + fn_remote)
214-
gw.run_cmd("rm -f " + fn_remote_i)
215-
gw.run_cmd("rm -f " + fn_remote_u)
216-
gw.run_cmd("rm -f " + fn_www_remote)
222+
run_script = fn_remote_i if action == 'install' else fn_remote_u
223+
gw.run_cmd(f"chmod +x {run_script} ; {run_script}", timeout = 17)
224+
225+
time.sleep(1.5)
226+
227+
gw.run_cmd(f"rm -f {fn_remote} ; rm -f {fn_remote_i} ; rm -f {fn_remote_u}")
228+
if full_install:
229+
gw.run_cmd(f"rm -f {fn_www_remote}")
217230

218231
print("Ready! The language files are installed.")

0 commit comments

Comments
 (0)