#!/bin/bash
# ipcop addon installer for IPCop 2
#
# 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
# even more modifications 2009-2012 by weizen_42
#
# $Id: $
#


CMD="$1"

#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()
{
    if [ ! -e /usr/sbin/etherwake ]; then
        echo "ether-wake binary addon is not installed !"
        echo "  Get it from http://www.ipadd.de/binary.html"
        exit
    fi

    ## verify already installed and uninstall
    if [ -e /home/httpd/cgi-bin/wakeonlan-gui.cgi ]; then
        echo "Upgrading"
        au
    fi

    echo "Copying files"
    cp -p wakeonlan-gui.cgi   /home/httpd/cgi-bin/wakeonlan-gui.cgi
    cp -p wakeup.gif          /home/httpd/html/images/wakeup.gif
    chmod +s                  /usr/sbin/etherwake

    mkdir -p /var/ipcop/addons/wakeonlan-gui
    chown -R nobody:nobody    /var/ipcop/addons/wakeonlan-gui

    # supported languages
    echo "Adding language texts"
    addtolanguage WakeOnLanGUI en,cs,de,es,fr,it,nl langs

    echo "Add CGI"
    addcgi wakeonlan-gui.cgi
}

# deinstallation
au()
{
    if [ ! -e /home/httpd/cgi-bin/wakeonlan-gui.cgi ]; then
        echo "WOL-GUI addon is not installed !"
    fi

    echo "Remove CGI"
    removecgi wakeonlan-gui.cgi
    rm /home/httpd/html/images/wakeup.gif
    rm /usr/local/bin/launch-ether-wake

    # remove languages
    echo "Removing language texts"
    removefromlanguage WakeOnLanGUI
}


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

case $CMD in
    -i|i|install)
        echo " "
        echo "Installing now ..."
        ai
        echo "Successfully done!"
        echo " ";;

    -u|u|uninstall)
        echo " "
        echo "Deinstalling addon now ..."
        au

        rm -rf /var/ipcop/addons/wakeonlan-gui
        echo "Successfully done!"
        echo " ";;
    *)
        err "Invalid Option";;
esac
sync
#end of file
