#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=2.0.2
PREVIOUSVERSION=2.0.1


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 dnsmasq(required before update), squid, etc...
#
#####

# Add directory for redirector config files
if [ ! -e /var/ipcop/proxy/redirector ]; then
    mkdir -p /var/ipcop/proxy/redirector
    chown -R nobody:nobody /var/ipcop/proxy/redirector
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.32-5 --keep 2.6.32-4 --remove 2.6.32-3

# Modify or remove line below, depending on update
# Create the new initramfs
/sbin/mkinitramfs --with-kernel=2.6.32-5 --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/perl5/site_perl/5.10.1/i486-linux/Net/DNS/RR/X25.pm

# Update adds chpasswd.cgi on port 81
/bin/rm -f /home/httpd/cgi-bin/chpasswd.cgi
# port 81 images directory no longer needed
/bin/rm -rf /home/httpd/vhost81/html/images

# 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

# Use new apache config (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."
