#!/bin/bash
#
#####################################################################################################
#
#   				(c) Markus Hoffmann, 2005-open end
#
#####################################################################################################
#
# COPYRIGHT NOTICE                                                        
# Copyright 2005 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/3.0/ )
#
#####################################################################################################
#
# 				    Im Script muss nichts veraendert werden
#				Nothings got to be changed in this script to work
#
#####################################################################################################
#

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

/bin/echo "This is qos installing on Ipcop $IPCOPVERSION."

# check layer7 installation
echo -e "checking layer7filter.....\t\t\t\c"
if [ -e /etc/l7-protocols ]
then
	echo -e "[\033[1;32mDone\033[0m]"
else
	echo -e "[\033[1;31mFailed\033[0m]"
cat <<END

You have to install layer7-filter first.

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

Aborting installation of QoS on Ipcop $IPCOPVERSION.....

END
	exit -1
fi


if [ -x "/var/ipcop/qos/bin/uninstall" ]
then
	echo -e "previous version detected"

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

if [ -d "/var/log/opt/QoS" ]
then
	echo -e "previous version detected"
	echo -e "uninstalling previous version.....\t\t\c"
	./uninstall_qos_1.9.2 >/dev/null 2>&1
	echo -e "[\033[1;32mDone\033[0m]"
fi

# Extract files.tar.gz
echo -e "installing files.....\t\t\t\t\c"
/bin/tar -zxf files.tar.gz -C /
cp license /var/ipcop/qos/ >/dev/null 2>&1
mv uninstall /var/ipcop/qos/bin/
mkdir -p /home/httpd/html/qos
chown nobody:nobody /home/httpd/html/qos
echo -e "[\033[1;32mDone\033[0m]"

echo -e "stopping trafficshaping (if enabled).....\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;32mDone\033[0m]"

# Appending Files
echo -e "creating firewall entrie.....\t\t\t\c"
cp -arf /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;32mDone\033[0m]"

echo -e "creating webinterface entries.....\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;32mDone\033[0m]"

echo -e "creating language entries.....\t\t\t\c"
if [ "$MINORVERSION" -le "6" ]
then
	cat de.header >>/var/ipcop/langs/de.pl
	cat fr.header >>/var/ipcop/langs/fr.pl
	cat en.header >>/var/ipcop/langs/en.pl
else
	if [ -d /var/ipcop/addon-lang ]
	then
		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
	fi
fi
echo -e "[\033[1;32mDone\033[0m]"

echo -e "creating shutdown entries.....\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;32mDone\033[0m]"

echo -e "creating cronjob.....\t\t\t\t\c"
cat cron.header >>/var/spool/cron/root.orig
sleep 1
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;32mDone\033[0m]"

/var/ipcop/l7filter/l7update >/dev/null 2>/dev/null &

echo -e "creating autorun.....\t\t\t\t\c"
SAMMLERINSTALLED=`grep sammlerstart /etc/rc.d/rc.local`
if [ "x$SAMMLERINSTALLED" == "x" ]
then
	cat local1.header >>/etc/rc.d/rc.local
fi
echo -e "[\033[1;32mDone\033[0m]"

while [ "$AUTO" != "y" -a "$AUTO" != "n" ]
do
echo -e "Enable automatic versioncheck for this mod ? [y/n] \c"
read AUTO
done

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
else
cat <<END >>/home/httpd/cgi-bin/qos.cgi
sub checkversion {
	#Automatic Updates is disabled
	return "0","0";
	}
END
fi

# Restore Backup
if [ -e "/tmp/backup.tgz" ]
then
	echo -e "restoring backup.....\t\t\t\t\c"	
	tar xfz /tmp/backup.tgz -C /
	rm -f /tmp/backup.tgz
	rm -f /tmp/classes.tmp >/dev/null 2>&1
	if [ $INSTALLED -lt 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;32mDone\033[0m]"
	if [ -e "/var/ipcop/qos/settings/enable" ]
	then
		echo -e "Starting QoS.....\t\t\t\t\c"
		/var/ipcop/qos/bin/qos.sh >/dev/null 2>&1
		echo -e "[\033[1;32mDone\033[0m]"
	fi
fi

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

# Cleaning up
echo -e "cleaning up.....\t\t\t\t\c"
INSTDIR=`pwd`
rm -f /var/lock/subsys/qos.sh
echo -e "[\033[1;32mDone\033[0m]"
/bin/echo -e "Installation finished."
/bin/echo -e "\n\nto uninstall qos just run '/var/ipcop/qos/bin/uninstall'\n"
rm -rf $INSTDIR

exit 0

