#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=1.9.12
PREVIOUSVERSION=1.9.11


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

if [ $MACHINE == 'i486' -a $(/bin/grep -c default /boot/extlinux.conf) == 2 ]; then
    # Remove the 2nd MENU default line from extlinux.conf
    # First change the second occurrence to 'MAKE DELETE'...
    sed -i -e '/default/{:1 n;/default/{s/default/DELETE/;:2 n;$!b2};b1}' /boot/extlinux.conf
    # ...then delete that line.
    sed -i -e '/DELETE/d' /boot/extlinux.conf
fi

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

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

# Remove old libcap, libgcrypt, libpng, readline
/bin/rm -f /lib/libcap.so.2.16
/bin/rm -f /usr/lib/libgcrypt.so.11.5.2
/bin/rm -f /usr/lib/libpng12.so.0.40.0
/bin/rm -f /lib/libhistory.so.6.0
/bin/rm -f /lib/libreadline.so.6.0

# Remove old pciutils lib (3.1.4 was forgotten in 1.9.11 update)
/bin/rm -f /usr/lib/libpci.so.3.1.4
/bin/rm -f /usr/lib/libpci.so.3.1.5

# usb_modeswitch
rm -rf /etc/udev/rules.d/80-usb_modeswitch.rules

# CPAN Text-ParseWords
rm -rf /usr/lib/perl5/5.10.1/$MACHINE-linux/auto/Text

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