#!/bin/bash
#
#####################################################################################################
#
#   				(c) Markus Hoffmann, 2005-open end
#
#####################################################################################################
#
# COPYRIGHT NOTICE                                                        
# Copyright 2005-2007 Markus Hoffmann
# All Rights Reserved.                     
# EMail: ipcop@mh-lantech.de
# URL  : http://www.mhaddons.tk
#
# License: CreativeCommons, some right restricted ( http://creativecommons.org/licenses/by-nd/2.5/ )
#
#####################################################################################################
#
# 				    Im Script muss nichts veraendert werden
#				Nothings got to be changed in this script to work
#
#####################################################################################################
#

IPCOPVERSION=`grep version /var/ipcop/general-functions.pl | cut -d= -f2 | tr -d [:blank:]\;\'|grep -v Data`
MAJORVERSION=`echo $IPCOPVERSION | cut -d. -f1,2`
MINORVERSION=`echo $IPCOPVERSION | cut -d. -f3`

/bin/echo -e "\nThis is \033[1;34mQoS_NG\033[0m installing on Ipcop $IPCOPVERSION.\n"

# check layer7 installation
echo -e "checking layer7filter.....\t\t\t\t\t\c"
if [ -e /lib/modules/2.4.36/kernel/net/ipv4/netfilter/ipt_layer7.o.gz ]
then
	echo -e "[\033[1;32m Done \033[0m]"
else
	echo -e "[\033[1;31m Failed \033[0m]"
cat <<END

You have to install layer7-filter first.

Download layer7-filter for Ipcop 1.4.21 and higher here:
http://de.embcop.org/?page_id=40

Aborting installation of QoS_NG.....

END
	exit -1
fi

if [ -x "/var/ipcop/qos/bin/uninstall" ]
then
	echo -e -n "\033[7s"
	echo -e -n "previous version detected"
	echo -e -n "\033[8u"
	sleep .1
	echo -e -n "\033[1;31mprevious version detected"
	echo -e -n "\033[8u"
	sleep .1
	echo -e -n "\033[0mprevious version detected"
	echo -e -n "\033[8u"
	sleep .1
	echo -e -n "\033[1;31mprevious version detected"
	echo -e -n "\033[8u"
	sleep .1
	echo -e -n "\033[0mprevious version detected"
	echo -e -n "\033[8u"
	sleep .1
	echo -e -n "\033[1;31mprevious version detected\033[0m"
	sleep 2
	echo -e -n "\033[8u"	
	ALLOWBACKUP=`grep "my \\$MODNAME" "/home/httpd/cgi-bin/qos.cgi"|cut -d'"' -f2`
	if [ "$ALLOWBACKUP" == "QoS_NG" ]
	then
		echo -e -n "\033[7s"
		while [ "$BACKUP" != "y" -a "$BACKUP" != "n" ]
		do
		echo -e -n "\033[8u"
		echo -e -n "                                                                                               "
		echo -e -n "\033[8u"
		echo -e "Create backup of existing settings ? [y/n] \t\t\t\c"
		read -n 1 BACKUP
		done
		if [ $BACKUP == "y" ]
		then
			INSTALLED=`grep "my \\$MODVERSION" "/home/httpd/cgi-bin/qos.cgi"|cut -d'"' -f2|cut -d"." -f2`
			INSTALLED1=`grep "my \\$MODVERSION" "/home/httpd/cgi-bin/qos.cgi"|cut -d'"' -f2|cut -d"." -f1`
			tar cfz /tmp/backup.tgz /var/ipcop/qos/settings >/dev/null 2>&1
		fi
	fi
	echo -e -n "\033[8u"
	echo -e -n "                                                                       "
	echo -e -n "\033[8u"
	echo -e "uninstalling previous version.....\t\t\t\t\c"
	/var/ipcop/qos/bin/uninstall >/dev/null 2>&1
	echo -e "[\033[1;32m Done \033[0m]"
fi

# Extract files.tar.gz
echo -e "installing files.....\t\t\t\t\t\t\c"
/bin/tar -zxf files.tar.gz -C /
DIR=`pwd`
cp $DIR/license /var/ipcop/qos/ >/dev/null 2>&1
mv $DIR/uninstall /var/ipcop/qos/bin/
echo -e "[\033[1;32m Done \033[0m]"

echo -e "stopping trafficshaping (if enabled).....\t\t\t\c"
sed -e s/on/off/ /var/ipcop/shaping/config>config
cp config /var/ipcop/shaping/config
rm config
grep -v "ENABLE" /var/ipcop/shaping/settings > settings
cp settings /var/ipcop/shaping/settings
rm settings
echo -e "ENABLE=off">>/var/ipcop/shaping/settings
LINE=$(( $( TMP=`grep -n "shaping.cgi" /var/ipcop/header.pl|cut -d":" -f1` && echo $TMP|cut -d" " -f1  ) -0))
/bin/sed -i "$LINE s/\[/\#\[/" /var/ipcop/header.pl
/usr/local/bin/restartshaping >/dev/null 2>&1
echo -e "[\033[1;32m Done \033[0m]"

# Appending Files

QOSFWL=`grep qos /etc/rc.d/rc.firewall.local`
if [ "x$QOSFWL" == "x" ]
then
	echo -e "creating firewall entrie.....\t\t\t\t\t\c"
	cp -af /etc/rc.d/rc.firewall.local /etc/rc.d/rc.firewall.local.before_qos
	LINE=$(( $( TMP=`grep -n ";;" /etc/rc.d/rc.firewall.local|cut -d":" -f1` && echo $TMP|cut -d" " -f1  ) -1))
	/bin/sed -i "$LINE a\#Added for qos1" /etc/rc.d/rc.firewall.local
	/bin/cat /etc/rc.d/rc.firewall.local | sed -e '/Added.for.qos1/ r local.header'>rc.firewall.local
	/bin/cp rc.firewall.local /etc/rc.d/rc.firewall.local
	/bin/rm rc.firewall.local
	LINE1=$(( $( TMP=`grep -m 2 -n ";;" /etc/rc.d/rc.firewall.local|cut -d":" -f1` && echo $TMP|cut -d" " -f2  ) -1))
	/bin/sed -i "$LINE1 a\#Added for qos2" /etc/rc.d/rc.firewall.local
	/bin/cat /etc/rc.d/rc.firewall.local | sed -e '/Added.for.qos2/ r local2.header'>rc.firewall.local
	/bin/cp rc.firewall.local /etc/rc.d/rc.firewall.local
	/bin/rm rc.firewall.local
	echo -e "[\033[1;32m Done \033[0m]"
fi

echo -e "creating webinterface entries.....\t\t\t\t\c"
/bin/cat /var/ipcop/header.pl | sed -e '/time.cgi/ r header.header'>header.pl
/bin/cp header.pl /var/ipcop/header.pl
/bin/rm header.pl
echo -e "[\033[1;32m Done \033[0m]"

echo -e "creating language entries.....\t\t\t\t\t\c"
grep -v "#" de.header>/var/ipcop/addon-lang/qos.de.pl
grep -v "#" en.header>/var/ipcop/addon-lang/qos.en.pl
grep -v "#" fr.header>/var/ipcop/addon-lang/qos.fr.pl
perl -e "require '/var/ipcop/lang.pl'; &Lang::BuildCacheLang" >/dev/null 2>&1
echo -e "[\033[1;32m Done \033[0m]"

echo -e "creating autorun.....\t\t\t\t\t\t\c"
cat local1.header >>/etc/rc.d/rc.local
echo -e "[\033[1;32m Done \033[0m]"

echo -e "creating shutdown entries.....\t\t\t\t\t\c"
LINE2=$(( $( TMP=`grep swap -wn /etc/rc.d/rc.halt|cut -d":" -f1` && echo $TMP|cut -d" " -f2  ) -1))
/bin/sed -i "$LINE2 a\#Added for qos" /etc/rc.d/rc.halt
/bin/cat /etc/rc.d/rc.halt | sed -e '/Added.for.qos/ r halt.header'>/tmp/rc.halt
/bin/cp /tmp/rc.halt /etc/rc.d/rc.halt
/bin/rm /tmp/rc.halt
echo -e "[\033[1;32m Done \033[0m]"

echo -e "creating cronjob.....\t\t\t\t\t\t\c"
L7INSTALLED=`grep l7filter /var/spool/cron/root.orig`
if [ "x$L7INSTALLED" == "x" ]
then
	echo -e "# Added for l7filter BEGIN" >> /var/spool/cron/root.orig
	echo -e "01 2 * * *  /var/ipcop/l7filter/l7update >/dev/null 2>&1" >> /var/spool/cron/root.orig
	echo -e "# Added for l7filter END" >> /var/spool/cron/root.orig
fi
fcrontab -z >/dev/null 2>&1
echo -e "[\033[1;32m Done \033[0m]"

echo -e -n "\033[7s"
while [ "$AUTO" != "y" -a "$AUTO" != "n" ]
do
echo -e -n "\033[8u"
echo -e "Enable automatic versioncheck for this mod ? [y/n] \t\t\c"
read  -n 1 AUTO
done

echo -e -n "\033[8u                                                                                   "
echo -e -n "\033[8u"
echo -e -n "automatic versioncheck for this mod.....\t\t\t"
if [ $AUTO == "y" ]
then
	cat <<END >>/home/httpd/cgi-bin/qos.cgi 
sub checkversion {
	#Automatic Updates is enabled
	my \$localcheckname=\$_[0];
	my \$localcheckversion=\$_[1];
	my @LOCALCHECK=&checkversionenabled(\$localcheckname,\$localcheckversion);
	return \$LOCALCHECK[0], \$LOCALCHECK[1];
	}
END
	echo -e "[\033[1;32m Enabled \033[0m]"
else
	cat <<END >>/home/httpd/cgi-bin/qos.cgi
sub checkversion {
	#Automatic Updates is disabled
	return "0","0";
	}
END
	echo -e "[\033[1;31m Disabled \033[0m]"
fi

if [ -e "/var/ipcop/red/active" ]
then
	echo -e -n "\033[7s"
	while [ "$UPDATE" != "y" -a "$UPDATE" != "n" ]
	do
	echo -e -n "\033[8u"
	echo -e "Update layer7-patterns right now ? [y/n] \t\t\t\c"
	read -n 1 UPDATE
	done

	if [ $UPDATE == "y" ]
	then
		echo -e -n "\033[8u                                                                       "
		echo -e -n "\033[8u"
		echo -e "updating layer7-patterns.....\t\t\t\t\t\c"
		/var/ipcop/l7filter/l7update startup
	fi
fi

# Restore Backup
if [ -e "/tmp/backup.tgz" ]
then
	echo -e "restoring backup.....\t\t\t\t\t\t\c"	
	tar xfz /tmp/backup.tgz -C /
	mv /tmp/backup.tgz /var/ipcop/qos/settings/
	rm -f /tmp/classes.tmp >/dev/null 2>&1
	if [ $INSTALLED -lt 2 -a $INSTALLED1 -eq 2 ]
	then
		CLASSES=`cat /var/ipcop/qos/settings/tc/classes`
		for j in $CLASSES
		do
			ROOTHANDLE=`echo $j|cut -d";" -f1`	
			CLASS=`echo $j|cut -d";" -f2`
			CLASSHANDLE=`echo $j|cut -d";" -f3`
			CLASSPRIO=`echo $j|cut -d";" -f4`
			CLASSMIN=`echo $j|cut -d";" -f5`
			UMAX=`echo $j|cut -d";" -f6`
			MAXDELAY=`echo $j|cut -d";" -f7`
			MAXRATE=`echo $j|cut -d";" -f8`
			echo -e "$ROOTHANDLE;$CLASS;$CLASSHANDLE;$CLASSPRIO;0;$MAXDELAY;$MAXRATE" >>/tmp/classes.tmp
		done
		rm -f /var/ipcop/qos/settings/tc/classes  >/dev/null 2>&1
		mv /tmp/classes.tmp /var/ipcop/qos/settings/tc/classes
		chown nobody:nobody /var/ipcop/qos/settings/tc/classes
	fi
	echo -e "[\033[1;32m Done \033[0m]"
	echo -e "converting rules.....\t\t\t\t\t\t\c"
	./update_rules.pl >/tmp/qos_ng_ruleupdate.log 2>&1
	echo -e "[\033[1;32m Done \033[0m]"
	if [ -e "/var/ipcop/qos/settings/enable" ]
	then
		echo -e "Starting QoS.....\t\t\t\t\t\t\c"
		/var/ipcop/qos/bin/qos.sh >/dev/null 2>&1
		echo -e "[\033[1;32m Done \033[0m]"
	fi
fi

touch /var/run/need-depmod-`uname -r`

# Cleaning up
echo -e "cleaning up.....\t\t\t\t\t\t\c"
INSTDIR=`pwd`
if [ $INSTDIR != "/" -a $INSTDIR != "~" -a $INSTDIR != "/root" ]
then
	rm -rf $INSTDIR
fi
echo -e "[\033[1;32m Done \033[0m]"
/bin/echo -e "Installation finished."
/bin/echo -e "\n\nto uninstall qos just run '/var/ipcop/qos/bin/uninstall'\n"

exit 0

