1#!/bin/sh
2# app version format :
3#    <app name>-<app version>-<model name>-<firmware version>-<dash version>.<ext file name>
4#    eg: kwilt-v<kwilt version>-r7800-v<r7800 version>-<dash version>.<ext file name>
5#    eg: kwilt-v1.2-r7800-v1.0.4.60-002.tar.gz
6#
7
8oc () { "$@" >> /dev/console 2>&1 ; } # stdout & stderr to /dev/console
9
10appname=kwilt
11modelname=r7800
12app_pid_file=/var/run/$appname.pid
13app_info_file=/tmp/$appname.info
14app_tgz_file=/tmp/$appname.tar.gz
15app_update_url="ftp://updates1.netgear.com/sw-apps/$appname/$modelname/"
16#app_update_url="-u user:passwd ftp://172.16.83.218/tmp/$modelname/"
17
18app_updated_file="/tmp/.$appname.updated"
19
20app_has_updated()
21{
22	[ -f $app_updated_file ]
23}
24
25touch_app_has_updated()
26{
27	touch $app_updated_file
28}
29
30app_local_version_file=/hipplay/version
31
32get_app_local_version()
33{
34	[ -f $app_local_version_file ] && cat $app_local_version_file
35}
36
37set_app_local_version() # $1: fullversion
38{
39	echo $1 > $app_local_version_file
40}
41
42dlna_has_run()
43{
44	[ -n "$(pidof minidlna)" ]
45}
46
47start()
48{
49	app_has_updated || { oc echo "[$appname] no run, has not been updated !"; return; }
50	dlna_has_run || { oc echo "[$appname] no run, dlna has not been run !"; return; }
51	if [ -d /hipplay ]; then
52		# do not print the log to console.
53		/hipplay/bin/busybox sh /hipplay/daemon start >/dev/null 2>&1 &
54	fi
55}
56
57stop()
58{
59	[ -d /hipplay ] && /hipplay/bin/busybox sh /hipplay/daemon stop
60}
61
62filter_app_info()
63{
64	local lapp=$(echo $appname | tr "A-Z" "a-z")
65	local lmodel=$(echo $modelname | tr "A-Z" "a-z")
66	local app
67
68	while read app; do
69		echo "$app" | tr "A-Z" "a-z" | grep -q '^'$lapp'-v[0-9.]*-'$lmodel'-v[0-9.]*-[0-9]*\..*$' \
70			&& echo "$app"
71	done
72}
73
74get_device_firmware_version()
75{
76	sed -n 's/^V\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*$/\1/p' /firmware_version
77}
78
79newer_version() # $1: x.x.x.x, $2: x.x.x.x, (return true if $1 >= $2)
80{
81	local v1 v2 n=1
82
83	while [ 1 ]; do
84		v1=$(echo $1 |awk -F. '{print $'$n'}')
85		v2=$(echo $2 |awk -F. '{print $'$n'}')
86		[ -z "$v2" ] && return 0
87		[ -z "$v1" ] && return 1
88		[ "$v1" -gt "$v2" ] && return 0
89		[ "$v1" -lt "$v2" ] && return 1
90		n=$(($n + 1))
91	done
92}
93
94app_version_allowed() # $1: fullversion
95{
96	local v1=$(get_device_firmware_version)
97	local v2=$(echo $1 | sed -n 's/^.*-v[0-9.]*-.*-v\([0-9.]*\)-[0-9]*\..*$/\1/p')
98	[ -z "$v2" ] && return 1
99	newer_version $v1 $v2 && return 0 || return 1
100}
101
102exit_update() # $1: exit_status, $2: message
103{
104	oc echo "$2"
105	rm -f $app_info_file
106	rm -f $app_tgz_file
107	rm -f $app_pid_file
108	exit $1
109}
110
111run_local_version_and_exit() # $1: exit_status, $2: message
112{
113	oc echo "[$appname] run local version, since $2"
114	touch_app_has_updated && start
115	exit_update "$1"
116}
117
118try_to_run_local_version_and_exit() # $1: exit_status, $2: message
119{
120	app_version_allowed $(get_app_local_version) || return 
121	run_local_version_and_exit "$1" "$2"
122}
123
124newer_app() # $1: fullversion, $2: fullversion, (return true if $1 >= $2)
125{
126	local v1=$(echo $1 | sed -n 's/^.*-v[0-9.]*-.*-v\([0-9.]*\)-[0-9]*\..*$/\1/p')
127	local v2=$(echo $2 | sed -n 's/^.*-v[0-9.]*-.*-v\([0-9.]*\)-[0-9]*\..*$/\1/p')
128
129	if [ "$v1" = "$v2" ]; then
130		v1=$(echo $1 | sed -n 's/^.*-v[0-9.]*-.*-v[0-9.]*-\([0-9]*\)\..*$/\1/p')
131		v2=$(echo $2 | sed -n 's/^.*-v[0-9.]*-.*-v[0-9.]*-\([0-9]*\)\..*$/\1/p')
132	fi
133
134	newer_version "$v1" "$v2" && return 0 || return 1
135}
136
137get_fullversion() # $1: app_info_file
138{
139	local app apps="$(awk '{print $1}' $1)"
140	local app_final=""
141
142	for app in $apps; do
143		app_version_allowed "$app" && newer_app "$app" "$app_final" && app_final="$app"
144	done
145	echo "$app_final"
146}
147
148get_dynamic_sleep_time() # $1: retry
149{
150	case "$1" in
151		1) echo 30 ;;
152		2) echo 60 ;;
153		3) echo 360 ;;
154		4) echo 900 ;;
155		*) echo 3600 ;;
156	esac
157}
158
159update()
160{
161	local retry fullversion localversion
162
163	app_has_updated && { oc echo "[$appname] no update, has ever been updated !"; return 1; }
164	[ -f $app_pid_file ] && { oc echo "[$appname] no update, others running now !"; return 1; }
165	echo "$$" > $app_pid_file
166
167	oc echo "[$appname] start to get info from update-sever : $app_update_url"
168	retry=0
169	while [ 1 ]; do
170		curl -l $app_update_url 2>/dev/null | filter_app_info > $app_info_file
171		[ -s $app_info_file ] && break
172
173		oc echo "[$appname] retry=$retry, fail to get update-server info."
174		[ "$retry" = "2" ] && try_to_run_local_version_and_exit "1" "update is given up !"
175
176		retry=$(($retry + 1)) && sleep $(get_dynamic_sleep_time $retry)
177	done
178
179	fullversion="$(get_fullversion $app_info_file)"
180	if [ -z "$fullversion" ]; then
181		try_to_run_local_version_and_exit "1" "bug in update-server"
182		exit_update "2" "[$appname] bug in local versoin & update-server !"
183	fi
184
185	localversion=$(get_app_local_version)
186	app_version_allowed "$localversion" && newer_app "$localversion" "$fullversion" && \
187		run_local_version_and_exit "0" "no newer version !"
188
189	oc echo "[$appname] start to download image : $app_update_url$fullversion"
190	retry=0
191	while [ 1 ]; do
192		curl $app_update_url$fullversion -o $app_tgz_file 2>/dev/null && break
193
194		oc echo "[$appname] retry=$retry, fail to download image."
195		[ "$retry" = "2" ] && try_to_run_local_version_and_exit "1" "download is given up !"
196
197		retry=$(($retry + 1)) && sleep $(get_dynamic_sleep_time $retry)
198	done
199
200	rm -rf /hipplay
201	mkdir /hipplay
202	tar xfz $app_tgz_file -C /hipplay
203	set_app_local_version "$fullversion"
204	oc echo "[$appname] install image \"$app_update_url$fullversion\" to R/W filesystem successfully."
205
206	run_local_version_and_exit "0" "the newly installed."
207}
208
209case "$1" in
210	start) start ;;
211	stop) stop ;;
212	update) update ;;
213esac
214
215