#!/bin/sh


web="/var/www/html"




install_web()
{


echo "Setting up web interface"
echo "========================"
echo $Xn "Inprotect webroot path [/var/www/html] " $Xc
read webroot < /dev/tty
test -n "$webroot" && web="$webroot"

test -d "$web" || {
        echo $Xn "$web does not exist - do you want to create it ? [y] " $Xc
        read answer < /dev/tty
        test "$answer" = "n" && {echo "Can not continue then. Bye" && exit}
        mkdir -p $web || {
                echo "Could not create $web"
                exit
                }
        }

cp console/html/* $web -rf
cp console/scripts/fetchupdates.pl /usr/local/bin -rf
cp console/scripts/inprotect.cfg /usr/local/etc -rf

crontab -l | grep -F -v -f console/crontab_web | grep -v ^\# > /tmp/cron.tmp
cat console/crontab_web >> /tmp/cron.tmp
crontab /tmp/cron.tmp
rm /tmp/cron.tmp -rf


}

install_db(){
echo "Setting up Inprotect database"
echo "========================"


FAIL=1
while [ "$FAIL" -eq "1" ];
do

	mysql -u root -e "show databases;" >>/dev/null 2>&1
	if [ $? == 0 ]; then
		mysql -u root < console/sql/inprotect.sql
		FAIL=0
	else
		echo "Please enter mysql root password:"
		stty -echo
		read mysql_pass < /dev/tty
		stty echo
		mysql -u root -p$mysql_pass -e "show databases;" >> /dev/null
		if [ $? == 0 ]; then
			mysql -u root -p$mysql_pass < console/sql/inprotect.sql
			FAIL=0
		else
			FAIL=1
		fi
	fi
done
						 
cp console/scripts/sched.pl /usr/local/bin -rf
cp console/scripts/inprotect.cfg /usr/local/etc -rf

crontab -l | grep -F -v -f console/crontab_db | grep -v ^\# > /tmp/cron.tmp
cat console/crontab_db >> /tmp/cron.tmp
crontab /tmp/cron.tmp
rm -rf /tmp/cron.tmp


}


install_scanner(){

cp scanner/*.pl /usr/local/bin -rf
crontab -l | grep -F -v -f scanner/crontab | grep -v ^\# > /tmp/cron.tmp
cat scanner/crontab >> /tmp/cron.tmp
crontab /tmp/cron.tmp
rm /tmp/cron.tmp -rf
cp scanner/nessusd /etc/init.d -rf
echo "/usr/local/bin/inprotect_reset.pl" >> /etc/rc.d/rc.local
chkconfig --add nessusd
chkconfig --level 3 nessusd on
chkconfig --level 4 nessusd on
chkconfig --level 5 nessusd on
cp scanner/inprotect.cfg /usr/local/etc -rf
/etc/init.d/nessusd start

}


echo "Inprotect installation"
echo "======================"
echo "Please run this installation as root user"
echo "1 - Install web interface only"
echo "2 - Install database only"
echo "3 - install web & database components"
echo "4 - install scanner only"
echo "5 - install web, db & scanner"
echo "Q - Quit without installation"
echo $Xn "Make your selection [1-5] " $Xc
 read answer < /dev/tty

case $answer in 
	1)
		install_web
		;;
	2)	
		install_db
		;;
	3)
		install_web
		install_db
		;;
	4)
		install_scanner
		;;
	5)
		install_web
		install_db
		install_scanner
		;;
	Q)	;;
	*)
		echo "Wrong selection was made, please re-run the installation script"
		;;
esac

echo "Please review settings in /usr/local/etc/inprotect.cfg file"
echo "Console setup finished"

