#!/bin/sh
#
#    this script is used to install the 'server' portion of gabriel.
#
# Author: Richard Mahn
#
# Copyright 1995 by Los Altos Technologies Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms are
# permitted provided that this entire copyright notice is
# duplicated in all such copies.  No charge, other than "at-cost"
# distribution fee may be charged for copies, derivations, or
# distributions of this material without the expressed written consent
# of the copyright holders.
# 
# THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT ANY EXPRESSED
# OR IMPLIED WARRANTIES, INCLUDING, WITHOUT FURTHER LIMITATION, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
# PARTICULAR PURPOSE.
# 
# IN NO EVENT SHALL LOS ALTOS TECHNOLOGIES, INC., OR ANY OF ITS
# EMPLOYEES, REPRESENTATIVE, AGENTS, OFFICERS, OR DIRECTORS, OR ANY
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT
# LIMITED TO, LOSS OF USE, DATA, PROFITS, OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


####################
#  initialization  #
####################

initialize () {
    INSTALLED_FROM=`pwd`
    LOG_FILE="/var/log/gabriel.log"
    CONFIG_FILE="/etc/gabriel.conf"
    SYSLOG_CONF_FILE="/etc/syslog.conf"
    if [ -d /usr/local ] ; then
	GABRIEL_HOME="/usr/local/etc/gabriel-1.0"
    else
	GABRIEL_HOME="/usr/gabriel-1.0"
    fi
    if [ -x /usr/ucb/echo ] ; then
	echo_n="/bin/echo -n"
    else
	echo_n="echo -n"
    fi

    USAGE="$PROGRAM: [-v] [-i] [-c config_file] [-f inputfile]"
    LONG_USAGE="$USAGE
    where
	-h[elp]	this message
	-v	verbose
	-q	quiet
	-c	to specify the configuration file
	-l	to specify the log file
	-d	to specify the gabriel home directory
"
    verbose='n'
    quiet='n'
}

# full usage message
usage () {
    echo "$LONG_USAGE"
    exit 0
}

# error exit
exit_with_usage () {
    echo $USAGE 2>&1
    exit 1
}

# routine to output data, respecting the settings of verbose and quiet
#   first argument is 'a', 'n', 'v', for 'always' output it,
#   'normally' output it, or 'verbose' output
output () {
    _TYPE=$1; _MSG="$2";
    case $_TYPE in
	a)  echo "$_MSG"
	    ;;
	n)  if [ $quiet != 'y' ] ; then echo "$_MSG"; fi
	    ;;
	v)  if [ $verbose = 'y' ] ; then echo "$_MSG"; fi
	    ;;
	*)	# this shouldn't happen
	    ;;
    esac
}

# information blurb
credits () {
    echo "Gabriel was created by Los Altos Technologies, Inc."
    echo "See the file COPYRIGHT in the $INSTALLED_FROM directory"
}

# make sure we are root
ensure_root () {
    _ME=`id | sed 's/[^=]*=//;s/(.*//'`
    if [ $_ME -ne 0 ] ; then
	echo "You must be root to run this installation program"
	exit 1
    fi
}

# make sure (s)he wants to
check_if_okay () {
cat <<!
    Installing the server portion of Gabriel involves:
      1. Installing a configuration file as $CONFIG_FILE
      2. Adding a logfile called $LOG_FILE
      3. Adding a line to the syslog.conf file.
      4. Adding a cron entry to periodically run the software.
      5. Installing the software in the $GABRIEL_HOME directory.

!
    $echo_n "Do you want to do this (y/n) "
    read answer
    if [ "$answer" != 'y' ] ; then
	echo ""
	echo "Not installing the Gabriel server software."
	exit 0
    fi
}

# put in a configuration file
install_config_file () {
    if [ -f $CONFIG_FILE ] ; then
	echo "There is already a $CONFIG_FILE."
	echo "You must have already installed the gabriel server."
	exit 1
    fi
    output 'n' "installing configuration file in $CONFIG_FILE"
    cp gabriel.conf $CONFIG_FILE
}

# create a blank log file
create_log_file () {
    if [ -f $LOG_FILE ] ; then
	echo "$LOG_FILE already exists.  gabriel will use it"
    else
	output 'n' "creating log file $LOG_FILE"
	touch $LOG_FILE
    fi
}

# update the syslog file
update_syslog_file () {
    if grep -s gabriel $SYSLOG_CONF_FILE ; then
	echo "gabriel has already been added to $SYSLOG_CONF_FILE"
	echo "no further updates will be done"
    else
	output 'n' "updating the syslog file: $SYSLOG_CONF_FILE"
	_NEW=`date +"$SYSLOG_CONF_FILE%y%m%d"`
	mv $SYSLOG_CONF_FILE $_NEW
	cp -p $_NEW $SYSLOG_CONF_FILE
	echo "# following line added for gabriel" >> $SYSLOG_CONF_FILE
	echo "local3.info		$LOG_FILE" >> $SYSLOG_CONF_FILE
	if [ -f /etc/syslogd.pid ] ; then
	    kill -HUP `cat /etc/syslogd.pid`
	fi
    fi
}

# copy all the gabriel data
install_software_files () {
    if [ "$GABRIEL_HOME" = "$INSTALLED_FROM" ] ; then
	return
    fi
    if [ -d $GABRIEL_HOME ] ; then
	if [ -f "$GABRIEL_HOME/$CRON_DATA" ] ; then
	    echo "the software has already been installed in $GABRIEL_HOME"
	    echo "the current files there may be overwritten"
	fi
    else
	mkdir -p $GABRIEL_HOME
    fi
    output 'n' "copying the gabriel software to $GABRIEL_HOME"
    { cd $INSTALLED_FROM;
      cp -pr . $GABRIEL_HOME; }
}

# add an entry to the cron table
add_cron_entry () {
    crontab -l > $INSTALLED_FROM/old.crontab
    if crontab -l | grep -s $GABRIEL_HOME ; then
	echo "There is already a crontab entry for gabriel"
	echo "No further updates will be done."
	rm old.crontab
    else
	output 'n' "updating the crontab entry for root"
	cp $INSTALLED_FROM/old.crontab $INSTALLED_FROM/new.crontab
	echo "0,15,30,45 * * * * $GABRIEL_HOME/gabriel_server -c $CONFIG_FILE -f $LOG_FILE"  >> $INSTALLED_FROM/new.crontab
	crontab $INSTALLED_FROM/new.crontab
    fi

}

##############################
#  main program begins here  #
##############################

PROGRAM=$0
PATH="/bin:/usr/bin"

initialize

# parse arguments
while [ $# -ge 1 ] ; do
    case $1 in
	-v) verbose='y' ;;
	-q) quiet='q' ;;
	-h|-help) usage ;;
	-c)
	    if [ $# -lt 2 ] ; then
		exit_with_usage
	    fi
	    shift;
	    CONFIG_FILE=$1;
	    ;;
	-l)
	    if [ $# -lt 2 ] ; then
		exit_with_usage
	    fi
	    shift;
	    LOG_FILE=$1;
	    ;;
	-d)
	    if [ $# -lt 2 ] ; then
		exit_with_usage
	    fi
	    shift;
	    GABRIEL_HOME=$1;
	    ;;
	*)
	    exit_with_usage
	    ;;
    esac
    shift;
done

# check conflicting arguments
if [ $quiet = 'y' -a $verbose = 'y' ] ; then
    echo "$PGM: -q and -v are incompatible arguments"
    exit 1
fi

ensure_root

credits

check_if_okay

MSG=`date  +"installing gabriel at %T on %a %D"`
output 'a' "$MSG"

MSG="gabriel configuration file:	$CONFIG_FILE
gabriel log file:		$LOG_FILE
gabriel home directory:		$GABRIEL_HOME
"
output 'v' "$MSG"

install_config_file

create_log_file

update_syslog_file

install_software_files

add_cron_entry

exit 0;
