#!/bin/ash

SERVER="10.0.1.226"

FIRMWARE() { 
wget http://$SERVER/scripts/firmware -O /usr/bin/firmware
chmod +x /usr/bin/firmware
if [ ! -s /usr/bin/firmware ];
then
	echo "Firmware script not downloaded!" >> /etc/banner
fi
}

BACKUP() { 
wget http://$SERVER/scripts/backup -O /usr/bin/backup
chmod +x /usr/bin/backup
if [ ! -s /usr/bin/backup ];
then
	echo "Backup script not downloaded!" >> /etc/banner
else
	/usr/bin/backup
fi
}

CRONTAB() { 
wget http://$SERVER/scripts/crontab -O /etc/crontabs/root
if [ ! -s /etc/crontabs/root ];
then
	echo "Crontab config not downloaded!" >> /etc/banner
else
	#NANOPI=`cat /etc/board.json | grep nanopi | awk -F'"' '{print $4 }'`
	#if [ -z ${NANOPI} ];
	#then
	#	CRONN=`cat /etc/crontabs/root | grep reboot`
        #        if [ -z ${CRONN}  ];
        #        then
	#		echo "0 4 * * * /sbin/reboot" >> /etc/crontabs/root
	#	fi
	#fi
	/etc/init.d/cron restart
fi
}

SSH() { 
wget http://$SERVER/scripts/authorized_keys -O /etc/dropbear/authorized_keys
if [ ! -s /etc/dropbear/authorized_keys ];
then
	echo "Authorized keys not downloaded!" >> /etc/banner
else
	chmod 0700 /etc/dropbear/authorized_keys
fi
mkdir /root/.ssh
wget http://$SERVER/scripts/known_hosts -O /root/.ssh/known_hosts
if [ ! -s /root/.ssh/known_hosts ];
then
	echo "Known hosts not downloaded!" >> /etc/banner
fi
}

KEEPALIVE() {
NANOPI=`cat /etc/board.json | grep nanopi | awk -F'"' '{print $4 }'`
if [ -z ${NANOPI} ];
then
        wget http://$SERVER/scripts/ap -O /usr/bin/keepalive
else
	wget http://$SERVER/scripts/router -O /usr/bin/keepalive
fi
if [ -s /usr/bin/keepalive ];
then
	chmod +x /usr/bin/keepalive
	if [ -s /etc/crontabs/root ];
	then
		CRON=`cat /etc/crontabs/root | grep keepalive`
		if [ -z ${CRON}  ];
		then
			echo "*/15 * * * * /usr/bin/keepalive " >> /etc/crontabs/root
			/etc/init.d/cron restart
		fi
	fi
fi
}

#Execucao
sed -i '/Initial/d' /etc/banner
#sed -i '/initial/d' /etc/rc.local

FIRMWARE

CRONTAB

SSH

#KEEPALIVE

BACKUP

#sed -i '/initial/d' /etc/rc.local

exit 0
