#!/bin/ksh

PATH=/usr/local/sysmansw/runtime/pgsql/bin:/usr/local/sysmansw/runtime/perl/bin:$PATH:/bin:/usr/bin:/sbin:/usr/sbin

LGFILE=ssw_install.log
LOGFILE=/tmp/$LGFILE

export currdir=`pwd`
export sw_user=""
export sw_group=""
export homedir=""
export sw_rt=""
export sw_pg=""
export sw_um=""
export sw_ct=""
export upgrade="0"
export hostname=`hostname`
export full_hostname=""
export system=`uname`
export systype=`uname`
if [ $system = "SunOS" ]
then
    system=$system-`uname -p`
fi

if [ $system = "Linux" ]
then
    system=$system-`uname -m`
fi

if [ `uname` = "Linux" ]
then
    ps='ps ax'
    pidnum=1
else
    ps='ps -ef'
    pidnum=2
fi

#########################################################################
#########################################################################
#########################################################################

create_sysmansw_cfg()
{
    cat << __EOF > /usr/local/sysmansw/sysmansw.cfg
#
# Sysman Software configuration file
#

#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
#
# User configurable options
#
# If apache should use port 80, change the port number below to 80 (or whatever
# port you like). Make sure to run rc.sysmansw as user root if the port number
# is below 1024!
#

HTTP_PORT=29170

# If you want to be able to connect more  than 10 clients, please increase the
# number below. Make sure to increase kernel parameters as well on Solaris
# or HP-UX systems(see the documentation)!

PG_MAX_CLIENTS=10

#
# End user configurable options
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
#

PATH=/usr/local/sysmansw/runtime/pgsql/bin:/usr/local/sysmansw/runtime/perl/bin:\$PATH

#Host configuration
HOST_HTTPD=$full_hostname
HOST_SYSMANSW=$hostname

#Postgres configuration
PGSQL_MAX_CONNS=\`echo "2*\$PG_MAX_CLIENTS+5" | bc\`
PGSQL_MAX_BUFS=\`echo "2*\$PGSQL_MAX_CONNS" | bc\`

#Port configuration
PORT_HTTPD=\$HTTP_PORT
PORT_PGSQL=29171
PORT_SYSMANSWD=29172
PORT_PERFGRAPH=29173
PORT_UPTIMETRICS=29174
PORT_CALLTRACK=29175

#User configuration
USER_SYSMANSW=$sw_user
GROUP_SYSMANSW=$sw_group
HOME_SYSMANSW=$homedir

#Native environment variable for Postgres
PGPORT=\$PORT_PGSQL

#Export them!
export PGSQL_MAX_CONNS PGSQL_MAX_BUFS
export HOST_HTTPD HOST_SYSMANSW
export PORT_HTTPD PORT_PGSQL
export PORT_SYSMANSWD
export PORT_PERFGRAPH PORT_UPTIMETRICS PORT_CALLTRACK
export USER_SYSMANSW

export PGPORT
export PATH
__EOF

    # Make sure it is called in .profile
    if [ -f $homedir/.profile ]
    then
        echo ". $homedir/sysmansw.cfg" > /tmp/ssw_$$
        cat $homedir/.profile >>  /tmp/ssw_$$
        mv -f /tmp/ssw_$$ $homedir/.profile
    else
        echo ". $homedir/sysmansw.cfg" > $homedir/.profile
    fi
    # For linux: if user has bash, add in .bash_profile
    if [ -f $homedir/.bash_profile ]
    then
        echo ". $homedir/sysmansw.cfg" > /tmp/ssw_$$
        cat $homedir/.bash_profile >>  /tmp/ssw_$$
        mv -f /tmp/ssw_$$ $homedir/.bash_profile
    fi

    chown $sw_user /usr/local/sysmansw/sysmansw.cfg
    chown $sw_user $homedir/.profile
    chmod +x $homedir/.profile
    chmod +x /usr/local/sysmansw/sysmansw.cfg
}

#########################################################################
#########################################################################
#########################################################################

check_version()
{
    old=$1
    new=`echo $2|perl -e '$ln = <STDIN>; $ln =~ /(\d\.\d.)/; print $1'`


    if [ "$old" = "" ]
    then
        upgrade="1"
        return
    fi

    upgrade=`echo "$old $new"|perl -e '
      chomp($oldnew=<STDIN>);
      ($old, $new) = split / /,$oldnew;
      $old =~ /(\d).(\d)(.*)/; $old_maj = $1; $old_min = $2; $old_let = $3;
      $new =~ /(\d).(\d)(.*)/; $new_maj = $1; $new_min = $2; $new_let = $3;
      if ($new_maj gt $old_maj) { print "1"; exit; }
      if ($new_maj eq $old_maj && $new_min gt $old_min) { print "1"; exit; }
      if ($new_maj eq $old_maj && $new_min eq $old_min && $new_let gt $old_let) { print "1"; exit; }
      print "0"
    '`
}

#########################################################################
#########################################################################
#########################################################################

install_rt()
{
    if [ ! -z "$sw_rt" ]
    then
        if [ -d /usr/local/sysmansw/runtime ]
        then
            echo "The runtime environment is already installed."
            check_version "`cat /usr/local/sysmansw/runtime/VERSION 2>/dev/null`" $sw_rt
            if [ $upgrade = "1" ]
            then
                echo "The version of $sw_rt is newer than your installed version."
                echo "Please check the documentation for upgrading information."
            fi
            echo
            return
        fi
        cd /usr/local/sysmansw
        echo "Unpacking runtime environment..."
        tar xf $currdir/$sw_rt
        chown -R $sw_user runtime
        if [ ! -f rc.sysmansw ]
        then
            mv -f runtime/rc.sysmansw .
        fi
        if [ ! -f sysmansw.cfg ]
        then
            create_sysmansw_cfg
        fi
        cd $currdir

        echo "Runtime environment installed!"
        echo
    fi
}

#########################################################################
#########################################################################
#########################################################################

install_pg()
{
    if [ ! -z "$sw_pg" ]
    then
        if [ -f /usr/local/sysmansw/perfgraph/perfgraphd ]
        then
            echo "PerfGraph is already installed."
            check_version "`cat /usr/local/sysmansw/perfgraph/VERSION 2>/dev/null`" $sw_pg
            if [ $upgrade = "1" ]
            then
                echo "The version of $sw_pg is newer than your installed version."
                echo "Please check the documentation for upgrading information."
            fi
            echo
            sw_pg=""
            return
        fi

        echo "Do you want to install PerfGraph? (y/n) [y] \c"
        read resp
        if [[ "$resp" != "" && $resp != "y" ]]
        then
            echo "Skipping PerfGraph..."
            echo
            sw_pg=""
            return
        fi

        if [ ! -d /usr/local/sysmansw/runtime ]
        then
            echo "Cannot find the runtime environment."
            echo "Please install the runtime environment first."
            exit
        fi

        cd /usr/local/sysmansw
        echo "Unpacking PerfGraph software..."
        tar xf $currdir/$sw_pg
        chown -R $sw_user perfgraph
        cd $currdir

        echo "PerfGraph installed!"
        echo
    fi
}

#########################################################################
#########################################################################
#########################################################################

install_um()
{
    if [ ! -z "$sw_um" ]
    then
        if [ -f /usr/local/sysmansw/uptimetrics/umfe ]
        then
            echo "Uptimetrics is already installed."
            check_version "`cat /usr/local/sysmansw/uptimetrics/VERSION 2>/dev/null`" $sw_um
            if [ $upgrade = "1" ]
            then
                echo "The version of $sw_um is newer than your installed version."
                echo "Please check the documentation for upgrading information."
            fi
            echo
            sw_um=""
            return
        fi

        echo "Do you want to install Uptimetrics? (y/n) [y] \c"
        read resp
        if [[ "$resp" != "" && $resp != "y" ]]
        then
            echo "Skipping Uptimetrics..."
            echo
            sw_um=""
            return
        fi

        if [ ! -d /usr/local/sysmansw/runtime ]
        then
            echo "Cannot find the runtime environment."
            echo "Please install the runtime environment first."
            exit
        fi

        cd /usr/local/sysmansw
        echo "Unpacking Uptimetrics software..."
        tar xf $currdir/$sw_um
        chown -R $sw_user uptimetrics
        cd $currdir

        echo "Uptimetrics installed!"
        echo
    fi
}

#########################################################################
#########################################################################
#########################################################################

install_ct()
{
    if [ ! -z "$sw_ct" ]
    then
        if [ -f /usr/local/sysmansw/calltrack/ctfe ]
        then
            echo "CallTrack is already installed."
            check_version "`cat /usr/local/sysmansw/calltrack/VERSION 2>/dev/null`" $sw_ct
            if [ $upgrade = "1" ]
            then
                echo "The version of $sw_ct is newer than your installed version."
                echo "Please check the documentation for upgrading information."
            fi
            echo
            sw_ct=""
            return
        fi

        echo "Do you want to install CallTrack? (y/n) [y] \c"
        read resp
        if [[ "$resp" != "" && $resp != "y" ]]
        then
            echo "Skipping CallTrack..."
            echo
            sw_ct=""
            return
        fi

        if [ ! -d /usr/local/sysmansw/runtime ]
        then
            echo "Cannot find the runtime environment."
            echo "Please install the runtime environment first."
            exit
        fi

        cd /usr/local/sysmansw
        echo "Unpacking CallTrack software..."
        tar xf $currdir/$sw_ct
        chown -R $sw_user calltrack
        cd $currdir

        echo "CallTrack installed!"
        echo
    fi
}

#########################################################################
#########################################################################
#########################################################################

install_client()
{
    if [[ ! -z "$sw_pg" || ! -z "$sw_um" ]]
    then
        if [ -f /usr/local/sysmansw/sysmanswd ]
        then
            echo "Client software is already installed."
            echo
            return
        fi

        cd /usr/local/sysmansw
        echo "Unpacking client software..."
        tar xf $currdir/$sw_cl
        chown -R $sw_user *
        cd $currdir

        echo "Client software installed!"
        echo
    fi
}

#########################################################################
#########################################################################
#########################################################################

start_postgres()
{
    pid=`$ps | grep postmaster | grep -v grep | grep sysmansw | awk "{print \\$$pidnum}"`
    if [[ "$pid" = "" && -f /usr/local/sysmansw/runtime/pgsql/bin/postmaster ]]
    then
        pg_was_running="no"
        rm -f /tmp/.s.PGSQL.$PORT_PGSQL
        su - $sw_user -c "/usr/local/sysmansw/runtime/pgsql/bin/postmaster \
                          -S -o -F -N 8 -B 16 -D /usr/local/sysmansw/runtime/pgsql/data"
    else
        pg_was_running="yes"
    fi
}

stop_postgres()
{
    if [ $pg_was_running = "no" ]
    then
        sleep 1
        pid=`$ps | grep postmaster | grep -v grep | grep sysmansw | awk "{print \\$$pidnum}"`
        if [[ "$pid" != "" && -f /usr/local/sysmansw/runtime/pgsql/bin/postmaster ]]
        then
            kill $pid
        fi
    fi
}

#########################################################################
#########################################################################
#########################################################################

license_pg()
{
    if [ ! -z "$sw_pg" ]
    then
        while true
        do
            echo
            echo "Installing license for PerfGraph."
            echo "Please enter the (demo-)license key [press enter to skip]: \c"
            read lic
            if [ "$lic" = "" ]
            then
                echo "You did not enter a license key. To enter a license key later,"
                echo "please run as user $sw_user:"
                echo "perfgraph/pgadmin -license <license key>"
                return
            fi
            start_postgres
            su - $sw_user -c "perfgraph/pgadmin -license $lic"
            ret=$?
            stop_postgres
            if [ $ret = 0 ]
            then
                echo "License accepted."
                return
            fi
            echo "Could not set license. Try again."
        done
    fi
}
#########################################################################
#########################################################################
#########################################################################

license_um()
{
    if [ ! -z "$sw_um" ]
    then
        while true
        do
            echo
            echo "Installing license for Uptimetrics."
            echo "Please enter the (demo-)license key [press enter to skip]: \c"
            read lic
            if [ "$lic" = "" ]
            then
                echo "You did not enter a license key. To enter a license key later,"
                echo "please run as user $sw_user:"
                echo "uptimetrics/umadmin -license <license key>"
                return
            fi
            su - $sw_user -c "uptimetrics/umadmin -license $lic"
            if [ $? = 0 ]
            then
                echo "License accepted."
                return
            fi
            echo "Could not set license. Try again."
        done
    fi
}
#########################################################################
#########################################################################
#########################################################################

license_ct()
{
    if [ ! -z "$sw_ct" ]
    then
        while true
        do
            echo
            echo "Installing license for CallTrack."
            echo "Please enter the (demo-)license key [press enter to skip]: \c"
            read lic
            if [ "$lic" = "" ]
            then
                echo "You did not enter a license key. To enter a license key later,"
                echo "please run as user $sw_user:"
                echo "calltrack/pgadmin -license <license key>"
                return
            fi
            start_postgres
            su - $sw_user -c "calltrack/ctadmin -license $lic"
            ret=$?
            stop_postgres
            if [ $ret = 0 ]
            then
                echo "License accepted."
                return
            fi
            echo "Could not set license. Try again."
        done
    fi
}

#########################################################################
#########################################################################
#########################################################################

check_package()
{
    case $system in
      SunOS-i386)  arch=solaris_x86
                   pg_arch=solaris
                   ;;
      SunOS-sparc) arch=solaris_sparc
                   pg_arch=solaris
                   ;;
      HP-UX)       arch=hpux_parisc
                   pg_arch=hpux
                   ;;
      Linux-i*)    arch=linux_x86
                   pg_arch=linux2_2
                   ;;
      *)           echo "Your hardware ($system) is not supported - please contact support@sysman.nl"
                   exit
                   ;;
    esac

    if [ -f rt-$arch-*.tar ]
    then
        sw_rt=`echo rt-$arch-*.tar`
    else
        if [ -f rt/rt-$arch-*.tar ]
        then
            sw_rt=`echo rt/rt-$arch-*.tar`
        fi
    fi

    if [ -f pg-server-$arch-*.tar ]
    then
        sw_pg=`echo pg-server-$arch-*.tar`
    else
        if [ -f pg/pg-server-$arch-*.tar ]
        then
            sw_pg=`echo pg/pg-server-$arch-*.tar`
        fi
    fi

    if [ -f um-server-$arch-*.tar ]
    then
        sw_um=`echo um-server-$arch-*.tar`
    else
        if [ -f um/um-server-$arch-*.tar ]
        then
            sw_um=`echo um/um-server-$arch-*.tar`
        fi
    fi

    if [ -f ct-server-$arch-*.tar ]
    then
        sw_ct=`echo ct-server-$arch-*.tar`
    else
        if [ -f ct/ct-server-$arch-*.tar ]
        then
            sw_ct=`echo ct/ct-server-$arch-*.tar`
        fi
    fi

    if [ -f ssw-client-$arch-*.tar ]
    then
        sw_cl=`echo ssw-client-$arch-*.tar`
    else
        if [ -f client/ssw-client-$arch-*.tar ]
        then
            sw_cl=`echo client/ssw-client-$arch-*.tar`
        fi
    fi

    echo "Found the following software:"
    if [ ! -z "$sw_rt" ]
    then
        echo "    Runtime:         $sw_rt"
        found="ok"
    fi
    if [ ! -z "$sw_pg" ]
    then
        echo "    PerfGraph:       $sw_pg"
        found="ok"
    fi
    if [ ! -z "$sw_um" ]
    then
        echo "    Uptimetrics:     $sw_um"
        found="ok"
    fi
    if [ ! -z "$sw_ct" ]
    then
        echo "    CallTrack:       $sw_ct"
        found="ok"
    fi

    if [ -z "$found" ]
    then
        echo "No software found that can be installed."
        echo "Expected something like rt-$arch-1.2.tar."
        echo "Please run this program either from the root directory of"
        echo "your cdrom, or from the directory with downloaded and unzipped"
        echo "software".
        echo
        exit
    fi
}

#########################################################################
#########################################################################
#########################################################################

check_symlink()
{
    if [ $homedir != /usr/local/sysmansw ]
    then
        echo
        echo "Creating symlink from /usr/local/sysmansw to $homedir"
        if [ ! -d /usr/local ]
        then
            mkdir /usr/local
        fi
        if [ -L /usr/local/sysmansw ]
        then
            rm -f /usr/local/sysmansw
        fi
        if [ -a /usr/local/sysmansw ]
        then
            echo "/usr/local/sysmansw already exists; please remove it,"
            echo "then restart the installation."
            exit
        fi
        ln -s $homedir /usr/local/sysmansw
        if [ $? != 0 ]
        then
            echo "Could not create the symlink, please try it yourself,"
            echo "then restart the installation."
            exit
        fi
    fi
}

#########################################################################
#########################################################################
#########################################################################

create_user()
{
    sw_user=$1
    echo
    echo "User '$sw_user' does not seem to exist."
    echo "I can create a user for you on this machine; however if you prefer"
    echo "to create a user yourself, answer n to the following question."
    echo "In a NIS environment, you may want to create the user yourself as well."
    echo
    echo "Do you want me to create user '$sw_user'? (y/n) [y] \c"
    read resp
    if [[ "$resp" != "" && $resp != "y" ]]
    then
        echo "Create user '$sw_user' first, then restart the installation."
        exit
    fi

    echo
    echo "I will create user '$sw_user' for you. By default the home directory is"
    echo "/usr/local/sysmansw. You can change this to be another directory, but"
    echo "then a symbolic link will be created from /usr/local/sysmansw to your"
    echo "home directory."
    echo
    echo "Home directory for '$sw_user'? [/usr/local/sysmansw] \c"
    read homedir
    if [ "$homedir" = "" ]
    then
        homedir=/usr/local/sysmansw

        # Remove possible symlink
        if [ ! -d /usr/local ]
        then
            mkdir /usr/local
        fi
        if [ -L /usr/local/sysmansw ]
        then
            rm -f /usr/local/sysmansw
        fi
    fi

    echo "Adding user..."
    useradd -d $homedir -s /bin/ksh -c "Sysman Software" -m $sw_user
    if [ $? != 0 ]
    then
        echo "Could not add user '$sw_user' - please create user yourself,"
        echo "then restart the installation."
        exit
    else
        echo "User added."
    fi
}

#########################################################################
#########################################################################
#########################################################################

check_sysmansw()
{
    if [ -d `echo ~sysmansw` ]
    then
        homedir=`echo ~sysmansw`
        echo "User sysmansw already exists."
        echo "Do you want to install the software as this user? (y/n) [y] \c"
        read resp
        if [[ "$resp" != "" && $resp != "y" ]]
        then
            echo "As what user should the software be installed? \c"
            read sw_user
            homedir=`ksh -c "echo ~$sw_user"`
            if [ ! -d $homedir ]
            then
                create_user $sw_user
            else
                echo
                echo "The software will be installed as user '$sw_user'"
                homedir=`ksh -c "echo ~$sw_user"`
            fi
        else
            sw_user=sysmansw
            homedir=`ksh -c "echo ~$sw_user"`
        fi
    else
        echo "User sysmansw does not exist."
        echo "Do you want to install the software as user sysmansw (recommended)? (y/n) [y] \c"
        read resp
        if [[ "$resp" != "" && $resp != "y" ]]
        then
            echo "As what user should the software be installed? \c"
            read sw_user
            homedir=`ksh -c "echo ~$sw_user"`
            if [ ! -d $homedir ]
            then
                create_user $sw_user
            fi
        else
            create_user sysmansw
        fi
    fi
    check_symlink

    sw_group=`su - $sw_user -c "echo;id"|grep gid|awk '{print $2}'|awk -F"(" '{print $2}'|awk -F")" '{print $1}'`
}

#########################################################################
#########################################################################
#########################################################################

configure_rt()
{
    echo "------------------------------------------"
    echo "Start configuration of runtime environment"
    if [ ! -d /usr/local/sysmansw/runtime/pgsql/data ]
    then
        echo
        echo "The initial database will be created; some messages will be displayed."
        echo
        mkdir /usr/local/sysmansw/runtime/pgsql/data
        chown $sw_user /usr/local/sysmansw/runtime/pgsql/data
        su - $sw_user -c "initdb --pglib=/usr/local/sysmansw/runtime/pgsql/lib \
                                 --pgdata=/usr/local/sysmansw/runtime/pgsql/data \
                                 --username=$sw_user"
    fi

    if [[ -f $homedir/perfgraph/web/pg.cgi && ! -a /usr/local/sysmansw/runtime/apache/cgi-bin/pg.cgi ]]
    then
        ln -s $homedir/perfgraph/web/pg.cgi /usr/local/sysmansw/runtime/apache/cgi-bin/pg.cgi
    fi

    if [[ -f $homedir/uptimetrics/web/um.cgi && ! -a /usr/local/sysmansw/runtime/apache/cgi-bin/um.cgi ]]
    then
        ln -s $homedir/uptimetrics/web/um.cgi /usr/local/sysmansw/runtime/apache/cgi-bin/um.cgi
    fi

    if [[ -f $homedir/calltrack/web/ct.cgi && ! -a /usr/local/sysmansw/runtime/apache/cgi-bin/ct.cgi ]]
    then
        ln -s $homedir/calltrack/web/ct.cgi /usr/local/sysmansw/runtime/apache/cgi-bin/ct.cgi
    fi

    ln -s $homedir/perfgraph/doc $homedir/runtime/apache/htdocs/pgdoc
    ln -s $homedir/uptimetrics/doc $homedir/runtime/apache/htdocs/umdoc
    ln -s $homedir/calltrack/doc $homedir/runtime/apache/htdocs/ctdoc

    echo "End configuration of runtime environment"
    echo "----------------------------------------"
}

#########################################################################
#########################################################################
#########################################################################

start_sw()
{
    echo "--------------------"
    echo "Start start software"
    su - $sw_user -c "/usr/local/sysmansw/rc.sysmansw restart"
    echo "End start software"
    echo "------------------"
}

#########################################################################
#########################################################################
#########################################################################

init_pg()
{
    if [ ! -z "$sw_pg" ]
    then
        su - $sw_user -c "perfgraph/pgadmin -s $hostname $pg_arch"
    fi
}

#########################################################################
#########################################################################
#########################################################################

init_um()
{
    if [ ! -z "$sw_um" ]
    then
        su - $sw_user -c "uptimetrics/umadmin -s ${hostname}_up \
                         '$hostname up' $hostname ping localhost"
    fi
}

#########################################################################
#########################################################################
#########################################################################

do_cron()
{
    echo "#---------------------------------------" > $homedir/$sw_user.cron
    echo "#" >> $homedir/$sw_user.cron
    if [ ! -z "$sw_pg" ]
    then
        echo "#Crontab entries for PerfGraph" >> $homedir/$sw_user.cron
        if [ "$systype" = "HP-UX" ]
        then
            echo "* * * * * $homedir/perfgraph/perfgather.ksh" >> $homedir/$sw_user.cron
        fi
        echo "30 3 * * * . $homedir/sysmansw.cfg; $homedir/perfgraph/pgadmin -dbcleandump" >> $homedir/$sw_user.cron
        echo "#" >> $homedir/$sw_user.cron
    fi

    if [ ! -z "$sw_um" ]
    then
        echo "#Crontab entries for Uptimetrics" >> $homedir/$sw_user.cron
        echo "59 * * * * sleep 15; $homedir/uptimetrics/collate.pl" >> $homedir/$sw_user.cron
        echo "30 3 * * * . $homedir/sysmansw.cfg; $homedir/uptimetrics/cleanup.ksh" >> $homedir/$sw_user.cron
        echo "* * * * * $homedir/uptimetrics/umclient.ksh" >> $homedir/$sw_user.cron
        echo "#" >> $homedir/$sw_user.cron
    fi

    if [ ! -z "$sw_ct" ]
    then
        echo "#Crontab entry for CallTrack" >> $homedir/$sw_user.cron
        echo "15 3 * * * . $homedir/sysmansw.cfg; $homedir/calltrack/ctadmin -dbdump" >> $homedir/$sw_user.cron
        echo "#" >> $homedir/$sw_user.cron
    fi
    echo "#---------------------------------------" >> $homedir/$sw_user.cron
    echo
    echo "The file $homedir/$sw_user.cron has been created with crontab entries"
    echo "for user $sw_user. Please put those entries the crontab of user $sw_user."

    if [ ! -z "$sw_pg" ]
    then
        if [[ "$systype" = "SunOS" || "$systype" = "HP-UX" ]]
        then
           if [ "$systype" = "SunOS" ]
           then
               sysuser=sys
           else
               sysuser=root
           fi
           echo "#---------------------------------------" > $homedir/$sysuser.cron
           echo "#Sar crontab entries for PerfGraph" >> $homedir/$sysuser.cron
           echo "* * * * * /usr/lib/sa/sa1" >> $homedir/$sysuser.cron
           echo "1 1-23 * * * $homedir/perfgraph/clean_sars.ksh" >> $homedir/$sysuser.cron
           echo "#" >> $homedir/$sysuser.cron
           echo "#---------------------------------------" >> $homedir/$sysuser.cron

           echo
           echo "The file $homedir/$sysuser.cron has been created with crontab entries"
           echo "for user $sysuser. Please put those entries the crontab of user $sysuser."
        fi
    fi
}

#########################################################################
#########################################################################
#########################################################################

get_hostname()
{
    hosts=""
    while read line
    do
        hosts=`echo $line | grep -v '^ *#' | grep "[ 	]$hostname\$"`
        if [ "$hosts" != "" ]
        then
            break
        fi

        hosts=`echo $line | grep -v '^ *#' | grep "[ 	]$hostname[ 	]"`
        if [ "$hosts" != "" ]
        then
            break
        fi

        hosts=`echo $line | grep -v '^ *#' | grep "[ 	]$hostname[.]"`
        if [ "$hosts" != "" ]
        then
            break
        fi
    done < /etc/hosts

    if [ "$hosts" = "" ]
    then
        echo "Your hostname is '$hostname' as reported by the command hostname."
        echo "However, this name is not available in the /etc/hosts file."
        echo "Please make sure the hostname is availble in the /etc/hosts file"
        echo "and then restart the installation"
        echo
        exit
    fi

    full_hostname=""
    if [ "`echo $hostname | grep '[.]'`" = "" ]
    then
        set $line
        ip=$1
        shift
        for host in $*
        do
            full_hostname=`echo $host | grep "^$hostname[.]"`
            if [ "$full_hostname" != "" ]
            then
                break
            fi
        done

        if [ "$full_hostname" = "" ]
        then
            echo "Cannot find your fully qualified hostname (hostname + domainname). Please enter"
            echo "your hostname + domainname (e.g. dbserver.company.com; if you don't have a"
            echo "domainname, just enter the hostname): \c"
            read full_hostname
        fi
    else
        set $line
        ip=$1
        full_hostname=$hostname
        hostname=${hostname%%.*}

        # Verify that de unqualified hostname is in /etc/hosts
        for host in $*
        do
            try=`echo $host | grep "^$hostname$"`
            if [ "$try" != "" ]
            then
                break
            fi
        done

        if [ "$try" = "" ]
        then
            echo "Cannot find your unqualified hostname (hostname without domainname). Please add"
            echo "your unqualified hostname in /etc/hosts as an alias on the following line"
            echo "in /etc/hosts:"
            echo
            echo "$line"
            echo
            echo "After that, restart the installation."
            echo
            exit 1
        fi
    fi

    echo "Host information:"
    echo "    IP address:      $ip"
    echo "    Hostname:        $hostname"
    echo "    Full hostname:   $full_hostname"
    echo
}

#########################################################################
#########################################################################
#########################################################################

> $LOGFILE
echo
echo
echo
echo
echo
echo "+----------------------------------------------------------------------+"
echo "|          Welcome to the Sysman Software Installation program!        |"
echo "|     This script will guide you through the setup of the software.    |"
echo "+----------------------------------------------------------------------+"
echo
echo "The installation consists of the following steps:"
echo "    1. Verification of your environment"
echo "    2. Select / create a user under which the software will run"
echo "    3. Install the software"
echo "    4. Enter license keys"
echo "    5. Start the software"
echo "    6. Create crontab entries"
echo
echo "[enter to continue]\c"
read resp
echo
echo "+----------------------------------------------------------------------+"
echo "| Step 1: Verification of your environment                             |"
echo "+----------------------------------------------------------------------+"
echo
if [ "`id|grep root`" = "" ]
then
    echo "*** Please switch to user root, then restart the installation."
    exit
fi
get_hostname
check_package
echo
echo "Step 1 complete [enter to continue]\c"
read resp
echo
echo "+----------------------------------------------------------------------+"
echo "| Step 2: Define / create user                                         |"
echo "+----------------------------------------------------------------------+"
echo
check_sysmansw
echo
echo "Step 2 complete [enter to continue]\c"
read resp
echo
echo "+----------------------------------------------------------------------+"
echo "| Step 3: Install the software                                         |"
echo "+----------------------------------------------------------------------+"
echo
install_rt
install_pg
install_um
install_ct
install_client
echo "The software has been installed, basic configuration will begin now."
echo "Please wait... \c"
configure_rt >> $LOGFILE 2>&1
echo
echo "Done!"
echo
echo "Step 3 complete [enter to continue]\c"
read resp
echo
echo "+----------------------------------------------------------------------+"
echo "| Step 4: Enter license keys                                           |"
echo "+----------------------------------------------------------------------+"
echo
license_pg
license_um
license_ct
echo
echo "Step 4 complete [enter to continue]\c"
read resp
echo
echo "+----------------------------------------------------------------------+"
echo "| Step 5: Start the software                                           |"
echo "+----------------------------------------------------------------------+"
echo
echo "Please wait... \c"
start_sw < /dev/null >> $LOGFILE 2>&1
init_pg  < /dev/null >> $LOGFILE 2>&1
init_um  < /dev/null >> $LOGFILE 2>&1
echo
echo "Done!"
echo
echo "Step 5 complete [enter to continue]\c"
read resp
echo
echo "+----------------------------------------------------------------------+"
echo "| Step 6: Create crontab entries                                       |"
echo "+----------------------------------------------------------------------+"
echo
do_cron
echo
echo "Step 6 complete [enter to continue]\c"
read resp
echo
echo "+----------------------------------------------------------------------+"
echo "      Now in case of a default installation, point your browser to      "
echo "                      http://$full_hostname:29170"
echo "+----------------------------------------------------------------------+"
echo
echo "[enter to continue]\c"
read resp
echo
echo "--> For this system, the client software already has been installed."
echo "--> Please refer to the documentation on how to add other clients!"
echo
echo "[enter to continue]\c"
read resp
echo
echo "--> The initial userid for CallTrack and Uptimetrics is"
echo "    admin, without a password."
echo
echo "[enter to continue]\c"
read resp
echo
mv $LOGFILE $homedir
chown $sw_user:$sw_group $homedir
echo "--> Please check the file $homedir/$LGFILE for error messages."
echo "    In case of problems, please send a message to support@sysman.nl"
echo
