#!/bin/bash
# ipcop addon binary installer Ver 0.9a for IPCop 1.4.x
#
# created 01 January 2005 by Frank 'Darkmann' Koch <darkmann@black-empire.de>
# modified 02 January 2005 by Tom 'wintermute' Eichstaedt <wintermute@tom-e.de>
#
# extended 2006-02-24 by weizen_42
#
# $Id: install 467 2011-09-24 20:45:34Z weizen_42 $
#

CMD="$1"
UPGRADE=0

if [ ! -e /usr/lib/ipcop/library.sh ]; then
    echo "Upgrade your IPCop, library.sh is missing"
    exit 1
fi
. /usr/lib/ipcop/library.sh

ismachine i486
if [ ${?} -ne 0 ]; then
    echo "This Addon is for i486 architecture only"
    exit 1
fi


#error handling
err()
{
    echo " "
    echo "Error : $1 "
    echo " "
    echo "Choose your option:"
    echo " "
    echo "./install -i   ---> to install"
    echo "./install -u   ---> to uninstall"
    echo " "
    exit
}


# installation
ai()
{
    ## verify already installed and uninstall
    if [ -e /var/ipcop/addons/xtiface/xtiface.conf ]; then
        echo "Upgrading"
        UPGRADE=1
        au
    else
        mkdir -p /var/ipcop/addons/xtiface
        cp xtiface.conf /var/ipcop/addons/xtiface/xtiface.conf
        chown nobody:nobody /var/ipcop/addons/xtiface/xtiface.conf
    fi

    echo "Copying files"
    cp -p launch-xtiface  /usr/local/bin/launch-xtiface

    chown root:nobody     /usr/local/bin/launch-xtiface
    chmod 4750            /usr/local/bin/launch-xtiface

    echo "Adding language texts"
    addtolanguage xtiface en,de,fr,bz langs/

    echo "Add CGI"
    addcgi xtiface.cgi

    echo "Adding special sections"
    addtofiletail xtiface local.add /etc/rc.d/rc.event.local
    addtofile xtiface dhcp.cgi.add1 /home/httpd/cgi-bin/dhcp.cgi "my @INTERFACEs"
}

# deinstallation
au()
{
    if [ ! -e /var/ipcop/addons/xtiface/xtiface.conf ]; then
        echo "Extra Interfaces addon is not installed !"
        exit
    fi

    echo "Stopping Extra Interfaces"
    if [ -e /usr/local/bin/launch-xtiface ]; then
        /usr/local/bin/launch-xtiface --stop 1
        /usr/local/bin/launch-xtiface --stop 2
        /usr/local/bin/launch-xtiface --stop 3
        /usr/local/bin/launch-xtiface --stop 4
    fi

    echo "Remove CGI"
    removecgi xtiface.cgi

    rm -f /usr/local/bin/launch-xtiface

    echo "Removing language texts"
    removefromlanguage xtiface

    echo "Removing special sections"
    removefromfile xtiface /etc/rc.d/rc.event.local
    removefromfile xtiface /etc/rc.d/rc.dnsmasq
    removefromfile xtiface /home/httpd/cgi-bin/dhcp.cgi
}


case $CMD in
    -i|i|install)
        echo " "
        echo "Installing now ..."
        ai
        echo "Successfully done!"
        if [ $UPGRADE -gt 0 ]; then
            /usr/local/bin/launch-xtiface --start 1
            /usr/local/bin/launch-xtiface --start 2
            /usr/local/bin/launch-xtiface --start 3
            /usr/local/bin/launch-xtiface --start 4
            /usr/local/bin/restartdhcp
        fi
        echo " ";;

    -u|u|uninstall)
        echo " "
        echo "Deinstalling addon now ..."
        au
        rm -rf /var/ipcop/addons/xtiface
        echo "Successfully done!"
        echo " ";;
 
    *)
        err "Invalid Option";;
esac
sync
#end of file
