#!/bin/sh
btup() {
	OPKG_PATH=$(grep "dest usb" /etc/opkg.conf | awk '{print $3}')  
	export TERMINFO="$OPKG_PATH/usr/share/terminfo/"
	screen -d -m rtorrent -n -o import=/etc/rtorrent/.rtorrent.rc
	sleep 5
	flag=1
	filepath="/etc/rtorrent/sav"
	hash=""
	while read LINE
	do
		[ $flag = 1 ] && {
			hash=`echo $LINE|awk '{print $1}'`
			flag=0
			echo $hash
			continue
		}
		[ $flag = 0 ] && {
			start=`echo $LINE|awk '{print $1}'`
			echo $start
			[ $start = 1 ] && {
				xmlrpc 127.0.0.1 d.open $hash
				xmlrpc 127.0.0.1 d.start $hash 
				logger "start=1"
			}
			[ $start = 0 ] && {
                   	        xmlrpc 127.0.0.1 d.stop $hash
                   	        xmlrpc 127.0.0.1 d.close $hash
                        	logger "start=0"
			}
			flag=1
		}
	done <$filepath
}
btdown(){
	xmlrpc 127.0.0.1 d.multicall main d.get_hash= d.get_state= |grep -E 'String|Integer'|awk '{print $4}'|tr -d \' > /etc/rtorrent/sav
	kill `ps -ef|grep 'rtorrent'|awk   '{print   $1}'`  2>/dev/null
}
case "$1" in
	down) btdown ;;
	up) btup ;; 
esac
