#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=1.9.18
PREVIOUSVERSION=1.9.17


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

# New email feature
if [  ! -e /var/ipcop/email ]; then
    mkdir -p /var/ipcop/email
    mkdir -p /var/ipcop/email/templates
    touch /var/ipcop/email/settings
    chown -R nobody:nobody /var/ipcop/email
fi

# monitor traffic
if [ $(/bin/grep -c monitorTraffic.pl /var/spool/cron/root.orig) == 0 ]; then
    echo "" >> /var/spool/cron/root.orig
    echo "# Run traffic monitoring (every 5 minutes)" >> /var/spool/cron/root.orig
    echo "*/5 * * * *	/usr/local/bin/monitorTraffic.pl > /dev/null" >> /var/spool/cron/root.orig
    /usr/bin/fcrontab  -z
fi

# move usb_modeswitch.d directory
if [ -e /etc/usb_modeswitch.d ]; then
    mv -f /etc/usb_modeswitch.d /usr/share/usb_modeswitch
fi

# Remove old squid error pages
/bin/rm -fr /usr/lib/squid/errors.ipcop
/bin/rm -fr /usr/lib/squid/errors

# nothing installed use libmpfr
/bin/rm -f /usr/lib/libmpfr.so*

#####
#
# 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/sbin/lsusb # moved to bin
/bin/rm -f /usr/lib/libaprutil-1.so.0.3.9
/bin/rm -f /usr/lib/libbind9.so.60.0.2
/bin/rm -f /usr/lib/libdns.so.66.0.2
/bin/rm -f /usr/lib/libisc.so.60.1.5
/bin/rm -f /usr/lib/libisccfg.so.60.0.4
/bin/rm -f /usr/lib/liblwres.so.60.0.0
/bin/rm -f /usr/lib/libfreetype.so.6.5.1
/bin/rm -f /usr/lib/libcairo.so.2.10800.10
/bin/rm -f /usr/lib/libpango-1.0.so.0.2800.1
/bin/rm -f /usr/lib/libpangocairo-1.0.so.0.2800.1
/bin/rm -f /usr/lib/libpangoft2-1.0.so.0.2800.1
/bin/rm -f /usr/lib/libpixman-1.so.0.18.2
/bin/rm -f /usr/lib/libpng14.so.14.3.0
/bin/rm -f /usr/lib/libglib-2.0.so.0.2512.0
/bin/rm -f /usr/lib/libgmodule-2.0.so.0.2512.0
/bin/rm -f /usr/lib/libgobject-2.0.so.0.2512.0
/bin/rm -f /usr/lib/libgthread-2.0.so.0.2512.0
/bin/rm -f /usr/lib/libxml2.so.2.7.7
/bin/rm -f /usr/lib/libnetfilter_conntrack.so.3.0.2
/bin/rm -f /usr/lib/libnl.so.1.1
/bin/rm -f /lib/libcap.so.2.19
/bin/rm -f /lib/libncursesw.so.5.7
/bin/rm -f /usr/lib/libpanelw.so.5.7

# Remove obsolete and/or not working net-tools binaries
/bin/rm -f /sbin/nameif
/bin/rm -f /sbin/rarp

# 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

# Remove old fuser, we use /usr/bin as new location
rm -f /bin/fuser

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

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

# 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

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