#!/bin/sh
##############################################################################
#
# You may edit anything below this point at your own risk, do not request
# support for modified versions of this software. This is free software
# and free for redistribution in accordance with the GNU GPL.
#
##############################################################################
#
# Created by: Ryan M. <ryan@r-fx.net>
# R-fx Networks (c) 1999-2003
# SIM 2.5-3 <sim@r-fx.org>
#
##############################################################################
#
## PATH enviroment entry
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

## A few variables before we get underway
CNF="/usr/local/sim/conf.sim"
EXPD=`date | awk '{print$3}'`
MDAT="sim.dat"
UFILE="sim-stable"
ALERT="false"
V="2.5-3"

## Variables that should be 0 by default
TIC="0"
TIE="0"
TSERVC="0"
TSYSC="0"

## Does our config file exist, if not abort
if [ -f "$CNF" ]; then
        . $CNF
	if [ -f "$CNFMISC" ] && [ -f "$FUNCF" ]; then
		. $CNFMISC
		. $FUNCF
	else
	        echo "SIM $V <sim@r-fx.org>"
	        echo "Could not find \$CNFMISC and/or \$FUNCF, exiting."
	        exit 1
	fi
else
        echo "SIM $V <sim@r-fx.org>"
        echo "Could not find $CNF, exiting."
        exit 1
fi

## Does our installation path exist, if not abort
if [ ! -d $INSPATH ]; then
        header
        echo "$INSPATH not found, aborting."
        exit 1
fi

## Does our version file exist, if not creat it
if [ ! -f $INSPATH/version ]; then
        touch $INSPATH/version
        chmod 640 $INSPATH/version
        chown root $INSPATH/version
        echo "$V" >> $INSPATH/version
fi

## Our output header
header() {
        echo "SIM $V <sim@r-fx.org>"
        echo "========================"
}

pre() {
## Does our log exist, if not creat it
if [ ! -f $SIMLOG ]; then
        touch $SIMLOG
        chmod 640 $SIMLOG
        chown root $SIMLOG
        SIZE=`du -sk $SIMLOG | awk '{print$1}'`
        echo "$PREFIX touched log file." >> $SIMLOG
else
        SIZE=`du -sk $SIMLOG | awk '{print$1}'`
fi

## Is our logsize greater than $SLOGSIZE, if so clear it
if [ "$SIZE" -gt "$SLOGSIZE" ]; then
        rm -f $SIMLOG
        touch $SIMLOG
        chmod 640 $SIMLOG
        chown root $SIMLOG
        echo "$PREFIX logfile cleared due to size ($SLOGSIZE KB)." >> $SIMLOG
fi

## Does our Main dat file exist, if not creat it
if [ ! -f $INSPATH/$DAT/$MDAT ]; then
        rm -f $INSPATH/$DAT/*.dat
        touch $INSPATH/$DAT/$MDAT
        chmod 640 $INSPATH/$DAT/*.dat
        chown root $INSPATH/$DAT/*.dat
        echo "EXP:$EXPD" >> $INSPATH/$DAT/$MDAT
        echo "$PREFIX $MDAT not found, created." >> $SIMLOG
fi

## Get our experation value
if [ -f $INSPATH/$DAT/$MDAT ]; then
        EXPV=`cat $INSPATH/$DAT/$MDAT  | tr ':' ' ' | grep EXP | awk '{print$2}'`
fi

## Is our experation value greater than the current date, if so reset .dat info
if [ "$EXPD" -gt "$EXPV" ]; then
        rm -f $INSPATH/$DAT/$MDAT
        echo "$PREFIX .dat files expired, removing." >> $SIMLOG
        pre
fi

## Creat our temp path
if [ ! -d "$INSPATH/tmp" ]; then
	mkdir $INSPATH/tmp
	chmod 600 $INSPATH/tmp
else
	chmod 600 $INSPATH/tmp
fi

## Creat/validate our cache files
if [ ! "$1" == "1" ]; then
  if [ -f "$PS_CACHE" ]; then
          rm $PS_CACHE
          touch $PS_CACHE
          chmod 600 $PS_CACHE
  else
          touch $PS_CACHE
          chmod 600 $PS_CACHE

  fi
  if [ -f "$NS_CACHE" ]; then
          rm $NS_CACHE
          touch $NS_CACHE
          chmod 600 $NS_CACHE
  else
          touch $NS_CACHE
          chmod 600 $NS_CACHE

  fi
  if [ -f "$PS_CACHE" ] && [ -f "$NS_CACHE" ]; then
          ps -auxww | grep -v $IGNORE >> $PS_CACHE
          netstat -nlp | grep -v $IGNORE >> $NS_CACHE
  else
          echo "$PREFIX error creating cache files, aborting." >> $SIMLOG
          echo "error creating cache files, aborting."
          exit 1
  fi
fi
}

srv_dat() {
SRV="$1"
if [ ! -f $INSPATH/$DAT/$SRV.dat ] && [ ! -z $SRV ]; then
        touch $INSPATH/$DAT/$SRV.dat
        chmod 640 $INSPATH/$DAT/$SRV.dat
        chown root:root $INSPATH/$DAT/$SRV.dat
        echo "EVC:0" >> $INSPATH/$DAT/$SRV.dat
elif [ -z $SRV ]; then
        echo "fatal error in srv_dat(), aborting..."
        echo "fatal error in srv_dat(), aborting..." >> $SIMLOG
	exit 1
fi
}

load_chk() {
if [ -f $INSPATH/$DAT/$MDAT ]; then
        TIE=`cat $INSPATH/$DAT/$MDAT  | tr ':' ' ' | grep TIE | awk '{print$2}'`
        if [ "$TIE" = "" ]; then
           TIE="0"
        fi
fi


## Lets load our system checks
if [ "$SYS_LM" = "true" ]; then
        TIC=$[TIC+1]
        TSYSC=$[TSERVC+1]
	EVCOUNT_LM="0"
        . $INSPATH/$CHKSYS/lm/lm.chk
        chk_loadmon
else
	EVCOUNT_LM="0"
        LOAD_MON="module disabled"
fi

if [ "$SYS_NET" = "true" ]; then
        TIC=$[TIC+1]
        TSYSC=$[TSERVC+1]
        EVCOUNT_NET="0"
        . $INSPATH/$CHKSYS/net.chk
        chk_net
else
        EVCOUNT_NET="0"
        NET="module disabled"
fi

## Lets load our service checks
if [ "$SERV_FTP" = "true" ]; then
        TIC=$[TIC+1]
	TSERVC=$[TSERVC+1]
	EVCOUNT_FTP="0"
        . $INSPATH/$CHKSERV/ftp.chk
        chk_ftp
else
	EVCOUNT_FTP="0"
        FTP="module disabled"
fi

if [ "$SERV_HTTP" = "true" ]; then
        TIC=$[TIC+1]
        TSERVC=$[TSERVC+1]
	EVCOUNT_HTTP="0"
        . $INSPATH/$CHKSERV/http.chk
        chk_http
else
	EVCOUNT_HTTP="0"
        HTTP="module disabled"
fi

if [ "$SERV_DNS" = "true" ]; then
        TIC=$[TIC+1]
        TSERVC=$[TSERVC+1]
	EVCOUNT_DNS="0"
        . $INSPATH/$CHKSERV/dns.chk
        chk_dns
else
	EVCOUNT_DNS="0"
        DNS="module disabled"
fi

if [ "$SERV_SSH" = "true" ]; then
        TIC=$[TIC+1]
        TSERVC=$[TSERVC+1]
	EVCOUNT_SSH="0"
        . $INSPATH/$CHKSERV/ssh.chk
        chk_ssh
else
	EVCOUNT_SSH="0"
        SSH="module disabled"
fi

if [ "$SERV_MYSQL" = "true" ]; then
        TIC=$[TIC+1]
        TSERVC=$[TSERVC+1]
	EVCOUNT_MYSQL="0"
        . $INSPATH/$CHKSERV/mysql.chk
        chk_mysql
else
	EVCOUNT_MYSQL="0"
        MYSQL="module disabled"
fi

if [ "$SERV_XINET" = "true" ]; then
        TIC=$[TIC+1]
        TSERVC=$[TSERVC+1]
        EVCOUNT_XINET="0"
        . $INSPATH/$CHKSERV/xinet.chk
        chk_xinet
else
        EVCOUNT_XINET="0"
        XINET="module disabled"
fi

if [ "$SERV_SMTP" = "true" ]; then
        TIC=$[TIC+1]
        TSERVC=$[TSERVC+1]
        EVCOUNT_SMTP="0"
        . $INSPATH/$CHKSERV/smtp.chk
        chk_smtp
else
        EVCOUNT_SMTP="0"
        SMTP="module disabled"
fi

if [ "$SERV_PGSQL" = "true" ]; then
        TIC=$[TIC+1]
        TSERVC=$[TSERVC+1]
        EVCOUNT_PGSQL="0"
        . $INSPATH/$CHKSERV/pgsql.chk
        chk_pgsql
else
        EVCOUNT_PGSQL="0"
        PGSQL="module disabled"  
fi

if [ "$SERV_EWP" = "true" ]; then   
        TIC=$[TIC+1]
        TSERVC=$[TSERVC+1]
        EVCOUNT_EWP="0"
        . $INSPATH/$CHKSERV/ewp.chk
        chk_ewp  
else
        EVCOUNT_EWP="0" 
        EWP="module disabled"
fi  

## Lets sum it up
if [ ! "$TIE" == "0" ]; then
   rm -f $INSPATH/$DAT/$MDAT
   chmod 640 $INSPATH/$DAT/*.dat
   chown root $INSPATH/$DAT/*.dat
   echo "EXP:$EXPD" >> $INSPATH/$DAT/$MDAT
   echo "TIE:$TIE" >> $INSPATH/$DAT/$MDAT
fi

if [ "$TIC" == "0" ]; then   
      echo "$PREFIX no .chk modules enabled." >> $SIMLOG
else    
      AVG=$[TIE/TIC]
fi
}

quiet() {
	lock
        pre
        load_chk
        alert
	fin_clean
}

verbose() {
	lock
        pre
        load_chk
        alert
        tail -n 10 $SIMLOG
	fin_clean
}

fin_clean() {
	rm -f $PS_CACHE
	rm -f $NS_CACHE
	rm -f $LOCK
	exit 0
}

trap_fin_clean() {
        echo "$PREFIX user interrupt -- caught sigterm, exiting." >> $SIMLOG
        echo "user interrupt -- caught sigterm, exiting."
	fin_clean
}
trap trap_fin_clean 2

clean() {
        pre
        rm -f $SIMLOG
        rm -f $INSPATH/$DAT/*.dat
	rm -f $LOCK
        pre
	header
        echo "$PREFIX cleared data & log files." >> $SIMLOG
	echo "Cleared .dat & log files."
}

lstatus() {
        pre 1
        tac $SIMLOG | more
}

status() {
	lock
        pre
        load_chk
        alert
        OUTSL=`tail -n $TAILSIML $SIMLOG`
        OUTKL=`tail -n $TAILSYSL $SYSLOG`
	STATUS="true"
        header
          if [ -f $STGEN ]; then
           . $STGEN
           else
           echo "Error: could not locate $STGEN, aborting."
           exit 1
          fi
	fin_clean
}

cj() {
pre 1
CRON="/etc/crontab"
TMPS="/tmp/sim_cj"
SIMCJ=`cat $CRON | grep -w sim`

if [ ! -f $CRON ]; then
   echo "$CRON does not exist, aborting."
   exit 1
fi

if [ "$SIMCJ" = "" ]; then
## Install SIM cronjob
   header
   cp -f $CRON $CRON.bk.$$
   cat > $TMPS <<EOF

*/5 * * * * root $INSPATH/sim -q >> /dev/null 2>&1
EOF
   cat $TMPS >> /etc/crontab
   rm -f $TMPS
   rm -f /tmp/crontab
   echo "Installed SIM cronjob."
else
## Remove SIM cronjob
    header
    cp -f $CRON $CRON.bk.$$
    SIMCJ=`cat $CRON | grep -vw sim >> $TMPS`
    rm -f $CRON
    touch $CRON
    chmod 644 $CRON
    cat $TMPS >> $CRON
    rm -f $TMPS
    rm -f /tmp/crontab
    echo "Removed SIM cronjob."
fi
}

update() {
lock
header
pre 1
echo "Checking $HOST for updates..."
cd $INSPATH/tmp
CV=`cat $INSPATH/version | tr '.' ' ' | tr '-' ' ' | awk '{print$1$2$3}'`

if [ -f $UFILE ]; then
   rm $UFILE
   wget -T 15 -q http://$HOST/downloads/$UFILE >> /dev/null 2>&1
   if [ ! -f $INSPATH/tmp/$UFILE ]; then
     echo "Could not get update information, possible connection error."
     exit 1
   fi
   SV=`cat $UFILE | tr '.' ' ' | tr '-' ' ' | awk '{print$1$2$3}'`
   UV=`cat $UFILE`
else
   wget -T 15 -q http://$HOST/downloads/$UFILE >> /dev/null 2>&1
   if [ ! -f $INSPATH/tmp/$UFILE ]; then
     echo "Could not get update information, possible connection error."
     exit 1
   fi
   SV=`cat $UFILE | tr '.' ' ' | tr '-' ' ' | awk '{print$1$2$3}'`
   UV=`cat $UFILE`
fi

if [ "$SV" -eq "$CV" ]; then
   echo "Current version of SIM already installed ($V)."
   rm -f $INSPATH/tmp/$UFILE
   exit 0
fi

if [ "$SV" -lt "$CV" ]; then
   echo "Currently installed version of SIM ($V), is newer than version info on"
   echo "$HOST, using a development release ?"
   rm -f $INSPATH/tmp/$UFILE
   exit 0
fi

if [ "$SV" -gt "$CV" ]; then
   echo ""
   echo "Update for SIM is availible, would you like to download and install"
   echo "SIM version $UV ? If you choose to update, you must reconfigure SIM"
   echo "via conf.sim or the autoconf script."
   echo ""
   echo "press return to continue or CTRL+C to abort..."
   read K
   echo -n "Downloading & extracting SIM version $UV:"
   echo -n "[###"
   wget -q http://$HOST/downloads/sim-$UV.tar.gz
   echo -n "##"
   tar xfz sim-$UV.tar.gz >> /dev/null
   echo -n "##"
   cd sim-$UV
   echo "###]"
   echo "Running setup script..."
   ./setup -q
   rm -f $INSPATH/version
fi

rm -f $INSPATH/tmp/$UFILE
rm -rf $INSPATH/tmp/sim-*
fin_clean
}

help() {
        header
        echo "usage $0"
        echo "-q     Quiet mode"
        echo "-v     Verbose mode"
        echo "-c     Clear data & log files"
        echo "-l     Display log file"
        echo "-s     Display status information"
        echo "-u     Check for SIM updates"
        echo "-j     Install/Remove SIM cronjob"
}

case "$1" in
 -q)
        quiet
        ;;
 -v)
        verbose
        ;;
 -c)
        clean
        ;;
 -l)
        lstatus
        ;;
 -s)
        status
        ;;
 -u)
        update
        ;;
 -j)
        cj
        ;;
 *)
        help
esac

exit 0
