1#!/bin/sh
2# $1: device name, $2: DM_file.
3
4
5f=`nvram get apps_install_folder`
6case $f in
7	"asusware.arm")
8		pkg_type=`echo $f|sed -e "s,asusware\.,,"`
9		PKG_LIST="openssl zlib libcurl libevent ncurses libxml2 libsigc++ libpar2 pcre spawn-fcgi"
10		;;
11	"asusware.big")
12		# DSL big-endian MIPS: DSL-N66U
13		pkg_type="mipsbig"
14		PKG_LIST="openssl zlib libevent ncurses libxml2 pcre spawn-fcgi"
15		;;
16	"asusware.mipsbig")
17		# QCA big-endian MIPS: RT-AC55U
18		pkg_type=`echo $f|sed -e "s,asusware\.,,"`
19		PKG_LIST="openssl zlib libevent ncurses libxml2 pcre spawn-fcgi"
20		;;
21	"asusware")
22		pkg_type="mipsel"
23		PKG_LIST="openssl zlib libcurl libevent ncurses libxml2 libuclibc++ libsigc++ libpar2 pcre spawn-fcgi"
24		;;
25	*)
26		echo "Unknown apps_install_folder: $f"
27		exit 1
28		;;
29esac
30autorun_file=.asusrouter
31nonautorun_file=$autorun_file.disabled
32APPS_INSTALL_FOLDER=`nvram get apps_install_folder`
33APPS_DEV=`nvram get apps_dev`
34apps_from_internet=`nvram get rc_support |grep appnet`
35apps_local_space=`nvram get apps_local_space`
36
37
38# $1: package name.
39# return value. 1: have package. 0: no package.
40_check_package(){
41	package_ready=`ipkg list_installed | grep "$1 "`
42	package_ready2=`app_get_field.sh $1 Enabled 1`
43
44	if [ -z "$package_ready" ] && [ -z "$package_ready2" ]; then
45		return 0
46	else
47		return 1
48	fi
49}
50
51# $1: ipkg log file, $2: the depends of package.
52_check_log_message(){
53	got_log=`cat $1 |sed -n '$p'`
54	action=`echo $got_log |awk '{print $1}'`
55
56	if [ "$action" == "Installing" ] || [ "$action" == "Configuring" ]; then
57		target=`echo $got_log |awk '{print $2}'`
58	elif [ "$action" == "Downloading" ]; then
59		target=`echo $got_log |awk '{print $2}' |awk '{FS="/"; print $NF}' |awk '{FS="_"; print $1}'`
60	elif [ "$action" == "Successfully" ]; then
61		target="terminated"
62	elif [ "$action" == "update-alternatives:" ]; then
63		target=""
64	elif [ -z "$action" ]; then
65		target="Space"
66	else
67		target="error"
68	fi
69
70	got_target=0
71	if [ "$action" == "Installing" ] || [ "$action" == "Configuring" ] || [ "$action" == "Downloading" ]; then
72		check_array=`echo $2 |sed 's/,/ /g'`
73		for check in $check_array; do
74			if [ "$target" == "$check" ]; then
75				got_target=1
76				break
77			fi
78		done
79	fi
80
81	if [ "$got_target" -eq "1" ]; then
82		nvram set apps_depend_action="$action"
83		nvram set apps_depend_action_target="$target"
84	fi
85
86	echo "$target"
87
88	return 0
89}
90
91# $1: delay number.
92_loop_delay(){
93	i=0
94	while [ $i -lt $1 ]; do
95		i=$((i+1))
96		echo "."
97	done
98}
99
100# $1: package name, $2: ipkg log file.
101_log_ipkg_install(){
102	package_deps=`app_get_field.sh $1 Depends 2`
103	package_deps=`echo $package_deps |sed 's/,/ /g'`
104	package_deps_do=
105
106	for dep in $package_deps; do
107		_check_package $dep
108		if [ "$?" == "1" ]; then
109			continue
110		fi
111
112		if [ -z "$package_deps_do" ]; then
113			package_deps_do=$dep
114			nvram set apps_depend_action="$dep"
115			nvram set apps_depend_action_target="Installing"
116		else
117			package_deps_do=$package_deps_do,$dep
118		fi
119	done
120	nvram set apps_depend_do="$package_deps_do"
121
122	ret=`_check_log_message "$2" "$package_deps_do"`
123	while [ "$ret" != "terminated" ] && [ "$ret" != "error" ]; do
124		_loop_delay 10
125		ret=`_check_log_message "$2" "$package_deps_do"`
126	done
127
128	echo "$ret"
129
130	return 0
131}
132
133
134if [ -n "$apps_from_internet" ]; then
135	exit 0
136fi
137
138if [ -z "$APPS_DEV" ]; then
139	echo "Wrong"
140	APPS_DEV=$1
141fi
142
143if [ -z "$APPS_DEV" ] || [ ! -b "/dev/$APPS_DEV" ];then
144	echo "Usage: app_base_library.sh <device name>"
145	nvram set apps_state_error=1
146	exit 1
147fi
148
149APPS_MOUNTED_PATH=`mount |grep "/dev/$APPS_DEV on " |awk '{print $3}'`
150if [ -z "$APPS_MOUNTED_PATH" ]; then
151	echo "$1 had not mounted yet!"
152	nvram set apps_state_error=2
153	exit 1
154fi
155
156APPS_INSTALL_PATH=$APPS_MOUNTED_PATH/$APPS_INSTALL_FOLDER
157if [ -L "$APPS_INSTALL_PATH" ] || [ ! -d "$APPS_INSTALL_PATH" ]; then
158	echo "Building the base directory!"
159	rm -rf $APPS_INSTALL_PATH
160	mkdir -p -m 0777 $APPS_INSTALL_PATH
161fi
162
163if [ ! -f "$APPS_INSTALL_PATH/$nonautorun_file" ]; then
164	rm -rf $APPS_INSTALL_PATH/$autorun_file
165	cp -f $apps_local_space/$autorun_file $APPS_INSTALL_PATH
166	if [ "$?" != "0" ]; then
167		nvram set apps_state_error=10
168		exit 1
169	fi
170else
171	rm -rf $APPS_INSTALL_PATH/$autorun_file
172fi
173
174install_log=$APPS_INSTALL_PATH/ipkg_log.txt
175list_installed=`ipkg list_installed`
176
177for pkg in $PKG_LIST ; do
178	echo "Checking the package: $pkg..."
179	if [ -z "`echo "$list_installed" |grep "$pkg - "`" ]; then
180		echo "Installing the package: $pkg..."
181		ipkg install $apps_local_space/${pkg}_*_${pkg_type}.ipk 1>$install_log &
182		result=`_log_ipkg_install downloadmaster $install_log`
183		if [ "$result" == "error" ]; then
184			echo "Failed to install $pkg!"
185			nvram set apps_state_error=4
186			exit 1
187		else
188			rm -f $install_log
189		fi
190	fi
191done
192
193DM_file=`ls $apps_local_space/downloadmaster_*_$pkg_type.ipk`
194if [ -n "$DM_file" ]; then
195	if [ -n "$2" ]; then
196		file_name=`echo $2 |awk '{FS="/"; print $NF}'`
197	else
198		file_name=`echo $DM_file |awk '{FS="/"; print $NF}'`
199	fi
200	file_ver=`echo $file_name |awk '{FS="_"; print $2}'`
201	DM_version1=`echo $file_ver |awk '{FS="."; print $1}'`
202	DM_version4=`echo $file_ver |awk '{FS="."; print $4}'`
203	if [ "$DM_version1" -gt "2" ] && [ "$DM_version4" -gt "59" ]; then
204		if [ -z "`echo "$list_installed" |grep "readline - "`" ]; then
205			echo "Installing the package: readline..."
206			ipkg install $apps_local_space/readline_*_$pkg_type.ipk 1>$install_log &
207			result=`_log_ipkg_install downloadmaster $install_log`
208			if [ "$result" == "error" ]; then
209				echo "Failed to install readline!"
210				nvram set apps_state_error=4
211				exit 1
212			else
213				rm -f $install_log
214			fi
215		fi
216
217		if [ "$DM_version1" -gt "2" ] && [ "$DM_version4" -gt "74" ]; then
218			if [ -z "`echo "$list_installed" |grep "expat - "`" ]; then
219				echo "Installing the package: expat..."
220				ipkg install $apps_local_space/expat_*_$pkg_type.ipk 1>$install_log &
221				result=`_log_ipkg_install downloadmaster $install_log`
222				if [ "$result" == "error" ]; then
223					echo "Failed to install expat!"
224					nvram set apps_state_error=4
225					exit 1
226				else
227					rm -f $install_log
228				fi
229			fi
230
231			if [ -z "`echo "$list_installed" |grep "wxbase - "`" ]; then
232				echo "Installing the package: wxbase..."
233				ipkg install $apps_local_space/wxbase_*_$pkg_type.ipk 1>$install_log &
234				result=`_log_ipkg_install downloadmaster $install_log`
235				if [ "$result" == "error" ]; then
236					echo "Failed to install wxbase!"
237					nvram set apps_state_error=4
238					exit 1
239				else
240					rm -f $install_log
241				fi
242			fi
243		fi
244	fi
245fi
246
247
248APPS_MOUNTED_TYPE=`mount |grep "/dev/$APPS_DEV on " |awk '{print $5}'`
249if [ "$APPS_MOUNTED_TYPE" == "vfat" ] || [ "$APPS_MOUNTED_TYPE" == "tfat" ]; then
250	app_move_to_pool.sh $APPS_DEV
251	if [ "$?" != "0" ]; then
252		# apps_state_error was already set by app_move_to_pool.sh.
253		exit 1
254	fi
255fi
256
257app_base_link.sh
258if [ "$?" != "0" ]; then
259	# apps_state_error was already set by app_base_link.sh.
260	exit 1
261fi
262
263nvram set apps_depend_do=
264nvram set apps_depend_action=
265nvram set apps_depend_action_target=
266
267echo "Success to build the base environment!"
268