#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=2.0.1
PREVIOUSVERSION=2.0.0


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

# Can't replace dnsmasq whilst active
/etc/rc.d/rc.dnsmasq --stop

# Add directories for urlfilter
if [ ! -e /var/ipcop/proxy/autoupdate ]; then
    mkdir -p /var/ipcop/proxy/autoupdate
    mkdir -p /var/ipcop/proxy/blacklists
    mkdir -p /home/httpd/vhost81/html/repository
    chown -R nobody:nobody /var/ipcop/proxy/autoupdate
    chown -R nobody:nobody /var/ipcop/proxy/blacklists
    chown -R nobody:nobody /home/httpd/vhost81/html/repository
    chmod 644 /home/httpd/vhost81/html/repository
fi
if [ ! -e  /var/ipcop/proxy/squidGuard.conf ]; then
    # while developing urlfilter, deliver an empty squidGuard.conf
    touch /var/ipcop/proxy/squidGuard.conf
fi
chown nobody:nobody /var/ipcop/proxy/squidGuard.conf


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

# Remove old libraries (version specific)
/bin/rm -f /usr/lib/libbind9.so.60.0.4
/bin/rm -f /usr/lib/libcrypto.so.0.9.8
/bin/rm -f /usr/lib/libdns.so.69.1.3
/bin/rm -f /usr/lib/libfreetype.so.6.7.1
/bin/rm -f /usr/lib/libisc.so.62.1.1
/bin/rm -f /usr/lib/libisccc.so.60.0.0
/bin/rm -f /usr/lib/libisccfg.so.62.0.0
/bin/rm -f /usr/lib/liblwres.so.60.0.1
/bin/rm -f /lib/libncursesw.so.5.8
/bin/rm -f /usr/lib/libpanelw.so.5.8
/bin/rm -f /usr/lib/libpci.so.3.1.8
/bin/rm -f /usr/lib/libpixman-1.so.0.20.2
/bin/rm -f /usr/lib/libpng15.so.15.4.0
/bin/rm -f /usr/lib/libssl.so.0.9.8

# Remove dead symlink
/bin/rm -f /usr/lib/libpng14.so

# Using usb-modeswitch-dispatcher compiled static with TCL
/bin/rm -f /usr/bin/jimsh
/bin/rm -f /usr/bin/tclsh

# being only prove of concept code, should not have been installed by default
/bin/rm -f /usr/libexec/ipsec/showpolicy

# 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 dnsmasq
/etc/rc.d/rc.dnsmasq --start

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

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

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

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

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

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