#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=2.1.6
PREVIOUSVERSION=2.1.5


msg() {
    /usr/bin/logger -t installpackage "$*"
    /bin/echo "$*"
}


# Verify architecture
MACHINE=`/usr/bin/perl -e "require '/usr/lib/ipcop/general-functions.pl';print \\$General::machine;"`
if [ "$MACHINE" != `/bin/cat ./ARCH` ]; then
    msg "Update for wrong architecture: `/bin/cat ./ARCH`! We are: $MACHINE. Aborting installation."
    exit 1
fi

# Verify version, note we also accept replaying an upgrade
CURRENTVERSION=`/usr/bin/perl -e "require '/usr/lib/ipcop/general-functions.pl';print \\$General::version;"`
if [ "$CURRENTVERSION" != "$PREVIOUSVERSION" -a "$CURRENTVERSION" != "$UPGRADEVERSION" ]; then
    msg "IPCop v$PREVIOUSVERSION or v$UPGRADEVERSION not running. Aborting installation."
    exit 1
fi

# Stop fcron, to keep tasks from running during update
FCRONPID=`cat /var/run/fcron.pid`
/bin/kill $FCRONPID
COUNT=30

while [ -e /var/run/fcron.pid -a ${COUNT} -ge 0 ]
do
    sleep 1
    COUNT=$(( ${COUNT} - 1 ))
done

if [ -e /var/run/fcron.pid ]; then
    msg "kill fcron, still running after 30 seconds"
    /bin/kill -KILL $FCRONPID
fi

#####
#
# Add version specific handling *before* unpacking the patch here.
# For example stopping dnsmasq(required before update), squid, etc...
#
#####

# Can't replace dnsmasq whilst active
/etc/rc.d/rc.dnsmasq --stop

#####
#
# End of version specific handling. Continue with universal stuff.
#
#####

FAILURE=0
/bin/tar -zxpf patch.tar.gz -C /
FAILURE=$?
if [ $FAILURE -ne 0 ]; then
    msg "Error extracting patch.tar.gz $1, need more free space on disk. Aborting."
    exit 4 # ERRORTAR
fi

# Modify or remove line below, depending on update
# Update bootloader config
/usr/local/sbin/updatekernel.pl --add 3.4-3 --keep 3.4-2 --remove 3.4-1

# Modify or remove line below, depending on update
# Create the new initramfs
/sbin/mkinitramfs --with-kernel=3.4-3 --with-firmware --many-modules --with-list=/etc/modules.initramfs

# Adjust the changed config files
# /usr/local/bin/upgrade.sh

# Remove old libraries (version specific)
# /bin/rm -f .....
/bin/rm -f /usr/lib/libgcrypt.so.20.0.1
/bin/rm -f /usr/lib/libgpg-error.so.0
/bin/rm -f /usr/lib/libgpg-error.so.0.10.0
/bin/rm -f /usr/lib/libnl-3.so.200
/bin/rm -f /usr/lib/libnl-3.so.200.19.0
/bin/rm -f /usr/lib/libnl-genl-3.so.200
/bin/rm -f /usr/lib/libnl-genl-3.so.200.19.0

/bin/rm -f /usr/lib/perl5/site_perl/5.14.2/i486-linux/Net/DNS/RR/EID.pm
/bin/rm -f /usr/lib/perl5/site_perl/5.14.2/i486-linux/Net/DNS/RR/NIMLOC.pm
/bin/rm -f /usr/lib/perl5/site_perl/5.14.2/i486-linux/Net/DNS/RR/NSAP.pm

# ppp
/bin/rm -rf /usr/lib/pppd/2.4.6

# squid helpers that have disappeared over time
/bin/rm -f /usr/lib/squid/digest_edirectory_auth
/bin/rm -f /usr/lib/squid/diskd-daemon
/bin/rm -f /usr/lib/squid/logfile-daemon
/bin/rm -f /usr/lib/squid/ntlm_auth

# tzdata changes
/bin/rm -f /usr/share/zoneinfo/posix/Asia/Riyadh87
/bin/rm -f /usr/share/zoneinfo/posix/Asia/Riyadh88
/bin/rm -f /usr/share/zoneinfo/posix/Asia/Riyadh89

# vlan
/bin/rm -f /sbin/vconfig

# renamed
/bin/rm -f /usr/local/sbin/installflash.sh

# For new shared libs. May not always be required, but makes sure we do not forget
/sbin/ldconfig

#####
#
# Add version specific handling *after* unpacking the patch here.
# For example restarting apache, squid, etc...
#
#####

# Restart rsyslogd to activate new version
/usr/local/bin/restartsyslogd

# Restart dnsmasq
/etc/rc.d/rc.dnsmasq --start

# Restart IPsec
if [ -e /var/run/pluto/pluto.pid ]; then
    /usr/local/bin/ipsecctrl --start
fi

# Restart OpenVPN server
if [ -e /var/run/openvpn.pid ]; then
    /usr/local/bin/openvpnctrl --restart
fi

# restart squid (helper checks whether squid is enabled)
/usr/local/bin/restartsquid

#####
#
# End of version specific handling. Continue with universal stuff.
#
#####

# Restart fcron
if [ -e /etc/FLASH ]; then
    /usr/sbin/fcron -s 86400
else
    /usr/sbin/fcron
fi

# Patch general-functions.pl
/bin/sed -i -e "s+^\(\$General::version\s*=\s*\).*+\1'$UPGRADEVERSION';+" /usr/lib/ipcop/general-functions.pl
# Patch /etc/issue
/bin/sed -i -e "s+$PREVIOUSVERSION+$UPGRADEVERSION+" /etc/issue

# Update menu
/usr/local/bin/updatemenu.pl

msg "$UPGRADEVERSION update installed."
