#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=1.9.11
PREVIOUSVERSION=1.9.10


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

# Create missing directories for en_US
[ ! -d /usr/share/locale/en_US/LC_MESSAGES ] && /bin/mkdir -p /usr/share/locale/en_US/LC_MESSAGES

# Stop OpenVPN server if running
[ -e /var/run/openvpn.pid ] && /usr/local/bin/openvpnctrl --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

# Update bootloader config
/usr/local/sbin/updatekernel.pl --add 2.6.27-3 --keep 2.6.27-2 --remove 2.6.27-1

# Create the new initramfs
/sbin/mkinitramfs --with-kernel=2.6.27-3 --with-firmware --many-modules --with-list=/etc/modules.initramfs

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

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

# Remove update script (is now upgrade.sh)
/bin/rm -f /usr/local/bin/upgrade
# Remove ipcoprscfg (is now ipcoprestore)
/bin/rm -f /usr/local/bin/ipcoprscfg
# Remove blacklogo (is now in httpd/vhost81)
/bin/rm -f /home/httpd/html/images/blacklogo.png

# Remove SUID flag from rc.red and change owner
/bin/chmod 755 /etc/rc.d/rc.red
/bin/chown root.root /etc/rc.d/rc.red
# Remove suidperl
/bin/rm -f /usr/bin/sperl5.10.1
/bin/rm -f /usr/bin/suidperl

# Remove old xtables lib
/bin/rm -f /lib/libxtables.so.2*

# We are using /etc/modprobe.d directory now
/bin/rm -f /etc/modules.conf

# Repair httpd.conf for flash installations
if [ -e /etc/FLASH ]; then
    sed -i -e s/"^\tOptions ExecCGI.*"/"\tOptions ExecCGI FollowSymlinks"/1 /etc/httpd/conf/httpd.conf
fi
# Restart httpd to activate 81 vhost
/usr/local/bin/restarthttpd

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

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

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

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

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