#!/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
#
#

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()
{
  ## verify already installed and uninstall
  if [ -e /var/ipcop/main/color_settings ]; then
    au
  else
    # copy config file
    echo "Copy config file"
    cp color_settings /var/ipcop/main/color_settings
    chown nobody:nobody /var/ipcop/main/color_settings
  fi


  # patch some files
  echo "Patching files"
  addto_file /home/httpd/cgi-bin/ gui.cgi guicolors.cgi.add140 "/<td\scolspan='2'><hr\s\/><\/td>/"
  PATCH=`grep guicolors /home/httpd/cgi-bin/gui.cgi`
  if [ "x$PATCH" == "x" ]; then
    addto_file /home/httpd/cgi-bin/ gui.cgi guicolors.cgi.add141 "/beep\swhen\sppp/"
  else
    echo "Patched 1.4.10"
  fi
  addto_file /home/httpd/cgi-bin/ gui.cgi guicolors.cgi.add1 "/readhash.*mainsettings);/"
  echo "Adding menu entry"
  addto_file /var/ipcop/ header.pl header.pl.add "/our\s\$javascript\s=\s1;/"


  # supported languages
  # english, german
  echo "Adding language texts"
  cd langs/
  addto_lang en.colors,de.colors guicolors
  cd ..
}

# deinstallation
au()
{
  # restore cgi
  echo "Restoring cgi files"
  removefrom_file /home/httpd/cgi-bin/ gui.cgi "/guicolors - BEGIN/,/guicolors - END/" 
  removefrom_file /var/ipcop/ header.pl "/guicolors - BEGIN/,/guicolors - END/" 

  echo "Removing menu entry"
  removefrom_file /var/ipcop/ header.pl "/guicolors - BEGIN/,/guicolors - END/" 

  # remove languages
  echo "Removing language texts"
  removefrom_lang en,de guicolors
}


# 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 /var/ipcop/main/color_settings
	echo "Successfully done!"
        echo " ";;
 
    *)
        err "Invalid Option";;
esac
sync
#end of file
