#!/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 /home/httpd/cgi-bin/muningui.cgi ]; then
    echo "Upgrading"
    au
  fi

  echo "Copying files"
  cp -p muningui.cgi    /home/httpd/cgi-bin/muningui.cgi
  cp -p munin-node      /usr/sbin/munin-node
  cp -p rc.munin-node   /etc/rc.d/rc.munin-node
  cp -p muninctrl       /usr/local/bin
  chown root.nobody     /usr/local/bin/muninctrl
  chmod 4750            /usr/local/bin/muninctrl

  mkdir -p /var/ipcop/munin
  chown -R nobody.nobody /var/ipcop/munin
  if [ ! -e /var/ipcop/munin/settings ]; then
    touch                  /var/ipcop/munin/settings
    chown nobody.nobody    /var/ipcop/munin/settings
  fi
  if [ ! -e /var/ipcop/munin/munin-node.conf ]; then
    cp munin-node.conf     /var/ipcop/munin/munin-node.conf
    chown nobody.nobody    /var/ipcop/munin/munin-node.conf
  fi

  echo "Adding files and standard plugins"
  mkdir -p /etc/munin/plugin-conf.d
  cp -p plugin-conf     /etc/munin/plugin-conf.d/munin-node
  mkdir -p /usr/share/munin/plugins
  cp -p plugins/* /usr/share/munin/plugins
  mkdir -p /etc/munin/plugins
  for i in cpu df df_abs df_inode fw_conntrack fw_forwarded_local irqstats load memory netstat swap uptime
  do
    ln -sf /usr/share/munin/plugins/$i /etc/munin/plugins/$i
  done

  # smartctl plugin ?
  if [ -e /usr/local/sbin/smartctl ]; then
    ln -sf /usr/share/munin/plugins/hddtemp_smartctl /etc/munin/plugins/hddtemp_smartctl
    echo -e "\\033[1;32m  Added smartctl plugin \\033[0m"
  fi

  # digitemp plugin ?
  if [ -e /var/ipcop/digitemp/settings ]; then
    eval $(/usr/local/bin/readhash /var/ipcop/digitemp/settings)
    ln -sf /usr/share/munin/plugins/digitemp_ /etc/munin/plugins/digitemp_$ADAPTER
    echo -e "\\033[1;32m  Added digitemp plugin \\033[0m"
  fi


  IFACES=`/usr/share/munin/plugins/if_ suggest`
  for i in $IFACES
  do
    ln -sf /usr/share/munin/plugins/if_ /etc/munin/plugins/if_$i
    ln -sf /usr/share/munin/plugins/if_err_ /etc/munin/plugins/if_err_$i
  done

  tar xvfz net-server-0.9.6-ipcop-0.1.0.tar.gz -C / >/dev/null

  # supported languages
  # english, german
  echo "Adding language texts"
  cd langs/
  addto_lang en.munin,de.munin munin
  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

  # add to status
  echo "Adding to status"
  addto_file /home/httpd/cgi-bin status.cgi status.add "/syslogd/"

  # and add start
  echo "Adding to rc.local"
  cat local.add >> /etc/rc.d/rc.local
}

# deinstallation
au()
{
  if [ ! -e /home/httpd/cgi-bin/muningui.cgi ]; then
    echo "Munin addon is not installed !"
    exit
  fi

  /etc/rc.d/rc.munin-node stop

  rm -f /home/httpd/cgi-bin/muningui.cgi
  rm -f /usr/sbin/munin-node
  rm -f /etc/rc.d/rc.munin-node

  rm -rf /etc/munin
  rm -rf /usr/share/munin

  rm -f /usr/lib/perl5/site_perl/5.8.5/Net/Server.*
  rm -rf /usr/lib/perl5/site_perl/5.8.5/Net/Server

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

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

  # remove status
  echo "Removing from status"
  removefrom_file /home/httpd/cgi-bin/ status.cgi "/munin - BEGIN/,/munin - END/" 

  # remove start
  echo "Removing from rc.local"
  removefrom_file /etc/rc.d/ rc.local "/munin - BEGIN/,/munin - 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 -e "To start: \033[1;34m/etc/rc.d/rc.munin-node start\033[0m\n"
        echo " ";;

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