#!/bin/sh

###############################################
# script: install.sh                          #
###############################################
# author: kom65                               #
# web: http://iconbit-script.narod.ru         #
# contact: Email: iconbit-script@yandex.ru    #
# Version: 1.0  (15/01/2013)                  #
###############################################

##############################################################################
login_a(){
txt=""
echo "Enter login for admin rtorrent"
echo -n "Login[admin]$txt: "
read txt
if [ -z "$txt" ]; then
        echo "Error input!"
        sleep 2
	echo "...coping defaulte passwd file"
	cp ./files/conf/lighttpd/passwd /opt/etc/lighttpd/
	login="admin"
	pass="admin"
	return 0
else
        login=$txt
	pass_a
fi
}

pass_a(){
txt=""
echo "Enter password for admin rtorrent"
echo -n "Password$txt: "
read txt
if [ -z "$txt" ]; then
        echo "Error input!"
        sleep 2
        pass_a
else
        pass_1=$txt
        txt=""
        echo -n "REPassword$txt: "
        read txt
        if [ -z "$txt" ]; then
                echo "Error input!"
                sleep 2
                pass_a
        else
                if [ "$txt" = "$pass_1" ];then
                        pass=$txt
                else
                        pass_a
                fi
        fi
fi
}
#############################################################################
if [ -e /opt/bin/clear ]; then clear; fi

echo -n "------------------------Start installing"
var0=0
LIMIT=10
while [ "$var0" -lt "$LIMIT" ];do
echo -n "."
sleep 1
var0=`expr $var0 + 1`
done


repo='http://iconbit-script.narod.ru'
#repo='http://night3.rosnet.ru/download/temp_install_iconbit'

echo "------------------------Check availability optware"
if [ ! -d /data/icapp ]; then 
echo "------------------------No-added system upgrade"
exit
fi
echo "------------------------Optware is available"

echo "------------------------Install a system for recording"
mount -o,remount,rw /
echo "------------------------Make a temp directory"
mkdir ./tmp
cd ./tmp
echo "------------------------Download the required files...."

wget --tries=5 --retry-connrefused --connect-timeout=10 $repo/rtorrent_install_files.tar -O rtorrent_install_files.tar
if [ ! -e ./rtorrent_install_files.tar ]; then
echo "------------------------Files not full downloading.Please try again later."
exit
fi

echo "------------------------Extracting files...."
tar -xvf rtorrent_install_files.tar  #-C ./tmp
rm ./rtorrent_install_files.tar
# tar -cvf  rtorrent_install_files.tar files

ipkg update

echo "------------------------Installing lighttpd and php-fcgi...."

ipkg install lighttpd
ipkg install php-fcgi

echo "------------------------Coping files configuraition for lighttpd....."
echo "...coping lighttpd.conf"
cp ./files/conf/lighttpd/lighttpd.conf /opt/etc/lighttpd/
echo "...coping 10-php-fcgi.conf"
cp ./files/conf/lighttpd/10-php-fcgi.conf /opt/etc/lighttpd/conf.d/
echo "...coping 11-auth-userfile-rtorrent.conf"
cp ./files/conf/lighttpd/11-auth-userfile-rtorrent.conf /opt/etc/lighttpd/conf.d/
echo "...coping 12-scgi-server.conf"
cp ./files/conf/lighttpd/12-scgi-server.conf /opt/etc/lighttpd/conf.d/

cp ./files/rcex.d/S80lighttpd /data/icapp/etc/rcex.d/
chmod a+x /data/icapp/etc/rcex.d/S80lighttpd
echo "...coping S80lighttpd to rcex.nfo"
cp ./files/rcex.nfo/S80lighttpd /data/icapp/etc/rcex.nfo/S80lighttpd
echo "------------------------End copy files configuraition for lighttpd"

if [ -e /opt/etc/init.d/S80lighttpd ]; then rm /opt/etc/init.d/S80lighttpd;fi

if [ ! -d /tmp/usbmounts/sda1/torrent/dl ]; then 
echo "------------------------Making dir..."
mkdir -p /tmp/usbmounts/sda1/torrent/dl
fi
if [ ! -d /tmp/usbmounts/sda1/torrent/session ]; then
echo "------------------------Making dir..."
mkdir -p /tmp/usbmounts/sda1/torrent/session
fi

echo "------------------------Installing rtorrent and rutorrent and required ipkg..."
ipkg install rtorrent
ipkg install adduser
ipkg install procps
ipkg install grep
ipkg install screen
ipkg install gawk
#ipkg install tee
#ipkg install stty

echo "------------------------Coping configuration files...."
echo "...coping rtorrent.conf"
cp ./files/conf/rtorrent/rtorrent.conf /opt/etc/
chmod a+x /opt/etc/rtorrent.conf
echo "...coping S99rtorrent"
cp ./files/rcex.d/S99rtorrent /data/icapp/etc/rcex.d/
echo "...coping K99rtorrent"
cp ./files/rcex.d/K99rtorrent /data/icapp/etc/rcex.d/
echo "...coping S99rtorrent to rcex.nfo"
cp ./files/rcex.nfo/S99rtorrent /data/icapp/etc/rcex.nfo/S99rtorrent

if [ -e /opt/etc/init.d/S99rtorrent ]; then rm /opt/etc/init.d/S99rtorrent;fi
echo "...coping pre-shutdown"
cp ./files/conf/rtorrent/pre-shutdown /tmp/opt/usr/lib/
echo "...coping signal_and_wait.inc.sh"
cp ./files/conf/rtorrent/signal_and_wait.inc.sh /tmp/opt/usr/lib/

chmod a+x /data/icapp/etc/rcex.d/S99rtorrent
chmod a+x /data/icapp/etc/rcex.d/K99rtorrent
chmod a+x /tmp/opt/usr/lib/pre-shutdown
chmod a+x /tmp/opt/usr/lib/signal_and_wait.inc.sh

echo "...coping rtorrent.pkg"
cp ./files/conf/rtorrent/rtorrent.pkg /data/icapp/
echo "...coping rtorrent.cgi"
cp ./files/conf/rtorrent/rtorrent.cgi /opt/share/www/
echo "...coping rtorrent.str"
cp ./files/conf/rtorrent/rtorrent.str /opt/share/www/

chmod a+x /opt/share/www/rtorrent.cgi
echo "------------------------End copy files configuraition for rtorrent"
echo "------------------------Coping rutorrent..."

echo "...coping rutorrent"
cp ./files/rtorrent /opt/share/www/ -R

login_a

echo "$login:$pass" > /opt/etc/lighttpd/passwd

if [ -e /opt/bin/clear ]; then clear; fi

cp ./files/conf/rtorrent/bin/rtorrent /opt/bin/rtorrent
chmod a+x /opt/bin/rtorrent

cd ..
rm ./tmp -R
echo "-------------------------Starting service lighttpd...."
/data/icapp/etc/rcex.d/S80lighttpd restart
echo "-------------------------Starting service rtorrent...."
/data/icapp/etc/rcex.d/S99rtorrent restart
echo "-------------------------Installing complete"

echo "To access, use http://<ip adress>:8081/rtorrent"
echo "Login:$login"
echo "Password:$pass"

exit

#/etc/profile

#pathmunge /opt/bin
#pathmunge /opt/sbin
#pathmunge /opt/etc/init.d
