1#!/bin/sh
2# ASUS app switch script
3# $1: package name, $2: device name.
4
5
6autorun_file=.asusrouter
7nonautorun_file=$autorun_file.disabled
8APPS_INSTALL_FOLDER=`nvram get apps_install_folder`
9SWAP_ENABLE=`nvram get apps_swap_enable`
10SWAP_FILE=`nvram get apps_swap_file`
11ORIG_APPS_MOUNTED_PATH=`nvram get apps_mounted_path`
12ORIG_APPS_INSTALL_PATH=$ORIG_APPS_MOUNTED_PATH/$APPS_INSTALL_FOLDER
13apps_local_space=`nvram get apps_local_space`
14APPS_PATH=/opt
15PATH=$APPS_PATH/usr/bin:$APPS_PATH/bin:$APPS_PATH/usr/sbin:$APPS_PATH/sbin:/usr/bin:/bin:/usr/sbin:/sbin
16unset LD_LIBRARY_PATH
17unset LD_PRELOAD
18
19
20# $1: installed path.
21_build_dir(){
22	if [ -z "$1" ]; then
23		return
24	fi
25
26	if [ -L "$1" ] || [ ! -d "$1" ]; then
27		rm -rf $1
28		mkdir -m 0777 $1
29	fi
30}
31
32
33nvram set apps_state_switch=0 # INITIALIZING
34nvram set apps_state_error=0
35if [ -z "$1" ]; then
36	echo "Usage: app_switch.sh <Package name> <device name>"
37	nvram set apps_state_error=1
38	exit 1
39fi
40
41if [ -z "$2" ] || [ ! -b "/dev/$2" ];then
42	echo "Usage: app_switch.sh <Package name> <device name>"
43	nvram set apps_state_error=1
44	exit 1
45fi
46
47APPS_MOUNTED_PATH=`mount |grep "/dev/$2 on " |awk '{print $3}'`
48if [ -z "$APPS_MOUNTED_PATH" ]; then
49	echo "$2 had not mounted yet!"
50	nvram set apps_state_error=2
51	exit 1
52fi
53
54APPS_INSTALL_PATH=$APPS_MOUNTED_PATH/$APPS_INSTALL_FOLDER
55_build_dir $APPS_INSTALL_PATH
56
57nvram set apps_dev=$2
58nvram set apps_mounted_path=$APPS_MOUNTED_PATH
59
60
61nvram set apps_state_switch=1 # STOPPING apps
62if [ -n "$ORIG_APPS_MOUNTED_PATH" ] && [ -d "$ORIG_APPS_INSTALL_PATH" ]; then
63	app_stop.sh
64
65	if [ -f "$ORIG_APPS_INSTALL_PATH/$autorun_file" ]; then
66		mv $ORIG_APPS_INSTALL_PATH/$autorun_file $ORIG_APPS_INSTALL_PATH/$nonautorun_file
67	else
68		cp -f $apps_local_space/$autorun_file $ORIG_APPS_INSTALL_PATH/$nonautorun_file
69	fi
70	if [ "$?" != "0" ]; then
71		nvram set apps_state_error=10
72		exit 1
73	fi
74fi
75
76
77nvram set apps_state_switch=2 # STOPPING swap
78if [ "$SWAP_ENABLE" != "1" ]; then
79	echo "Skip to swap off!"
80elif [ -f "$ORIG_APPS_INSTALL_PATH/$SWAP_FILE" ]; then
81	swapoff $ORIG_APPS_INSTALL_PATH/$SWAP_FILE
82fi
83
84
85nvram set apps_state_switch=3 # CHECKING the chosed pool
86mount_ready=`app_check_pool.sh $2`
87if [ "$mount_ready" == "Non-mounted" ]; then
88	echo "Had not mounted yet!"
89	nvram set apps_state_error=2
90	exit 1
91fi
92
93if [ -d "$APPS_INSTALL_PATH" ]; then
94	app_base_link.sh
95	if [ "$?" != "0" ]; then
96		# apps_state_error was already set by app_base_link.sh.
97		exit 1
98	fi
99fi
100
101if [ -f "$APPS_INSTALL_PATH/$nonautorun_file" ]; then
102	rm -rf $APPS_PATH/$nonautorun_file
103	rm -rf $APPS_INSTALL_PATH/$nonautorun_file
104fi
105
106
107nvram set apps_state_switch=4 # EXECUTING
108app_install.sh $1
109if [ "$?" != "0" ]; then
110	# apps_state_error was already set by app_install.sh.
111	exit 1
112fi
113
114
115nvram set apps_state_switch=5
116