1#!/bin/sh
2# $1: package name.
3
4
5APPS_INSTALL_FOLDER=`nvram get apps_install_folder`
6APPS_PATH=/opt
7APPS_DEV=`nvram get apps_dev`
8APPS_MOUNTED_PATH=`nvram get apps_mounted_path`
9APPS_INSTALL_PATH=$APPS_MOUNTED_PATH/$APPS_INSTALL_FOLDER
10
11
12# $1: package name.
13# return value. 1: have package. 0: no package.
14_check_package(){
15	package_ready=`ipkg list_installed | grep "$1 "`
16
17	if [ -z "$package_ready" ]; then
18		return 0
19	else
20		return 1
21	fi
22}
23
24
25nvram set apps_state_remove=0 # INITIALIZING
26nvram set apps_state_error=0
27if [ -z "$1" ]; then
28	echo "Usage: app_remove.sh <Package name>"
29	nvram set apps_state_error=1
30	exit 1
31fi
32
33if [ -z "$APPS_MOUNTED_PATH" ]; then
34	echo "Had not mounted yet!"
35	nvram set apps_state_error=2
36	exit 1
37fi
38
39_check_package $1
40if [ "$?" == "0" ]; then
41	echo "The \"$1\" is not installed yet!"
42	nvram set apps_state_remove=2 # FINISHED
43	exit 0
44fi
45
46need_asuslighttpd=0
47need_smartsync=0
48if [ "$1" == "downloadmaster" ]; then
49	DM_version1=`app_get_field.sh downloadmaster Version 1 |awk '{FS=".";print $1}'`
50	DM_version4=`app_get_field.sh downloadmaster Version 1 |awk '{FS=".";print $4}'`
51
52	if [ "$DM_version1" -gt "3" ]; then
53		need_asuslighttpd=1
54	elif [ "$DM_version1" -eq "3" ] && [ "$DM_version4" -gt "59" ]; then
55		need_asuslighttpd=1
56	fi
57elif [ "$1" == "mediaserver" ]; then
58	MS_version1=`app_get_field.sh mediaserver Version 1 |awk '{FS=".";print $1}'`
59	MS_version4=`app_get_field.sh mediaserver Version 1 |awk '{FS=".";print $4}'`
60
61	if [ "$MS_version1" -gt "1" ]; then
62		need_asuslighttpd=1
63	elif [ "$MS_version1" -eq "1" ] && [ "$MS_version4" -gt "15" ]; then
64		need_asuslighttpd=1
65	fi
66elif [ "$1" == "aicloud" ]; then
67	AC_version1=`app_get_field.sh aicloud Version 1 |awk '{FS=".";print $1}'`
68	AC_version4=`app_get_field.sh aicloud Version 1 |awk '{FS=".";print $4}'`
69
70	if [ "$AC_version1" -gt "1" ]; then
71		need_smartsync=1
72	elif [ "$AC_version1" -eq "1" ] && [ "$AC_version4" -gt "4" ]; then
73		need_smartsync=1
74	fi
75fi
76
77if [ "$need_asuslighttpd" == "1" ]; then
78	dep_on_asuslighttpd=0
79	if [ "$1" == "downloadmaster" ] ; then
80		dep_on_asuslighttpd=`ipkg list_installed|awk '{print $1}'|grep -c mediaserver`
81	elif [ "$1" == "mediaserver" ] ; then
82		dep_on_asuslighttpd=`ipkg list_installed|awk '{print $1}'|grep -c downloadmaster`
83	fi
84
85	if [ "$dep_on_asuslighttpd" != "0" ] ; then
86		echo "asuslighttpd is need by another installed package"
87		need_asuslighttpd=0
88	fi
89fi
90
91nvram set apps_state_remove=1 # REMOVING
92echo "Removing the package: $1..."
93app_set_enabled.sh $1 no
94ipkg remove $1
95if [ "$?" != "0" ]; then
96	nvram set apps_state_error=9
97	exit 1
98fi
99
100if [ "$need_asuslighttpd" == "1" ]; then
101	_check_package asuslighttpd
102	if [ "$?" != "0" ]; then
103		echo "Removing the dependent package: asuslighttpd..."
104		app_set_enabled.sh asuslighttpd no
105		ipkg remove asuslighttpd
106		if [ "$?" != "0" ]; then
107			nvram set apps_state_error=9
108			exit 1
109		fi
110	fi
111elif [ "$need_smartsync" == "1" ]; then
112	_check_package smartsync
113	if [ "$?" != "0" ]; then
114		echo "Removing the dependent package: smartsync..."
115		app_set_enabled.sh smartsync no
116		ipkg remove smartsync
117		if [ "$?" != "0" ]; then
118			nvram set apps_state_error=9
119			exit 1
120		fi
121	fi
122
123	deps=`app_get_field.sh smartsync Depends 2 |sed 's/,/ /g'`
124
125	for dep in $deps; do
126		_check_package $dep
127		if [ "$?" != "0" ]; then
128			echo "Removing the dependent package of smartsync: $dep..."
129			app_set_enabled.sh $dep no
130			ipkg remove $dep
131			if [ "$?" != "0" ]; then
132				nvram set apps_state_error=9
133				exit 1
134			fi
135		fi
136	done
137fi
138
139APPS_MOUNTED_TYPE=`mount |grep "/dev/$APPS_DEV on " |awk '{print $5}'`
140if [ "$APPS_MOUNTED_TYPE" != "vfat" ] && [ "$APPS_MOUNTED_TYPE" != "tfat" ]; then
141	nvram set apps_state_remove=2 # FINISHED
142	exit 0
143fi
144
145objs=`ls -a $APPS_INSTALL_PATH`
146for obj in $objs; do
147	if [ "$obj" == "." ] || [ "$obj" == ".." ]; then
148		continue
149	fi
150
151	if [ "$obj" != "bin" ] && [ "$obj" != "lib" ] && [ ! -e "$APPS_PATH/$obj" ]; then
152		rm -rf $APPS_INSTALL_PATH/$obj
153	fi
154done
155
156objs=`ls -a $APPS_INSTALL_PATH/bin`
157for obj in $objs; do
158	if [ "$obj" == "." ] || [ "$obj" == ".." ]; then
159		continue
160	fi
161
162	if [ ! -e "$APPS_PATH/bin/$obj" ]; then
163		rm -rf $APPS_INSTALL_PATH/bin/$obj
164	fi
165done
166
167objs=`ls -a $APPS_INSTALL_PATH/lib`
168for obj in $objs; do
169	if [ "$obj" == "." ] || [ "$obj" == ".." ]; then
170		continue
171	fi
172
173	if [ ! -e "$APPS_PATH/lib/$obj" ]; then
174		rm -rf $APPS_INSTALL_PATH/lib/$obj
175	fi
176done
177
178app_base_link.sh
179if [ "$?" != "0" ]; then
180	# apps_state_error was already set by app_base_link.sh.
181	exit 1
182fi
183
184nvram set apps_state_remove=2 # FINISHED
185