#!/bin/sh /etc/rc.common
 START=99



g_ret=0
INSTALL_PATH=$(grep "dest usb" /etc/opkg.conf | awk '{print $3}')  
DISK_PATH=${INSTALL_PATH%/opkg-install}
#DISK_PATH="/mnt/usb_sda1"
#INSTALL_PATH="$DISK_PATH/opkg-install"

BIN=/usr/bin/transmission-daemon
SSD=start-stop-daemon

LIST_SEP=""
append_params() {
	local p; local v; local s="$1"; shift
	for p in $*; do
		config_get v "$s" "$p"
		IFS="$LIST_SEP"
		for v in $v; do
			[ -n "$v" ] && (
				p=${p//_/-}
				echo "\""$p"\": "$v"," | sed -e 's|_|_|g' >> $config_dir/settings.json
			)
		done
		unset IFS
	done
}

append_params_quotes() {
	local p; local v; local s="$1"; shift
	for p in $*; do
		config_get v "$s" "$p"
		IFS="$LIST_SEP"
		for v in $v; do
			[ -n "$v" ] && (
				p=${p//_/-}
				echo "\""$p"\": \""$v"\"," | sed -e 's|/|\\/|g;s|_|_|g' >> $config_dir/settings.json
			)
		done
		unset IFS
	done
}

start_service() {
	local s="$1"
	local enable=0

	# disabled?
	config_get_bool enable "$s" enable 0
	[ "$enable" == 0 ] && return 0
	config_get config_dir "$s" config_dir ''
	mkdir -p "$config_dir"

	echo "{" > $config_dir/settings.json

	append_params "$s" \
		alt_speed_down alt_speed_enabled alt_speed_time_begin alt_speed_time_day \
		alt_speed_time_enabled alt_speed_time_end alt_speed_up \
		blocklist_enabled dht_enabled encryption \
		incomplete_dir_enabled lazy_bitfield_enabled message_level \
		open_file_limit peer_limit_global peer_limit_per_torrent peer_port \
		peer_port_random_high peer_port_random_low peer_port_random_on_start \
		peer_socket_tos pex_enabled port_forwarding_enabled preallocation \
		proxy_auth_enabled proxy_enabled proxy_port proxy_type ratio_limit \
		ratio_limit_enabled rename_partial_files rpc_authentication_required \
		rpc_enabled rpc_port rpc_whitelist_enabled speed_limit_down \
		speed_limit_down_enabled speed_limit_up speed_limit_up_enabled umask \
		upload_slots_per_torrent watch_dir_enabled

	append_params_quotes "$s" \
		bind_address_ipv4 bind_address_ipv6 download_dir incomplete_dir proxy \
		proxy_auth_password proxy_auth_username \
		rpc_bind_address rpc_password rpc_username rpc_whitelist watch_dir

	echo "\""invalid-key"\": false" >> $config_dir/settings.json
	echo "}" >> $config_dir/settings.json
	
	eval "nice -n 19 $SSD -q -b -x $BIN -S -- -g $config_dir"
}

CheckIns () {      
	echo "CheckIns"
	RET=`uci -q get bt.@transmission[0].Installing 2>/dev/null`
	[ $RET -eq 5 ] && exit 1
	[ $RET -eq 2 ] && {
		g_ret=`expr $g_ret + 1`
		[ $g_ret -gt 10 ] && {
	  		echo "install_bt: install error"
	  		uci -q set bt.@transmission[0].Installing=5 2>/dev/null
	  		exit 1    
		}     
		echo "install_bt: fail, restart loop"       
		uci -q set bt.@transmission[0].Installing=1 2>/dev/null     
		return 0
	}
	return 1
}


OpkgIns(){
	echo "OpkgIns: $3"
	 [ ! -e $INSTALL_PATH/$1/$2 ] && {
		echo 1 > /tmp/bt_tt
		opkg -V2 --nodeps -dest usb install "$PKG_SERVER""$3" 2>>/tmp/bt_err
		CheckIns;
		[ $? -eq 0 ] && return 0
	  	[ ! -e $INSTALL_PATH/$1/$2 ] && {
			g_ret=`expr $g_ret + 1`
	  		return 0
	  	}
	 }
	 return 1
}



stop() {
	uci -q set bt.@transmission[0].enable=0
	killall `basename $BIN`
}


start() {
	[ -e /usr/bin/transmission-daemon ] && {
		uci -q set bt.@transmission[0].enable=1
		config_load transmission
		config_foreach start_service transmission
	}
}

restart() {
	stop; sleep 5; start
}

install() {
	[ ! -d $DISK_PATH  ] && exit 1

	stop
	sleep 3
	remove_ln

	uci -q set bt.@transmission[0].Installing=1 2>/dev/null
	
	[ ! -d $INSTALL_PATH/usr/lib/opkg ] && {
		mkdir $INSTALL_PATH
		mkdir $INSTALL_PATH/usr
		mkdir $INSTALL_PATH/usr/lib
		mkdir $INSTALL_PATH/usr/lib/opkg
	}
	#[ ! -e $INSTALL_PATH/usr/lib/opkg/status ] && {
		cp /www/cgi-bin/webstax/status $INSTALL_PATH/usr/lib/opkg/status
	#}
	. /sbin/vigorcode.sh
	while [ $g_ret -le 15 ]
	do
		echo "install_bt: enter loop $g_ret"
		OpkgIns   usr/lib   libcurl.so.4.0.1      libcurl_7.17.1-1_arm.ipk
		[  $? -eq 0 ] && continue
		OpkgIns   usr/lib   libevent-1.4.so.2.1.3    libevent_1.4.10-stable-1_arm.ipk
		[  $? -eq 0 ] && continue
		OpkgIns   usr/bin   transmission-daemon    transmission-daemon_2.13-1_arm.ipk
		[  $? -eq 0 ] && continue
		#OpkgIns   usr/bin   transmissioncli    transmission-cli_1.92-1_arm.ipk
		#[  $? -eq 0 ] && continue
		[ ! -d $INSTALL_PATH/usr/share/transmission/web ] && {
			opkg -V2 --nodeps -dest usb install "$PKG_SERVER"transmission-web_2.13-1_arm.ipk 2>>/tmp/bt_err
		}
		break
	done
	ln -s $INSTALL_PATH/usr/lib/libevent-1.4.so.2.1.3 /usr/lib/libevent-1.4.so.2.1.3 2>/dev/null
	ln -s $INSTALL_PATH/usr/lib/libevent-1.4.so.2.1.3 /usr/lib/libevent-1.4.so.2 2>/dev/null
	ln -s $INSTALL_PATH/usr/lib/libcurl.so.4.0.1 /usr/lib/libcurl.so.4.0.1 2>/dev/null
	ln -s $INSTALL_PATH/usr/lib/libcurl.so.4.0.1 /usr/lib/libcurl.so.4 2>/dev/null
	ln -s $INSTALL_PATH/usr/bin/transmission-daemon /usr/bin/transmission-daemon
	ln -s $INSTALL_PATH/usr/bin/transmission-remote  /usr/bin/transmission-remote 
	#ln -s $INSTALL_PATH/usr/bin/transmissioncli  /usr/bin/transmissioncli
	ln -s $INSTALL_PATH/usr/share/transmission/ /usr/share/transmission
	#cp "$INSTALL_PATH/etc/init.d/transmission" "/etc/init.d/"
	#cp "$INSTALL_PATH/etc/config/transmission" "/etc/config/"
	
	uci -q set bt.@transmission[0].Installing=3 2>/dev/null
	start
	sh /lib/firewall/user.sh > /dev/null 2>&1
}



remove(){
	stop
	sleep 3
	remove_ln
	rm $INSTALL_PATH/usr/lib/libevent-1.4.so.2 
	rm $INSTALL_PATH/usr/lib/libevent-1.4.so.2.1.3  
	rm $INSTALL_PATH/usr/lib/libcurl.so.4 
	rm $INSTALL_PATH/usr/lib/libcurl.so.4.0.1  
	rm $INSTALL_PATH/usr/bin/transmission*
	rm -R $INSTALL_PATH/usr/share/transmission
}


remove_ln (){
	rm /usr/lib/libevent-1.4.so.2 
	rm /usr/lib/libevent-1.4.so.2.1.3  
	rm /usr/lib/libcurl.so.4 
	rm /usr/lib/libcurl.so.4.0.1  
	rm /usr/bin/transmission*
	rm /usr/share/transmission
}
boot (){
	[ -e /usr/lib/libevent-1.4.so.2.1.3 ] || ln -s $INSTALL_PATH/usr/lib/libevent-1.4.so.2.1.3 /usr/lib/libevent-1.4.so.2.1.3 2>/dev/null
	[ -e /usr/lib/libevent-1.4.so.2 ] || ln -s $INSTALL_PATH/usr/lib/libevent-1.4.so.2.1.3 /usr/lib/libevent-1.4.so.2 2>/dev/null
	[ -e /usr/lib/libcurl.so.4.0.1 ] || ln -s $INSTALL_PATH/usr/lib/libcurl.so.4.0.1 /usr/lib/libcurl.so.4.0.1 2>/dev/null
	[ -e /usr/lib/libcurl.so.4 ] || ln -s $INSTALL_PATH/usr/lib/libcurl.so.4.0.1 /usr/lib/libcurl.so.4 2>/dev/null
	[ -e /usr/bin/transmission-daemon ] || ln -s $INSTALL_PATH/usr/bin/transmission-daemon /usr/bin/transmission-daemon
	[ -e /usr/bin/transmission-remote ] || ln -s $INSTALL_PATH/usr/bin/transmission-remote /usr/bin/transmission-remote 
	[ -e /usr/bin/transmissioncli ] || ln -s $INSTALL_PATH/usr/bin/transmissioncli /usr/bin/transmissioncli
	[ -e /usr/share/transmission ] || ln -s $INSTALL_PATH/usr/share/transmission/ /usr/share/transmission
	start
}