#!/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
#
# 2006-03-24  cs language added
#

CMD="$1"

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

  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
  cp -p launch-ether-wake   /usr/local/bin/launch-ether-wake
  chown root:nobody         /usr/local/bin/launch-ether-wake
  chmod 4750                /usr/local/bin/launch-ether-wake

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

  # supported languages
  # english, german, dutch, french, spanish, italian
  echo "Adding language texts"
  cd langs/
  addto_lang en.wolgui,de.wolgui,nl.wolgui,fr.wolgui,es.wolgui,it.wolgui,cs.wolgui WoLGUI
  cd ..

  # try and add to menu
  echo "Adding menu entry"
  addto_header /var/ipcop/ header.pl menu.add "/\/cgi-bin\/time.cgi/" 1.4.0
}

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

  rm /home/httpd/cgi-bin/wakeonlan-gui.cgi
  rm /home/httpd/html/images/wakeup.gif
  rm -f /usr/local/bin/launch-ether-wake
  # old versions install in sbin
  rm -f /usr/local/sbin/launch-ether-wake

  # remove languages
  echo "Removing language texts"
  removefrom_lang en,de,nl,fr,es,it,cs WoLGUI

  # remove menu
  echo "Removing menu entry"
  removefrom_file /var/ipcop/ header.pl "/wolgui - BEGIN/,/wolgui - END/" 
}


# get usefull subroutines
# taken from:
# MOD Addons Server Ver 2.3 for IPCop 1.3.0/1.4.0
. ./library.addons

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 -f /var/ipcop/wakeonlan-gui/*
        rmdir /var/ipcop/wakeonlan-gui

	echo "Successfully done!"
        echo " ";;
    *)
        err "Invalid Option";;
esac
sync
#end of file
