#!/bin/sh
################################################################################
#
#     Script: CS_cims_install
# 
#     The information in this  document is subject to  change  without  notice
#     and should not be construed as a commitment by CIMS Lab, Inc.
#     CIMS Lab, Inc.  assumes no responsibility for any errors that
#     may appear in this document.
#
#     The software described in this  document is  furnished to the  purchaser
#     under  a license for use on a single  computer  system and can be copied
#     (with inclusion of CIMS Lab's copyright) only for use in such system, 
#     except as  may  otherwise be provided  in writing  from CIMS Lab, Inc.
#
#     All rights reserved.  No part  of this document may be reproduced in any
#     form  or  by any  means,  except as may otherwise be provided in writing
#     from CIMS Lab, Inc.
# 
#     Copyright (c) 1979 - 2005 by
# 
#     CIMS Lab, Inc., Laurel Lab
#     14440 Cherry Lane Court		Telephone:  (301)-725-2500
#     Suite 201			Fax      :  (301)-725-7196
#     Laurel, MD 20707
#     USA
#
#  CS_cims_install is the script used to install the CIMS/UNIX Server. 
#
#  CS_cims_install Development History
# 
# 	Original Author   : George C. Bonifant, CIMS Lab, Inc.
# 	Creation Date     : 18-Feb-2005
# 	Last Modification : 16-Aug-2005
# 	Version Number    : V1.2
# 
#  CS_cims_install Modification Log
# 
#	GCB=George C. Bonifant, CIMS Lab, Inc., Laurel Lab
# 
# 	GCB V1.1 18-Feb-2005
# 
# 		Created script.
#
#	GCB V1.2 16-Aug-2005
#
#		Support for running under OS/390 Unix System Services.
#
################################################################################
#

################################################################################
# CS_cims_install initialization...
################################################################################
#
DEF_DEST="/usr/cims"
UPGRADE="N"
rundir=`pwd`
ECHO_N="echo "
ECHO_C="\\c"

export ECHO_N ECHO_C

PLATFORM=`uname -a | awk '{print $1}'`

################################################################################
# CS_cims_install code begins here...
################################################################################
#
now=`date`
echo "
********************************************************************************
Starting CIMS Server for Unix $0 Script at ${now}
********************************************************************************
"

################################################################################
# Make sure we are really root
################################################################################
#
this_uid=-1
if [ -r /usr/bin/id ]		# Most systems have id command
then
    this_uid=`/usr/bin/id -u`
elif [ -r /bin/id ]             # Most systems have id command
then
    this_uid=`/bin/id -u`
fi

if [ "$this_uid" != "0" ]
then
    echo ""
    echo "$0: ERROR **** $0: installation procedure must be executed by root"
    echo ""
    echo "$0: Exiting ..."
    exit 1
fi

echo '
This installation script automatically installs CIMS Server for Unix. You
will be prompted for various pieces of information throughout. 
Default answers are denoted between square brackets ([]).  You may quit from 
this script at any prompt which asks a "y"/"n" question.  CIMS Server for 
Unix may partially install depending on the point at which you quit.

This script assumes that you have read the CIMS Server for Unix documentation
If you have not, it is recommended even if you are reinstalling or upgrading.  

During this installation, you will be prompted for the following information

    - Location to install CIMS Server for Unix.
    - Path to the Java JRE bin directory.
    - If Perl is not accessable as /usr/bin/perl, you will 
      be prompted if you wish to create a link from /usr/bin/perl 
      to the location of perl on this machine. You will need to 
      know the full path to the perl executable.

If you are not prepared to respond to these prompts, quit now...

'


$ECHO_N "Press enter when ready or \"q\" to quit: $ECHO_C" 

read dummy

: ${dummy:="Continue"}

if [ "$dummy" = "q" ]
then
    echo "
$0: Exiting at user request..."
    exit
fi

################################################################################
# See if CIMS/UNIX is already installed
################################################################################
#
if [ -f /etc/cims.conf ]	# cims.conf exists if CIMS previously installed
then
    cims_dir=`awk -F= '/CIMS_HOME/ {print $2}' /etc/cims.conf`
    DEF_DEST=$cims_dir
    if [ -r ${cims_dir}/config/cims.par ]
    then

	echo "
It looks like CIMS Server for Unix is installed on your system in ${DEF_DEST}.
If this is the case, you should backup your current installation before 
proceeding.

"
    fi
    while true
    do
        echo ""
        $ECHO_N "Are you installing an upgrade to CIMS/UNIX Server[n]? $ECHO_C"
        read answer
        : ${answer:="n"}
        case $answer in 
          n|N)
	    break
	    ;;
          y|Y) 
	    UPGRADE="Y"
	    break
	    ;;
          q|Q|ex)
	    echo ""
	    echo "$0: Exiting $0 at user request"
	    echo ""
	    exit 1
	    ;;
          *)
	    echo "
Please answer y or n or q"
	    continue
	    ;;
        esac
    done
fi

################################################################################
# Determine the desired location of CIMS/UNIX Server and the location of the
# CIMS Server for Unix distribution file
################################################################################
#
while true
do
    echo ""
    $ECHO_N "Do you want CIMS Server for Unix installed in $DEF_DEST [y]? $ECHO_C"
    read answer
    : ${answer:="y"}	# default to yes
    case $answer in
      n|N)
	echo ""
	$ECHO_N "Enter destination directory [$DEF_DEST]: $ECHO_C"
	read destination
	: ${destination:=$DEF_DEST}	# Use default if not specified

	echo ""
	$ECHO_N "Please verify destination directory as $destination [y]: $ECHO_C"
	read answer
	: ${answer:="y"}		# default to yes
	case $answer in
	  n|N)
	    continue			# Loop Back
	    ;;
	  y|Y)				# skip to check directory
	    ;;
	  q|Q|ex)
	    echo ""
	    echo "$0: Exiting $0 at user request"
	    echo ""
	    exit 1
	    ;;
	  *)
	    echo ""
	    echo "
Please answer y or n or q"
	    continue			# Loop Back
	    ;;
	esac
	;;
      y|Y)
	destination=${DEF_DEST}
	;;
      q|Q|ex)
	echo ""
	echo "$0: Exiting $0 at user request"
	echo ""
	exit 1
	;;
      *)
	echo ""
	echo "
Please answer y or n or q"
	continue
	;;
    esac

    export destination
    server_dir=${destination}
    export server_dir

    if test ! -r "${destination}"
    then
	echo ""
	echo "WARNING **** $0: ${destination} does not exist"

	echo ""
	$ECHO_N "Do you want to create $destination [y]: $ECHO_C"
	read answer
	: ${answer:="y"}		# default to yes
	case $answer in
	  n|N)
	    continue			# Loop Back
	    ;;
	  y|Y)
	    echo ""
	    echo "$0: Creating directory ${destination}"
	    mkdir ${destination}
	    ;;
	  q|Q|ex)
	    echo ""
	    echo "$0: Exiting $0 at user request"
	    echo ""
	    exit 1
	    ;;
	  *)
	    echo ""
	    echo "
Please answer y or n or q"
	    continue			# Loop Back
	    ;;
	esac
    fi

    if test -d "${destination}"
    then
	chmod 0775 ${destination}
	break
    else
	echo ""
	echo "$0: ERROR **** $0: ${destination} could not be created"
	echo ""
	exit 1
    fi

done

export destination

################################################################################
# Get the name of the directory where the distribution files were unloaded.
################################################################################
#
while true
do
    echo ""
    $ECHO_N "Enter directory containing CIMS Server for Unix distribution files [$rundir]: $ECHO_C"
    read dist_dir
    : ${dist_dir:=$rundir}		# Use destination if not specified
    if test ! -d "${dist_dir}"
    then
	echo ""
	echo "$0: ${dist_dir} does not exist, please try again..."
	continue
    else
	break
    fi
done

export	dist_dir

################################################################################
# Check to make sure distribution is ready; if not, exit...
################################################################################
#
if test "$PLATFORM" = "OS/390"
then
    dist_files1="CS_zOS_dist.tar"
else
    dist_files1="CS_dist.tar"
fi

for DIST_FILE in ${dist_files1}
do
    test -f "${dist_dir}/$DIST_FILE"
    if test "$?" != "0"
    then
	echo ""
	echo "$0: ERROR **** distribution file $DIST_FILE missing."
	echo ""
	exit 1
    fi
done


################################################################################
# Get the CIMS/UNIX user (UID) and group (GID)
################################################################################
#
echo "

Provide the username and group of the account that is to maintain CIMS/UNIX Server.
The default is root and 0, respectively.  If you provide any other user and group, 
the user account and group must be established.
"

echo ""
$ECHO_N "Enter username of account that maintains CIMS/UNIX [root]: $ECHO_C"
read CIMS_ACCOUNT
: ${CIMS_ACCOUNT:="root"}		# default to root

echo ""
$ECHO_N "Enter the group to which the username belongs [0]: $ECHO_C"
read CIMS_GROUP
: ${CIMS_GROUP:="0"}			# default to 0

export CIMS_ACCOUNT CIMS_GROUP

# Just to be sure!!!
if test $destination = "/" -o $destination = ""
then
    echo ""
    echo "$0: ERROR **** invalid destination directory"
    echo ""
    exit 1
fi

################################################################################
# Create CIMS/UNIX Log Directory if needed
################################################################################
#
CIMS_LOG_DIR=$destination/log
export CIMS_LOG_DIR
if test ! -d "$CIMS_LOG_DIR"
then
    echo ""
    echo "$0: Creating directory ${CIMS_LOG_DIR}"
    mkdir $CIMS_LOG_DIR
    chmod 775 $CIMS_LOG_DIR
    chown $CIMS_ACCOUNT:$CIMS_GROUP $CIMS_LOG_DIR
fi

################################################################################
# Create CIMS/UNIX Log tmp Directory if needed
################################################################################
#
CIMS_LOGTMP_DIR=$CIMS_LOG_DIR/tmp
export CIMS_LOGTMP_DIR
if test ! -d "$CIMS_LOGTMP_DIR"
then
    echo ""
    echo "$0: Creating directory ${CIMS_LOGTMP_DIR}"
    mkdir $CIMS_LOGTMP_DIR
    chmod 775 $CIMS_LOGTMP_DIR
    chown $CIMS_ACCOUNT:$CIMS_GROUP $CIMS_LOGTMP_DIR
fi

################################################################################
# Copy the distribution file to the appropriate CIMS/UNIX 
#  Server Home directory and unpack.
################################################################################
#
echo ""
echo "$0: Copying files to ${destination} directory ..."

chmod 775 ${destination}
cd ${destination}
if test "$PLATFORM" = "OS/390"
then
    cp ${dist_dir}/CS_zOS_dist.tar ${destination} 
    echo ""
    echo "$0: Extracting CIMS/UNIX Server files ..."
    tar xvf CS_zOS_dist.tar
    rm CS_zOS_dist.tar
else
    cp ${dist_dir}/CS_dist.tar ${destination} 
    echo ""
    echo "$0: Extracting CIMS/UNIX Server files ..."
    tar xvf CS_dist.tar
    rm CS_dist.tar
fi


################################################################################
# Create /etc/cims.conf 
################################################################################
#
echo ""
echo "$0: Creating the CIMS Server for Unix Configuration file, /etc/cims.conf"
echo "CIMS_HOME=${destination}" > /etc/cims.conf
if test ! -f /etc/cims.conf
then
    echo ""
    echo "$0: ERROR **** could not create /etc/cims.conf"
    echo ""
    exit 1
else
    chmod 664 /etc/cims.conf
fi

################################################################################
# If 
#	nightly.sh 
#	nightly.pl 
#	procCIMS.pl 
#	loadCIMS.pl
#	loadCIMS.sh
# do not already exist, create them
################################################################################
#
if test ! -f ${destination}/scripts/nightly.sh
then
    cp ${destination}/scripts/sample_nightly.sh ${destination}/scripts/nightly.sh
fi

if test ! -f ${destination}/scripts/nightly.pl
then
    cp ${destination}/scripts/sample_nightly.pl ${destination}/scripts/nightly.pl
fi

if test ! -f ${destination}/scripts/procCIMS.pl
then
    cp ${destination}/scripts/sample_procCIMS.pl ${destination}/scripts/procCIMS.pl
fi

if test ! -f ${destination}/scripts/loadCIMS.pl
then
    cp ${destination}/scripts/sample_loadCIMS.pl ${destination}/scripts/loadCIMS.pl
fi

if test ! -f ${destination}/scripts/loadCIMS.sh
then
    cp ${destination}/scripts/sample_loadCIMS.sh ${destination}/scripts/loadCIMS.sh
fi

################################################################################
# Set file permissions and ownership
################################################################################
#
echo ""
echo "$0: Setting file permissions/ownership for binaries and scripts ..."

chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/bin
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/bin/*
chown -R ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/sample_collectors
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/config
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/config/*
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/jlib
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/jlib/*
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/lib
chown -R ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/lib/*
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/log
chown -R ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/sample_processes
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/scripts
chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/scripts/*

chmod 0775 ${destination}/*
chmod 0664 ${destination}/config/*
chmod 0770 ${destination}/bin/*
chmod 0770 ${destination}/scripts/*
chmod 0770 ${destination}/jlib/*
chmod 0770 ${destination}/sample_collectors/*
chmod 0770 ${destination}/sample_collectors/*/*.pl
chmod 0770 ${destination}/sample_processes/*
chmod 0770 ${destination}/sample_processes/*/*.pl
chmod 0664 ${destination}/sample_processes/*/*.xml
chmod 0664 ${destination}/sample_processes/*/*.txt
chmod -R 0770 ${destination}/lib/*

################################################################################
# If there is no collectors directory, create one from sample_collectors
################################################################################
#
if test ! -d ${destination}/collectors
then
    echo ""
    echo "$0: Creating collectors directory from sample_collectors"
    cp -r ${destination}/sample_collectors ${destination}/collectors
    chown -R ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/collectors
    chmod 0770 ${destination}/collectors/*
    chmod 0770 ${destination}/collectors/*/*.pl
fi

################################################################################
# If there is no processes directory, create one from sample_processes
################################################################################
#
if test ! -d ${destination}/processes
then
    echo ""
    echo "$0: Creating processes directory from sample_processes"
    cp -r ${destination}/sample_processes ${destination}/processes
    chown -R ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/processes
    chmod 0770 ${destination}/processes/*
    chmod 0770 ${destination}/processes/*/*.pl
    chmod 0664 ${destination}/processes/*/*.xml
    chmod 0664 ${destination}/processes/*/*.txt
fi

################################################################################
# Create cims.par 
################################################################################
#
if test ! -r ${destination}/config/cims.par
then
    if test "$PLATFORM" = "OS/390"
    then
        $ECHO_N "Enter Java JRE bin directory path [/usr/lpp/java/IBM/J1.4/bin/]: $ECHO_C"
        read JAVABINDIR
        : ${JAVABINDIR:="/usr/lpp/java/IBM/J1.4/bin/"}		# default to /usr/lpp/java/IBM/J1.4/bin/
    else
        $ECHO_N "Enter Java JRE bin directory path [/opt/java1.4/jre/bin/]: $ECHO_C"
        read JAVABINDIR
        : ${JAVABINDIR:="/opt/java1.4/jre/bin/"}		# default to /opt/java1.4/jre/bin/
    fi

    echo ""
    echo "dbConfigurationFile=CIMSDBConfig.xml" > ${destination}/config/cims.par
    echo "defaultDataSourceId=CIMSServer" >> ${destination}/config/cims.par
    echo "LicenseKey=XXXXXXXXXXXX" >> ${destination}/config/cims.par
    echo "javapath=$JAVABINDIR" >> ${destination}/config/cims.par
    echo "processesdir=${destination}/processes" >> ${destination}/config/cims.par

    chown ${CIMS_ACCOUNT}:${CIMS_GROUP} ${destination}/config/cims.par
    chmod 660 ${destination}/config/cims.par
fi


################################################################################
# Create link to perl if needed.
################################################################################
#
if test ! -e /usr/bin/perl
then
    echo ""
    $ECHO_N "/usr/bin/perl does not exist on this machine,$ECHO_C"
    $ECHO_N "    Do you want to create a link to perl on this machine [y]: $ECHO_C"
    read PERL_LINKRESP
    : ${PERL_LINKRESP:="y"}            # default to y

    if test $PERL_LINKRESP = "y"
    then
        $ECHO_N "Enter full path to perl, for example /usr/local/bin/perl [-]: $ECHO_C"
        read PERL_PATH
        : ${PERL_PATH:="-"}            # default to y
        if test $PERL_PATH = "-"
        then
            $ECHO_N "Unable to create link to perl $ECHO_C"
        else
            if test -x $PERL_PATH
            then
                ln -s $PERL_PATH /usr/bin/perl
            else
                $ECHO_N "Unable to create link to perl $ECHO_C"
            fi
        fi
    fi
fi

echo "

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*                                                                             *
*      YOU HAVE SUCCESSFULLY INSTALLED CIMS SERVER FOR UNIX                   *
*                                                                             *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


"
now=`date`
echo "
********************************************************************************
Ending CIMS Server for Unix $0 Script at ${now}
********************************************************************************
"
exit 0
