#!/bin/bash
#

#modification to change the year automatically
setyear=2008
thisyear=`date +%Y`
if [ "x$setyear" != "x$thisyear" ]
then
	sed -i -e "s/$setyear/$thisyear/g" $0
	echo -e "\nautomatically updated download-link, please start l7update again!\n"
	exit 0
fi
# end modification

if [ "x$1" != "x" -a "x$1" != "xstartup" ]
then 
	MIRROR=$1
else
	MIRROR=superb-east
fi
cd /var/ipcop/l7filter
/var/ipcop/l7filter/wget -O "/var/ipcop/l7filter/index.html" -q "http://sourceforge.net/project/showfiles.php?group_id=80085&package_id=81756"
currentY=$(cat localversion | cut -f1 -d"-")
currentM=$(cat localversion | cut -f2 -d"-")
currentD=$(cat localversion | cut -f3 -d"-")
currentV="$currentD$currentM$currentY"
lastY=$(cat "/var/ipcop/l7filter/index.html" |grep -B 1 proto 2>/dev/null|grep -m 1 "2008-" 2>/dev/null|cut -d- -f4 2>/dev/null)
lastM=$(cat "/var/ipcop/l7filter/index.html" |grep -B 1 proto 2>/dev/null|grep -m 1 "2008-" 2>/dev/null|cut -d- -f5 2>/dev/null)
lastD=$(cat "/var/ipcop/l7filter/index.html" |grep -B 1 proto 2>/dev/null|grep -m 1 "2008-" 2>/dev/null|cut -d- -f6|cut -d. -f1 2>/dev/null)
lastV="$lastD$lastM$lastY"
lastupdate="$lastY-$lastM-$lastD"
#echo $lastupdate
[ "$lastV" == "" ] && echo -e "Unable to detect online Version, exiting..........." && exit 1
[ "$currentV" == "" ] && echo -e "Unable to detect installed Version, exiting..........." && exit 1
/bin/rm index.html
if [ "$lastV" != "$currentV" ]
then
    CHECK=`/var/ipcop/l7filter/wget --spider http://$MIRROR.dl.sourceforge.net/sourceforge/l7-filter/l7-protocols-"$lastupdate".tar.gz 2>&1`
    #echo $CHECK
    SIZE=`/bin/echo $CHECK| cut -dL -f2| awk {'print $2'}|cut -d, -f1`
    NOTFOUND=`/bin/echo $SIZE|grep http`
    NOTFOUND="x${NOTFOUND}"
    FAILED=`/bin/echo $CHECK| grep failed| cut -d: -f6`
    FAILED="x${FAILED}"
    #echo $SIZE
	#echo;echo;echo
    #echo $NOTFOUND
	#echo;echo;echo
    #echo $FAILED
    if [ "$FAILED" != "x" ]
    then
	 if [ "x$1" != "xstartup" ]
	 then
        	/bin/echo "Error while downloading file."
	        exit 0
	 else
		/bin/echo -e "[\033[1;31m Failed \033[0m]"
		exit 0
	 fi
    fi	
    if [ "$NOTFOUND" != "x" ]
    then
	 if [ "x$1" != "xstartup" ]
	 then
	       /bin/echo "File is not on the mirror right now."
       	exit 0
	 else
		/bin/echo -e "[\033[1;31m Failed \033[0m]"
		exit 0
	 fi
    fi	
    if [ $SIZE -le 2 ]
    then
	 if [ "x$1" != "xstartup" ]
	 then
	       /bin/echo "File is not on the mirror right now."
       	exit 0
	 else
		/bin/echo -e "[\033[1;31m Failed \033[0m]"
		exit 0
	 fi
    fi
    /var/ipcop/l7filter/wget -q http://$MIRROR.dl.sourceforge.net/sourceforge/l7-filter/l7-protocols-"$lastupdate".tar.gz
    /bin/echo $lastupdate > localversion
    /bin/tar xfz l7-protocols-"$lastupdate".tar.gz
    /bin/rm -rf /etc/l7-protocols/
    /bin/mv l7-protocols-"$lastupdate" /etc/l7-protocols
    /bin/chown -R root.root /etc/l7-protocols
    /bin/rm l7-protocols-"$lastupdate".tar.gz
    /bin/echo "$(date) : Layer7 protocols updated successefully" >>/var/log/messages
    if [ "x$1" == "xstartup" ]
    then
		echo -e "[\033[1;32m Done \033[0m]"
    fi
else
    /bin/echo "$(date) : Your Layer7 protocols are up to date" >>/var/log/messages
    if [ "x$1" == "xstartup" ]
    then
		echo -e "[\033[1;32m Done \033[0m]"
    fi
fi
