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

V="2.5-3"
CNF="conf.sim"

header() {
clear
echo "SIM $V Auto-Config Script"
echo ""
}

config() {
echo "All questions default to value in brackets if no answer is given. If you"
echo "make a typo during the autoconf process, hit CTRL+C (^C) to abort and"
echo "rerun the autoconf script ($0)."
echo
echo "The below are general configuration options for SIM:"
echo "press return to continue..."
read E

INSPATH="/usr/local/sim"
echo "Where is SIM installed ?"
echo -n "[$INSPATH]:$c"
read cc
if [ ! -z "$cc" ]; then
	INSPATH="$cc"
fi

if [ ! -f $INSPATH/$CNF.old ]; then
 if [ ! -f $INSPATH/$CNF ]; then
    header
    echo "Unable to locate $INSPATH/$CNF, aborting."
    exit 1
 else
    . $INSPATH/$CNF >> /dev/null 2>&1
 fi 
 if [ ! -f $INSPATH/$CNF.default ]; then
    cp $INSPATH/$CNF $INSPATH/$CNF.default
 fi
else
    . $INSPATH/$CNF >> /dev/null 2>&1
    . $INSPATH/$CNF.old >> /dev/null 2>&1
fi

echo
echo "Where should the sim.log file be created ?"
echo -n "[$SIMLOG]:$c"
read cc
if [ ! -z "$cc" ]; then
	SIMLOG="$cc"
fi

echo

echo "Max size of sim.log before rotated ? (value in KB)"
echo -n "[$SLOGSIZE]:$c"
read cc
if [ ! -z "$cc" ]; then
	SLOGSIZE="$cc"
fi

echo

echo "What is the location of your kernel log ?"
if [ -f /var/log/messages ]; then
   SYSLOG="/var/log/messages"
   echo "Found kernel log at $SYSLOG"
else
echo -n "[$SYSLOG]:$c"
read cc
 if [ ! -z "$cc" ]; then
	SYSLOG="$cc"
 fi
fi
echo

echo "Where should alerts be emailed to ? (e.g: root, user@domain)"
echo -n "[$EMAIL]:$c"
read cc
if [ ! -z "$cc" ]; then
	EMAIL="$cc"
fi

echo

echo "Disable alert emails after how many events, to avoid email flood ?"
echo "(Note: events stats are cleared daily)"
echo -n "[$EVLIMIT]:$c"
read cc
if [ ! -z "$cc" ]; then
	EVLIMIT="$cc"
fi

echo
echo "The below are configuration options for Service modules:"
echo "press return to continue..."
read F

echo "Auto-restart services found to be offline ? (true=enable, false=disable)"
echo -n "[$RST]:$c"
read cc
if [ ! -z "$cc" ]; then
	RST="$cc"
fi
echo

echo "Enforce laxed service checking ? (true=enable, false=disable)"
echo -n "[$LAXCHK]:$c"
read cc
if [ ! -z "$cc" ]; then
        LAXCHK="$cc"
fi
echo

echo "Disable auto-restart after how many downed service events ?"
echo "(Note: events stats are cleared daily)"
echo -n "[$DISRST]:$c"
read cc
if [ ! -z "$cc" ]; then
	DISRST="$cc"
fi
echo

## Service modules
echo "Enable FTP service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_FTP]:$c"
read cc
if [ ! -z "$cc" ]; then
	SERV_FTP="$cc"
fi
echo
if [ "$SERV_FTP" = "true" ]; then
        echo "Name of the FTP service as appears in 'ps' ?"
	TMP_VAL=`ps -auxww | grep -w proftpd` >> /dev/null 2>&1
	if [ ! "$TMP_VAL" == "" ]; then
		FTP_NAME="proftpd"
	        echo "Found service name as $FTP_NAME"
	else
	        echo -n "[$FTP_NAME]:$c"
	        read cc
	        if [ ! -z "$cc" ]; then
	          FTP_NAME="$cc"
	        fi
	fi
        echo
        echo "TCP/IP port that FTP operates on ?"
        TMP_VAL=`netstat -npl | grep LISTEN | grep -w $FTP_NAME | grep -w 21` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
		FTP_PORT="21"
                echo "Found service port as $FTP_PORT"
	else
	        echo -n "[$FTP_PORT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  FTP_PORT="$cc"
	        fi
	fi
        echo
        echo "Path to FTP service init script ?"
	if [ -f /etc/init.d/proftpd ]; then
		FTP_INIT="/etc/init.d/proftpd"
		echo "Found service init script at $FTP_INIT"
        else
	        echo -n "[$FTP_INIT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	   FTP_INIT="$cc"
	        fi
	fi
echo
fi

echo "Enable HTTP service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_HTTP]:$c"
read cc
if [ ! -z "$cc" ]; then
	SERV_HTTP="$cc"
fi
echo
if [ "$SERV_HTTP" = "true" ]; then
        echo "Name of the HTTP service as appears in 'ps' ?"
        TMP_VAL=`ps -auxww | grep -w httpd` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
		HTTP_NAME="httpd"
		echo "Found service name as $HTTP_NAME"
	else
	        echo -n "[$HTTP_NAME]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
	          HTTP_NAME="$cc"
	        fi
	fi
        echo
        echo "TCP/IP port that HTTP operates on ?"
        TMP_VAL=`netstat -npl | grep LISTEN | grep -w $HTTP_NAME | grep -w 80` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
		HTTP_PORT="80"
		echo "Found service port as $HTTP_PORT"
	else
	        echo -n "[$HTTP_PORT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  HTTP_PORT="$cc"
	        fi
	fi
        echo
        echo "Path to HTTP service init script ?"
	if [ -f /etc/init.d/httpd ]; then
	 HTTP_INIT="/etc/init.d/httpd"
	 echo "Found service init script at $HTTP_INIT"
	else         
         echo -n "[$HTTP_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           HTTP_INIT="$cc"
         fi
	fi
echo
fi

echo "Enable DNS service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_DNS]:$c"
read cc
if [ ! -z "$cc" ]; then
	SERV_DNS="$cc"
fi
echo
if [ "$SERV_DNS" = "true" ]; then
        echo "Name of the DNS service as appears in 'ps' ?"
        TMP_VAL=`ps -auxww | grep -w named` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
		DNS_NAME="named"
                echo "Found service name as $DNS_NAME"
	else
	        echo -n "[$DNS_NAME]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  DNS_NAME="$cc"
	        fi
	fi
        echo
        echo "TCP/IP port that DNS operates on ?"
        TMP_VAL=`netstat -npl | grep LISTEN | grep -w $DNS_NAME | grep -w 53` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                DNS_PORT="53"
                echo "Found service port as $DNS_PORT"
        else
	        echo -n "[$DNS_PORT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  DNS_PORT="$cc"
	        fi
	fi
        echo
        echo "Path to DNS service init script ?"
	if [ -f /etc/init.d/named ]; then
         DNS_INIT="/etc/init.d/named"
         echo "Found service init script at $DNS_INIT"
        else
         echo -n "[$DNS_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           DNS_INIT="$cc"
         fi
	fi
echo
fi

echo "Enable SSH service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_SSH]:$c"
read cc
if [ ! -z "$cc" ]; then
	SERV_SSH="$cc"
fi
echo
if [ "$SERV_SSH" = "true" ]; then
        echo "Name of the SSH service as appears in 'ps' ?"
        TMP_VAL=`ps -auxww | grep -w sshd` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                SSH_NAME="sshd"
                echo "Found service name as $SSH_NAME"
        else
	        echo -n "[$SSH_NAME]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  SSH_NAME="$cc"
	        fi
	fi
        echo
        echo "TCP/IP port that SSH operates on ?"
        TMP_VAL=`netstat -npl | grep LISTEN | grep -w $SSH_NAME | grep -w 22` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                SSH_PORT="22"
                echo "Found service port as $SSH_PORT"
        else
	        echo -n "[$SSH_PORT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  SSH_PORT="$cc"
	        fi
	fi
        echo
        echo "Path to SSH service init script ?"
	if [ -f /etc/init.d/sshd ]; then
         SSH_INIT="/etc/init.d/sshd"
         echo "Found service init script at $SSH_INIT"
        else
         echo -n "[$SSH_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           SSH_INIT="$cc"
         fi
	fi
echo
fi

echo "Enable MYSQL service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_MYSQL]:$c"
read cc
if [ ! -z "$cc" ]; then
	SERV_MYSQL="$cc"
fi
echo
if [ "$SERV_MYSQL" = "true" ]; then
        echo "Name of the MYSQL service as appears in 'ps' ?"
        TMP_VAL=`ps -auxww | grep -w mysqld` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                MYSQL_NAME="mysqld"
                echo "Found service name as $MYSQL_NAME"
        else
	        echo -n "[$MYSQL_NAME]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  MYSQL_NAME="$cc"
	        fi
	fi
        echo
        echo "TCP/IP port that MYSQL operates on ?"
        TMP_VAL=`netstat -npl | grep LISTEN | grep -w $MYSQL_NAME | grep -w 3306` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                MYSQL_PORT="3306"
                echo "Found service port as $MYSQL_PORT"
        else
	        echo -n "[$MYSQL_PORT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  MYSQL_PORT="$cc"
	        fi
	fi
        echo
        echo "Path to MYSQL service init script ?"
        if [ -f /etc/init.d/mysqld ]; then
         MYSQL_INIT="/etc/init.d/mysqld"
         echo "Found service init script at $MYSQL_INIT"
	elif [ -f /etc/init.d/mysql ]; then
         MYSQL_INIT="/etc/init.d/mysql"
         echo "Found service init script at $MYSQL_INIT"
        else
         echo -n "[$MYSQL_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           MYSQL_INIT="$cc"
         fi
	fi
echo
fi

echo "Enable SMTP service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_SMTP]:$c"
read cc
if [ ! -z "$cc" ]; then
        SERV_SMTP="$cc"
fi
echo
if [ "$SERV_SMTP" = "true" ]; then
        echo "Name of the SMTP service as appears in 'ps' ?"
        TMP_VAL=`ps -auxww | grep -w sendmail` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                SMTP_NAME="sendmail"
                echo "Found service name as $SMTP_NAME"
        else
	        echo -n "[$SMTP_NAME]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  SMTP_NAME="$cc"
	        fi
	fi
        echo
        echo "TCP/IP port that SMTP operates on ?"
        TMP_VAL=`netstat -npl | grep -w $SMTP_NAME | grep -w 25` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                SMTP_PORT="25"
                echo "Found service port as $SMTP_PORT"
        else
	        echo -n "[$SMTP_PORT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  SMTP_PORT="$cc"
	        fi
	fi
        echo
        echo "Path to SMTP service init script ?"
        if [ -f /etc/init.d/sendmail ]; then
         SMTP_INIT="/etc/init.d/sendmail"
         echo "Found service init script at $SMTP_INIT"
        else
         echo -n "[$SMTP_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           SMTP_INIT="$cc"
         fi
        fi
echo
fi

echo "Enable XINET service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_XINET]:$c"
read cc
if [ ! -z "$cc" ]; then
        SERV_XINET="$cc"
fi
echo
if [ "$SERV_XINET" = "true" ]; then
        echo "Name of the XINET service as appears in 'ps' ?"
        TMP_VAL=`ps -auxww | grep -w xinetd` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                XINET_NAME="xinetd"
                echo "Found service name as $XINET_NAME"
        else
	        echo -n "[$XINET_NAME]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  XINET_NAME="$cc"
	        fi
	fi
        echo
        echo "TCP/IP port that any XINET service operates on (e.g: pop3, 110) ?"
        TMP_VAL=`netstat -npl | grep LISTEN | grep -w $XINET_NAME | grep -w 110` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                XINET_PORT="110"
                echo "Found service port as $XINET_PORT"
        else
	        echo -n "[$XINET_PORT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  XINET_PORT="$cc"
	        fi
	fi
        echo
        echo "Path to XINET service init script ?"
        if [ -f /etc/init.d/xinetd ]; then
         XINET_INIT="/etc/init.d/xinetd"
         echo "Found service init script at $XINET_INIT"
        else
         echo -n "[$XINET_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           XINET_INIT="$cc"
         fi
        fi
echo
fi

echo "Enable ENSIM service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_EWP]:$c"
read cc
if [ ! -z "$cc" ]; then
        SERV_EWP="$cc"  
fi
echo
if [ "$SERV_EWP" = "true" ]; then
        echo "Name of the ENSIM service as appears in 'ps' ?"
        TMP_VAL=`ps -auxww | grep -w ocwhttpd` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                EWP_NAME="ocwhttpd"
                echo "Found service name as $EWP_NAME"
        else
	        echo -n "[$EWP_NAME]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  EWP_NAME="$cc"
	        fi
	fi
        echo  
        echo "TCP/IP port that ENSIM operates on ?"
        TMP_VAL=`netstat -npl | grep LISTEN | grep -w $EWP_NAME | grep -w 19638` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                EWP_PORT="19638"
                echo "Found service port as $EWP_PORT"
        else
	        echo -n "[$EWP_PORT]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
        	  EWP_PORT="$cc"
	        fi
	fi
        echo
        echo "Path to ENSIM service init script ?"
        if [ -f /etc/init.d/webppliance ]; then
         EWP_INIT="/etc/init.d/webppliance"
         echo "Found service init script at $EWP_INIT"
        else
         echo -n "[$EWP_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           EWP_INIT="$cc"
         fi
	fi
echo
fi

echo "Enable PGSQL service monitoring ? (true=enable, false=disable)"
echo -n "[$SERV_PGSQL]:$c"
read cc
if [ ! -z "$cc" ]; then
        SERV_PGSQL="$cc"
fi
echo
if [ "$SERV_PGSQL" = "true" ]; then
        echo "Name of the PGSQL service as appears in 'ps' ?"
        TMP_VAL=`ps -auxww | grep -w postmaster` >> /dev/null 2>&1
        if [ ! "$TMP_VAL" == "" ]; then
                PGSQL_NAME="postmaster"
                echo "Found service name as $PGSQL_NAME"
        else
	        echo -n "[$PGSQL_NAME]:$c"
        	read cc
	        if [ ! -z "$cc" ]; then
	          PGSQL_NAME="$cc"
	        fi
	fi
        echo
        echo "Path to PGSQL service init script ?"
        if [ -f /etc/init.d/postgresql ]; then
         PGSQL_INIT="/etc/init.d/postgresql"
         echo "Found service init script at $PGSQL_INIT"
        else
         echo -n "[$PGSQL_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           PGSQL_INIT="$cc"
         fi
        fi
echo
fi

if [ "$SERV_HTTP" == "true" ]; then
echo "The below are configuration options for Service Specific features:"
echo "press return to continue..."
read H

cat <<EOF
After an unclean HTTP shutdown, semaphore array's may remain allocated
and cause the service to fall into a looping restart cycle. Using this
feature clears semaphore arrays on HTTP restart.
EOF
echo "Enable semaphore cleanup ?"
echo -n "[$SP_SCLEAN]:$c"
read cc
if [ ! -z "$cc" ]; then
   SP_SCLEAN="$cc"
fi
echo

cat <<EOF
This is an implamented feature in the http module, its purpose is to
determine if/when the apache server locks up or otherwise stops
responding.
EOF
echo "Enable URL aware monitoring ?"
echo -n "[$SP_HTTP_URL]:$c"
read cc
if [ ! -z "$cc" ]; then
   SP_HTTP_URL="$cc"
fi
echo
if [ "$SP_HTTP_URL" = "true" ]; then
    echo "URL path to a local file ? (exclude HTTP://)"
    echo "Note: This URL should be valid and reside on the local server, otherwise"
    echo "HTTP will loop restarting"
    echo -n "[$URL]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       URL="$cc"
    fi
    echo
fi

cat <<EOF
HTTP log files can grow large and cause the service to crash
(segfault), this feature will keep the main HTTP logs incheck.
EOF
echo "Enable HTTP log monitor ?"
echo -n "[$SP_HTLOG]:$c"
read cc
if [ ! -z "$cc" ]; then
   SP_HTLOG="$cc"
fi
echo
if [ "$SP_HTLOG" = "true" ]; then
    echo "What is the location of your HTTP servers, log files ?"
    echo "(should point to a directory, not file)"
    echo -n "[$LPATH_HT]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       LPATH_HT="$cc"
    fi
    echo
    echo "Max size of HTTP log files, before cleared ? (value in MB)"
    echo -n "[$LSIZE_HT]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       LSIZE_HT="$cc"
    fi
    echo
fi

cat <<EOF
MySQL uses a /tmp symlink of its mysql.sock socket file. This
feature verifies that the symlink exists from the main mysql.sock
file, and if not it is recreated.
EOF
echo "Enable MySQL Socket correction ?"
echo -n "[$SP_MYSQLS]:$c"
read cc
if [ ! -z "$cc" ]; then
   SP_MYSQLS="$cc"
fi
echo
if [ "$SP_MYSQLS" = "true" ]; then
    echo "What is the location of your main mysql.sock file ?"
    if [ -e "/var/lib/mysql/mysql.sock" ]; then
	MYSQL_SOCK="/var/lib/mysql/mysql.sock"
    	echo "Found socket file at $MYSQL_SOCK"
    else
	echo -n "[$MYSQL_SOCK]:$c"
	read cc
	if [ ! -z "$cc" ]; then
	   MYSQL_SOCK="$cc"
	fi
    fi
    echo
    echo "What is the location of your /tmp symlink to mysql.sock ?"
    if [ -e "/tmp/mysql.sock" ]; then
	MYSQL_TMPSOCK=/tmp/mysql.sock
	echo "Found /tmp symlink of mysql.sock at $MYSQL_TMPSOCK"
    else
	echo -n "[$MYSQL_TMPSOCK]:$c"
	read cc
	if [ ! -z "$cc" ]; then
	   MYSQL_TMPSOCK="$cc"
	fi
    fi
    echo
fi

fi

echo "The below are configuration options for System modules:"
echo "press return to continue..."
read G

## System monitoring
echo "Enable NETWORK monitoring ? (true=enable, false=disable)"
echo -n "[$SYS_NET]:$c"
read cc
if [ ! -z "$cc" ]; then
        SYS_NET="$cc"
fi
echo

if [ "$SYS_NET" = "true" ]; then
        echo "interface to monitor ?"
        echo -n "[$IF]:$c"
        read cc
        if [ ! -z "$cc" ]; then
          IF="$cc"
        fi
        echo
        echo "Path to NETWORK init script ?"
        if [ -f /etc/init.d/network ]; then
         NET_INIT="/etc/init.d/network"
         echo "Found service init script at $NET_INIT"
        else
         echo -n "[$NET_INIT]:$c"
         read cc
         if [ ! -z "$cc" ]; then
           NET_INIT="$cc"
         fi
        fi
echo
fi

echo "Enable LOAD monitor ? (true=enable, false=disable)"
echo -n "[$SYS_LM]:$c"
read cc
if [ ! -z "$cc" ]; then
	SYS_LM="$cc"
fi
echo
if [ "$SYS_LM" = "true" ]; then
    echo "Load level before status condition 'warning' ?"
    echo -n "[$WARN_LD]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       WARN_LD="$cc"
    fi
    echo
    echo "Load level before status condition 'critical' ?"
    echo -n "[$CRIT_LD]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       CRIT_LD="$cc"
    fi
    echo
    echo "Enable a global (wall) message at status condition 'warning' & 'critical' ?"
    echo -n "[$GMSG]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       GMSG="$cc"
    fi
    echo
    echo "Renice services at status condition 'warning' or 'critical' ?"
    echo "(3 values - warn, crit, false - false=disabled)"
    echo -n "[$RPP]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       RPP="$cc"
    fi
    echo
    echo "Stop nonessential services at status condition 'warning' or 'critical' ?"
    echo "(3 values - warn, crit, false - false=disabled)"
    echo -n "[$STSRV_LM]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       STSRV_LM="$cc"
    fi
    echo
    echo "Reboot system on status condition 'warning' or 'critical' ?"
    echo "(3 values - warn, crit, false - false=disabled)"
    echo -n "[$RST_LM]:$c"
    read cc
    if [ ! -z "$cc" ]; then
       RST_LM="$cc"
    fi
    echo
fi

##
echo "Configuration completed, saving $CNF... "
}


gen() {
if [ ! -f $INSPATH/$CNF ]; then
   header
   echo "Unable to locate $INSPATH/$CNF, aborting."
   exit 1
fi
if [ ! -f $INSPATH/$CNF.default ]; then
   cp $INSPATH/$CNF $INSPATH/$CNF.default
fi

rm -f $INSPATH/$CNF
cat > $INSPATH/$CNF <<EOF
# SIM $V <sim@r-fx.org>
#
# NOTE: This file should be edited with word/line wrapping off,
#	if your using pico please start it with the -w switch.
#	(e.g: pico -w filename)
#
# [Main configuration]
#
# Here we configure our install path, log paths and some other 
# misc values. You should not need to edit this section, for most
# systems. At most possibly your SYSLOG value and SLOGSIZE value.

INSPATH="$INSPATH"        	                # the installed path of SIM
FUNCF="\$INSPATH/internals/functions.sim"   	     # sourced functions file
CNFMISC="\$INSPATH/internals/conf.misc"                  # sourced misc configuration
SIMLOG="$SIMLOG"      	                # log file path (should point to a file not directory)
SYSLOG="$SYSLOG"                	        # path to kernel log or equivilant
SLOGSIZE="$SLOGSIZE"                                       	# max size of SIM logfile before it is cleared (value in KB)

# [Alerts configuration]
#
# Alerts sent out upon events , are controled by the below values
# Be sure to edit the EMAIL value to include your email address

EMAIL="$EMAIL"                                    	# address/user to send alerts to
                                                	# seperate multiple addresses with ","
                                                	# e.g: EMAIL="root, mike@myisp.com, sms@att.net"
SUBJ="Status warning from \$HOSTNAME"            	# subject of email alerts
EVLIMIT="$EVLIMIT"                                        	# disable email alerts after N events (to avoid being spammed)
							#
LFEXP="3"                                               # expire lockfile after 3 minutes if stale
TAILSIML="15"                                        	# display N lines from the SIM log in alert email
TAILSYSL="15"                                        	# display N line from the kernel log in alert email

# [Service monitor configuration]
#
# Some global options related to service monitoring

RST="$RST"                                              # enabled auto-restart of downed services
LAXCHK="$LAXCHK"                                           # set laxed or strict service checks
DISRST="$DISRST"                                             # disable autorestart after N events
                                                        # event stats are cleared daily
INIT_ARG="restart"                                      # argument to pass to init scripts when service is down
                                                        
# This section is to enable or disable our Service checks
# The values are either a true/false statment (true=enable, false=disable)
# Service checks by default are disabled

SERV_FTP="$SERV_FTP"                                    	# FTP Service
SERV_HTTP="$SERV_HTTP"                                  	# HTTP Service
SERV_DNS="$SERV_DNS"                                    	# DNS Service
SERV_SSH="$SERV_SSH"                                        # SSH Service
SERV_MYSQL="$SERV_MYSQL"                                      # MySQL Service
SERV_PGSQL="$SERV_PGSQL"                                      # PGSQL Service
SERV_EWP="$SERV_EWP"                                        # Ensim Service
SERV_XINET="$SERV_XINET"                                      # XINET Sevice
SERV_SMTP="$SERV_SMTP"                                   # SMTP Service

# The below values are used to check against the processes list
# and make sure the given service is running

FTP_NAME="$FTP_NAME"                                  	# name of the FTP service as appears in 'ps'
HTTP_NAME="$HTTP_NAME"                                   	# name of HTTP service as appears in 'ps'
DNS_NAME="$DNS_NAME"                                    	# name of DNS service as appears in 'ps'
SSH_NAME="$SSH_NAME"                                         # name of SSH service as appears in 'ps'
MYSQL_NAME="$MYSQL_NAME"                                     # name of MySQL service as appears in 'ps'
PGSQL_NAME="$PGSQL_NAME"                                 # name of PGSQL service as appears in 'ps'
EWP_NAME="$EWP_NAME"                                     # name of Ensim service as appears in 'ps'
XINET_NAME="$XINET_NAME"                                     # name of XINET service as appears in 'ps'
SMTP_NAME="$SMTP_NAME"                                # name of SMTP service as appears in 'ps'

# The below values are used to check against the netstat output
# and make sure the given service is operating on the said port

FTP_PORT="$FTP_PORT"                                           # TCP/IP port for FTP
HTTP_PORT="$HTTP_PORT"                                          # TCP/IP port for HTTP
DNS_PORT="$DNS_PORT"                                           # TCP/IP port for DNS
SSH_PORT="$SSH_PORT"                                           # TCP/IP port for SSH
MYSQL_PORT="$MYSQL_PORT"                                       # TCP/IP port for MySQL
EWP_PORT="$EWP_PORT"                                        # TCP/IP port for Ensim
XINET_PORT="$XINET_PORT"                                        # TCP/IP port for an XINET service
SMTP_PORT="$SMTP_PORT"                                      # TCP/IP port for SMTP service

# The init paths are what is used to restart a given service
# ("start" is passed to the given init script)

FTP_INIT="$FTP_INIT"                      	# path to FTP service init script
HTTP_INIT="$HTTP_INIT"                       	# path to HTTP service init script
DNS_INIT="$DNS_INIT"                        	# path to DNS service init script
SSH_INIT="$SSH_INIT"                             # path to SSH service init script
MYSQL_INIT="$MYSQL_INIT"                         # path to MySQL service init script
PGSQL_INIT="$PGSQL_INIT"                     # path to PGSQL service init script
EWP_INIT="$EWP_INIT"                      # path to Ensim service init script
XINET_INIT="$XINET_INIT"                         # path to XINET service init script
SMTP_INIT="$SMTP_INIT"                    # path to SMTP service init script

# [Service specifics]
#
# Here we edit parameters for service specific features
# such as our HTTP log monitor. For the most part this
# is just integrated features in the service .chk files.
#

SP_HTTP_URL="$SP_HTTP_URL"                                     # true/false value to enable/disable url aware monitoring
SP_HTLOG="$SP_HTLOG"                                    	# true/false value to enable/disable HTTP log monitor
SP_MYSQLS="$SP_MYSQLS"                                       # true/false value to enable/disable MySQL Socket fix

# Semaphore array cleanup on HTTP restart
#
SP_SCLEAN="$SP_SCLEAN"                                       # true/false value to enable/disable semaphore array cleanups

# HTTP log size monitor
#
# Keep main apache log files incheck to avoid segfaults
# (crashing) if/when logs grow to large.

LPATH_HT="$LPATH_HT"                          	# the location of your HTTP servers log files
LSIZE_HT="$LSIZE_HT"                                      	# the max size of the log directory (value in MB)

# URL aware monitoring
#
# This is an implamented feature in the http module
# its purpose is to determine if/when  the apache
# server locks up or otherwise stops responding.

URL_PROT="$URL_PROT"                                         # protocol for url ? http one would hope
URL="$URL"                              # url path to a local file - do not include http://

# MySQL Socket correction
#
# MySQL uses a /tmp symlink of its mysql.sock socket
# file. This feature verifies that the symlink exists
# from the main mysql.sock file, and if not it is
# recreated.

MYSQL_SOCK="$MYSQL_SOCK"                  # main mysq.sock file
MYSQL_TMPSOCK="$MYSQL_TMPSOCK"                         # /tmp or equivalant symlink of the socket

# [System monitor configuration]
#
# This section is to enable or disable our System chk modules
# The values are either a true/false statment (true=enable, false=disable)
# System chk modules are disabled by default

SYS_LM="$SYS_LM"                                          # true/false value to enable/disable the load monitor
SYS_NET="$SYS_NET"						# true/false value to enable/disable network monitor

# Here we edit the parameters for our system load module
#
# This module will in due time be completely redone but for now
# it serves the purpose and works reasonable well....

WARN_LD="$WARN_LD"                                            # max load value before status warning (5 minute load avg.)
CRIT_LD="$CRIT_LD"                                            # max load value before status critical (5 minute load avg.)
GMSG="$GMSG"						# send message to all users asking that they logoff for a short period

# The following 2 options ($RPP & $STSRV_LM) are still
# considered to be somewhat experimental. They work
# inhouse for me but not much testing has been done
# with these features. Use at your own risk :P
# Note: use common sence and read the comments --
# e.g: setting STSRV_LM to run at load critical
# and having RST_LM reboot the server !?
#

RPP="$RPP"						# prioritize nonessential processes at load status warn or crit
                                                        # 3 values for RPP: warn, crit, or false
							#
STSRV_LM="$STSRV_LM"					# stop nonessential services at load status warn or crit
							# 3 values for STSRV_LM: warn, crit, or false

RST_LM="$RST_LM"                                          # reboot server at load status warn or crit
							# 3 values for RST_LM: warn, crit, or false
							#
DISCMD_WARN="$DISCMD_WARN"						# disable command execution ($WARN_CMD) after N runs
DISCMD_CRIT="$DISCMD_CRIT"						# disable command execution ($CRIT_CMD) after N runs
							# 
							# Features $GMSG, $RPP, $RST_LM, $STSRV_LM, $DISCMD_WARN, and
							# $DISCMD_CRIT are dependent on the preset command configurations
							# below, altering them will omit the above features.
							# 
WARN_CMD=". \$INSPATH/internals/chk/sys/lm/warn.lm"      # command to execute at load status warning
CRIT_CMD=". \$INSPATH/internals/chk/sys/lm/crit.lm"      # command to execute at load status critical

# Here we edit the parameters for our network monitor
#
# The network status is a fundamental aspect of keeping a server
# online, as such the network module makes an effort in doing so.
#
IF="$IF"						# interface name to watch
NET_INIT="$NET_INIT"				# network service init script
EOF
chmod 640 $INSPATH/$CNF
chown root $INSPATH/$CNF
echo "Done, $CNF saved to $INSPATH."
}

# Lets run our functions
header
config
gen
