#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=2.0.3
PREVIOUSVERSION=2.0.2


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

#  New directory for urlfilter blacklist updates
if [ ! -e /var/ipcop/proxy/blacklistupdate ]; then
    mkdir -p /var/ipcop/proxy/blacklistupdate
    chown -R nobody:nobody /var/ipcop/proxy/blacklistupdate
fi

#  New directory for squidGuard logs
if [ ! -e /var/log/squidGuard ]; then
    mkdir -p /var/log/squidGuard
    chown squid.root /var/log/squidGuard
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-6 --keep 2.6.32-5 --remove 2.6.32-4

# Modify or remove line below, depending on update
# Create the new initramfs
/sbin/mkinitramfs --with-kernel=2.6.32-6 --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 /lib/libz.so.1.2.5
/bin/rm -f /usr/lib/libaprutil-1.so.0.3.12
/bin/rm -f /usr/lib/libfreetype.so.6.7.2
/bin/rm -f /usr/lib/libgmp.so.10.0.1
/bin/rm -f /usr/lib/libgmp.so.10.0.3
/bin/rm -f /usr/lib/liblber-2.4.so.2.5.6
/bin/rm -f /usr/lib/liblber.so
/bin/rm -f /usr/lib/libldap-2.4.so.2.5.6
/bin/rm -f /usr/lib/libldap.so
/bin/rm -f /usr/lib/libnetfilter_conntrack.so.3.1.3
/bin/rm -f /usr/lib/libnl-genl.so.2
/bin/rm -f /usr/lib/libnl-genl.so.2.0.0
/bin/rm -f /usr/lib/libnl.so.2
/bin/rm -f /usr/lib/libnl.so.2.0.0
/bin/rm -f /usr/lib/libpcap.so.1.2.0
/bin/rm -f /usr/lib/libpng15.so.15.5.0
/bin/rm -f /usr/lib/librrd.so.4.1.4
/bin/rm -f /usr/lib/librrd_th.so.4.1.4
/bin/rm -f /usr/lib/libsqlite3.so

# Remove .so links
/bin/rm -f /lib/libcap.so
/bin/rm -f /lib/libip4tc.so
/bin/rm -f /lib/libip6tc.so
/bin/rm -f /lib/libiptc.so
/bin/rm -f /usr/lib/libapr-1.so
/bin/rm -f /usr/lib/libaprutil-1.so
/bin/rm -f /usr/lib/libfreetype.so
/bin/rm -f /usr/lib/libgmp.so
/bin/rm -f /usr/lib/liblzo2.so
/bin/rm -f /usr/lib/libnetfilter_conntrack.so
/bin/rm -f /usr/lib/libnl.so
/bin/rm -f /usr/lib/libnl-3.so
/bin/rm -f /usr/lib/libnl-genl-3.so
/bin/rm -f /usr/lib/libnl-genl.so
/bin/rm -f /usr/lib/libparted.so
/bin/rm -f /usr/lib/libpcap.so
/bin/rm -f /usr/lib/libpci.so.3
/bin/rm -f /usr/lib/libpng.so
/bin/rm -f /usr/lib/libpng15.so
/bin/rm -f /usr/lib/librrd.so
/bin/rm -f /usr/lib/librrd_th.so
/bin/rm -f /usr/lib/libz.so

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

# redirect background image converted to png, remove obsolete jpg
/bin/rm -f /home/httpd/html/images/redirect-background.jpg

# makesquidconf renamed to .pl
/bin/rm -f /usr/local/bin/makesquidconf

#####
#
# 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 ntp daemon
if [ -e /var/run/ntpd.pid ]; then
    /usr/local/bin/restartntpd
fi

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

# restart squid (helper checks whether squid is enabled)
/usr/local/bin/restartsquid

# Readd serial console, removed by 2.0.1 update
TMP=`grep "console=" /proc/cmdline`
if [ "x$TMP" != "x" ]; then
    TMP=`grep "^7" /etc/inittab`
    if [ "x$TMP" == "x" ]; then
        # Pull needed information from /proc/cmdline
        CONSOLE=`cat /proc/cmdline | sed 's/.*console=\([^ ]*\).*/\1/'`
        TTY=`echo $CONSOLE | cut -d ',' -f 1`
        BAUDRATE=`echo $CONSOLE | sed 's/.*,\([0-9]*\).*/\1/'`
        echo "7:2345:respawn:/sbin/agetty -I '\033(K' $TTY $BAUDRATE vt102" >> /etc/inittab
    fi
fi

# Add filtersettings file for urlfilter
if [ ! -e /var/ipcop/proxy/filtersettings ]; then
    touch /var/ipcop/proxy/filtersettings
    chown nobody:nobody /var/ipcop/proxy/filtersettings
fi

# Fix directory permissions after patch extraction
chown -R nobody:nobody /var/ipcop/proxy/blacklistupdate

# fix permissions of urlfilter repository
chmod 755  /home/httpd/vhost81/html/repository

# Fix owner of addons/lang
chown root:root /var/ipcop/addons/lang

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