#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=2.2.0
PREVIOUSVERSION=2.1.9


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=120

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.10-1 --keep 3.4-3 --remove 3.4-2

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

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

# Remove renamed program
/bin/rm -f /usr/local/bin/openvpnctrl
/bin/rm -f /usr/lib/openvpn/plugins/openvpn-plugin-down-root.la

# Remove old libraries (version specific)
/bin/rm -f /usr/lib/libbind9.so.140.0.7
/bin/rm -f /usr/lib/libdns.so.146
/bin/rm -f /usr/lib/libdns.so.146.1.0
/bin/rm -f /usr/lib/libisc.so.142
/bin/rm -f /usr/lib/libisc.so.142.4.0
/bin/rm -f /usr/lib/libisccc.so.140.0.1
/bin/rm -f /usr/lib/libisccfg.so.140.2.3
/bin/rm -f /usr/lib/liblwres.so.141.0.0
/bin/rm -f /usr/lib/libglib-2.0.so.0.4000.0
/bin/rm -f /usr/lib/libgmodule-2.0.so.0.4000.0
/bin/rm -f /usr/lib/libgobject-2.0.so.0.4000.0
/bin/rm -f /usr/lib/libgthread-2.0.so.0.4000.0
/bin/rm -f /usr/lib/libgmp.so.10.0.5
/bin/rm -f /usr/lib/libffi.so.6.0.1
/bin/rm -f /usr/lib/libgcrypt.so.20.0.2
/bin/rm -f /usr/lib/libgpg-error.so.0.12.2
/bin/rm -f /usr/lib/libnl-3.so.200.20.0
/bin/rm -f /usr/lib/libnl-genl-3.so.200.20.0
/bin/rm -f /usr/lib/libpcap.so.1.6.2
/bin/rm -f /usr/lib/libpng16.so.16.16.0
/bin/rm -f /usr/lib/libltdl.so
/bin/rm -f /usr/lib/libltdl.so.7.3.0
/bin/rm -f /usr/lib/liblzma.so.5
/bin/rm -f /usr/lib/liblzma.so.5.0.5
/bin/rm -f /usr/lib/libpci.so.3.3.0
/bin/rm -f /usr/lib/librrd.so.4.2.2
/bin/rm -f /usr/lib/librrd_th.so.4.2.2

# Remove old Perl 5.14.2
/bin/rm -f /usr/bin/perl5.14.2
/bin/rm -rf /usr/lib/perl5/5.14.2
/bin/rm -rf /usr/lib/perl5/site_perl/5.14.2

# 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

# Use new sshd
/usr/local/bin/restartssh

# Restart ntp daemon
if [ -e /var/run/ntpd.pid ]; then
    /usr/local/bin/restartntpd
fi

# Restart OpenVPN server
/usr/local/bin/restartopenvpn --restart

# 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."
