#!/bin/bash
#
# Upgrade IPCop
#

UPGRADEVERSION=2.1.3
PREVIOUSVERSION=2.1.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


FCRONPID=`cat /var/run/fcron.pid`
/bin/kill $FCRONPID
COUNT=15

while [ -e /var/run/fcron.pid -a ${COUNT} -ge 0 ]
do
    sleep 1
    COUNT=$(( ${COUNT} - 1 ))
done

if [ -e /var/run/fcron.pid ]; then
    /bin/kill -KILL $FCRONPID
fi

#####
#
# Add version specific handling *before* unpacking the patch here.
# For example stopping dnsmasq(required before update), squid, etc...
#
#####


#####
#
# 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-2 --keep 2.6.32-1 --remove 2.6.32-0

# Modify or remove line below, depending on update
# Create the new initramfs
# /sbin/mkinitramfs --with-kernel=2.6.32-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 .....
# bind
/bin/rm -f /usr/lib/libbind9.so.80
/bin/rm -f /usr/lib/libbind9.so.80.0.8
/bin/rm -f /usr/lib/libdns.so.122
/bin/rm -f /usr/lib/libdns.so.122.1.0
/bin/rm -f /usr/lib/libisc.so.84
/bin/rm -f /usr/lib/libisc.so.84.5.1
/bin/rm -f /usr/lib/libisccc.so.80
/bin/rm -f /usr/lib/libisccc.so.80.0.4
/bin/rm -f /usr/lib/libisccfg.so.82
/bin/rm -f /usr/lib/libisccfg.so.82.0.7
/bin/rm -f /usr/lib/liblwres.so.80
/bin/rm -f /usr/lib/liblwres.so.80.0.7
# cairo
/bin/rm -f /usr/lib/libcairo.so.2
/bin/rm -f /usr/lib/libcairo.so.2.11000.2
# glib
/bin/rm -f /usr/lib/libglib-2.0.so.0
/bin/rm -f /usr/lib/libglib-2.0.so.0.3000.3
/bin/rm -f /usr/lib/libgmodule-2.0.so.0
/bin/rm -f /usr/lib/libgmodule-2.0.so.0.3000.3
/bin/rm -f /usr/lib/libgobject-2.0.so.0
/bin/rm -f /usr/lib/libgobject-2.0.so.0.3000.3
/bin/rm -f /usr/lib/libgthread-2.0.so.0
/bin/rm -f /usr/lib/libgthread-2.0.so.0.3000.3
# libgcrypt
/bin/rm -f /usr/lib/libgcrypt.so.11
/bin/rm -f /usr/lib/libgcrypt.so.11.8.2
# libpng
/bin/rm -f /usr/lib/libpng16.so.16
/bin/rm -f /usr/lib/libpng16.so.16.7.0
# freetype
/bin/rm -f /usr/lib/libfreetype.so.6
/bin/rm -f /usr/lib/libfreetype.so.6.10.2
# libgpg-error
/bin/rm -f /usr/lib/libgpg-error.so.0
/bin/rm -f /usr/lib/libgpg-error.so.0.7.0
# libnl
/bin/rm -f /usr/lib/libnl-3.so.200
/bin/rm -f /usr/lib/libnl-3.so.200.12.1
/bin/rm -f /usr/lib/libnl-genl-3.so.200
/bin/rm -f /usr/lib/libnl-genl-3.so.200.12.1
# openldap
/bin/rm -f /usr/lib/liblber-2.4.so.2
/bin/rm -f /usr/lib/liblber-2.4.so.2.10.1
/bin/rm -f /usr/lib/libldap-2.4.so.2
/bin/rm -f /usr/lib/libldap-2.4.so.2.10.1
# pango
/bin/rm -f /usr/lib/libpango-1.0.so.0
/bin/rm -f /usr/lib/libpango-1.0.so.0.2800.4
/bin/rm -f /usr/lib/libpangocairo-1.0.so.0
/bin/rm -f /usr/lib/libpangocairo-1.0.so.0.2800.4
/bin/rm -f /usr/lib/libpangoft2-1.0.so.0
/bin/rm -f /usr/lib/libpangoft2-1.0.so.0.2800.4
/bin/rm -rf /usr/lib/pango/1.6.0
# pcre
/bin/rm -f /usr/lib/libpcre.so.1
/bin/rm -f /usr/lib/libpcre.so.1.2.1
# procps
/bin/rm -f /lib/libprocps.so.1
/bin/rm -f /lib/libprocps.so.1.1.2
# readline
/bin/rm -f /usr/lib/libhistory.so.6
/bin/rm -f /usr/lib/libhistory.so.6.2
/bin/rm -f /usr/lib/libreadline.so.6
/bin/rm -f /usr/lib/libreadline.so.6.2
# tzdata
/bin/rm -rf /usr/share/zoneinfo/posix/Mideast

# arping now in /usr/bin
/bin/rm -f /usr/sbin/arping
# renamed makegraphs
/bin/rm -f /usr/local/bin/makegraphs
# pidof now in /usr/bin
/bin/rm -f /bin/pidof
# squid
/bin/rm -f /usr/lib/squid/url_fake_rewrite
/bin/rm -f /usr/lib/squid/url_fake_rewrite.sh

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

# Cannot unpack to symlinked /var/log on FLASH installation, so copy here
cp /var/ipcop/traffic/empty-ulogd.db /var/log/traffic/ulogd.db

# Create EdDSSA key before starting new sshd
if [ ! -e /etc/ssh/ssh_host_ed25519_key ]; then
    /usr/bin/ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
fi
# Use new sshd
/usr/local/bin/restartssh

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

# Patch fcrontab for new fcron
/bin/sed -i -e 's|^!mailto.*|#!mailto()|g' /var/spool/cron/root.orig
# Patch frontab for renamed makegraphs
/bin/sed -i -e 's|makegraphs |makegraphs\.pl |g' /var/spool/cron/root.orig
/usr/bin/fcrontab -z

#####
#
# End of version specific handling. Continue with universal stuff.
#
#####

# Restart fcron
if [ -e /etc/FLASH ]; then
    /usr/sbin/fcron -s 86400
else
    /usr/sbin/fcron
fi

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