#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=1.9.13
PREVIOUSVERSION=1.9.12


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

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

# Can't replace while 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
    /etc/rc.d/rc.dnsmasq --start
    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 2.6.27-2 --keep 2.6.27-1 --remove 2.6.27-0

# Modify or remove line below, depending on update
# Create the new initramfs
# /sbin/mkinitramfs --with-kernel=2.6.27-2 --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 /usr/lib/libapr-1.so.0.3.9
/bin/rm -f /usr/lib/libgmp.so.3
/bin/rm -f /usr/lib/libgmp.so.3.5.0
/bin/rm -f /usr/lib/liblber-2.4.so.2.4.2
/bin/rm -f /usr/lib/libldap-2.4.so.2.4.2
/bin/rm -f /usr/lib/libcrypto.so.0.9.7
/bin/rm -f /usr/lib/libssl.so.0.9.7

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

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

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

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

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

# Restart fcrond to activate new version
/usr/bin/killall fcron
# We need a small pause to give fcron time to stop
sleep 1
if [ -e /etc/FLASH ]; then
    /usr/sbin/fcron -s 86400
else
    /usr/sbin/fcron
fi

# Use modprobe instead of insmod/rmmod
/bin/rm -f /sbin/insmod /sbin/rmmod

# fcdslx modules may miss in modules.dep
/sbin/depmod -ae -F /boot/System.map-2.6.27-3 2.6.27-3

# Use new apache (restart pauses for 10 seconds, should be enough to complete upgrade)
/usr/local/bin/restarthttpd restart &

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

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