#!/bin/sh
# $Id: postinstall,v 1.1.2.11 2005/05/02 22:41:50 ms152511 Exp $
# postinstall for scs 

BASEDIR=`dirname $0`/..
INSTDIR=$BASEDIR/install
MODDIR=$BASEDIR/modules
SCSHOME=/scs
SCSLOGDIR="$SCSHOME/logs"
SCSINIT="init.installscs"
TOMCATROOT=/var/tomcat4
TOMCATETC=/etc/tomcat4
INITTOOL=$SCSHOME/sbin/init_tool.pl
POSTGRES_SRPM=postgresql-7.3.4-1PGDG.src.rpm

RPM=/bin/rpm
CP=/bin/cp
MKDIR=/bin/mkdir
LN=/bin/ln
RM=/bin/rm
AWK=/bin/awk
CHOWN=/bin/chown
CHMOD=/bin/chmod
UNAME=/bin/uname
SSHKEYGEN=/usr/bin/ssh-keygen

# get the architecture 
ARCH="`$UNAME -m`"

TRUE="true"
FALSE="false"

#
# fuctions
#

errorExit() {
   echo "ERROR: $@!" 1>&2;
   exit 1;
}

printInfo() {
   echo "Info: $@";
}

printWarn() {
   echo "WARN: $@";
}

#
# Main
#


#
# Get the factory install switch if it exists
#
# The FACTORY_INST flag is used to figure out if we are 
# attempting to install an appliance at the Sun factory.
# If at the factory there are special things we want to 
# do at first startup instead of in the postinstall. 
#
FACTORY_INST="$FALSE"
ARG=$@
if [ "$ARG" = "$TRUE" ] ; then
  FACTORY_INST="$TRUE" 
fi


#
# call the paraniod script
#
/bin/sh $INSTDIR/paranoid

#  
# make the scs log directory
#
if [ ! -d "$SCSLOGDIR" ] ; then
  printInfo "Creating log directory: $SCSLOGDIR"
  $MKDIR -p "$SCSLOGDIR" 
fi

#
# make sure am log file exists
#
if [ ! -f "$SCSLOGDIR/AM.log" ] ; then
  printInfo "Creating Active Monitor log"
  touch $SCSLOGDIR/AM.log
fi

#
# set the permissions on the log file directory
#

chown tomcat4:tomcat4 "$SCSLOGDIR"
chmod 0775 "$SCSLOGDIR"

#
# add /scs/lib to ld.so.conf (if required)
#

if grep $SCSHOME/lib /etc/ld.so.conf > /dev/null 2>&1 ; then
   : 
else
   echo "$SCSHOME/lib" >> /etc/ld.so.conf 
fi

#
# create the ui work directory
#
printInfo "Creating ui tomcat work directory"
DIR="$TOMCATROOT/work/Standalone/localhost/sdui"
# clear the JSP cache to prevent the old pages from being displayed
$RM -rf "$DIR"
$MKDIR -p "$DIR"
$CHOWN -R tomcat4:tomcat4 "$DIR"

printInfo "Setting ownership"
$CHOWN -R tomcat4:tomcat4 "$TOMCATETC"


#
# Setup the SCS web server
#
printInfo "Preparing scs-httpd"

# The SCS web server needs its config file and apache modules pointer setup.
# First we need to know which OS we're running.
BUILD=""
OS=""

# look for the vanilla rh
if $RPM -q redhat-release > /dev/null 2>&1 ; then 
    RHREL="`$RPM -q redhat-release 2>/dev/null`"
    BUILD="`echo $RHREL | sed -e 's/\-/ /g' | $AWK '{ print $3; }'`"
    OS="redhat"
fi

if $RPM -q fedora-release > /dev/null 2>&1 ; then 
    RHREL="`$RPM -q fedora-release 2>/dev/null`"
    BUILD="`echo $RHREL | sed -e 's/\-/ /g' | $AWK '{ print $3; }'`"
    OS="fedora"
fi

# look for non-vanilla versions
if [ "X$BUILD" = "X" ] ; then
    for i in ws es as 
      do
      if $RPM -q redhat-release-$i > /dev/null 2>&1 ; then
	  RHREL="`$RPM -q redhat-release-$i 2> /dev/null`"
          BUILD="`echo $RHREL | sed -e 's/\-/ /g' | $AWK '{ print $4; }'`"
          OS="redhat"
	  if [ X"$BUILD" != "X" ] ; then
              break;
 	  fi 
      fi
    done
fi

# check the suse builds
if $RPM -q suse-release > /dev/null 2>&1 ; then 
    SUSEREL="`$RPM -q suse-release 2>/dev/null`"
    BUILD="`echo $SUSEREL | sed -e 's/\-/ /g' | $AWK '{ print $3; }'`"
    OS="suse"
fi

# check the sles builds
if $RPM -q sles-release > /dev/null 2>&1 ; then 
    SUSEREL="`$RPM -q sles-release 2>/dev/null`"
    BUILD="`echo $SUSEREL | sed -e 's/\-/ /g' | $AWK '{ print $3; }'`"
    OS="sles"
fi

# stop httpd if it is running
if [ -f /var/run/scs-httpd.pid ]; then
    "$INITTOOL" stop init.scs-httpd
fi

# switch out tomcat4 init scripts
"$INITTOOL" stop tomcat4
"$INITTOOL" disable tomcat4
"$INITTOOL" add init.scs-tomcat4
"$INITTOOL" enable init.scs-tomcat4
"$INITTOOL" start init.scs-tomcat4

# remove the old modules directory
$RM -f "$TOMCATROOT/modules"

# configure apache based on the OS version

# assume Apache 1.3.x by default, then override to 2.0.x if necessary
APACHE_VERS="1.3.x"
APACHE_LIB="/usr/lib/apache"
APACHE_RPM="apache-1.3"
APACHE_BIN="/usr/sbin/httpd"

case "$OS" in
    redhat)
        case "$BUILD" in
            8.0|3[AE]S)
            APACHE_LIB="/usr/lib/httpd/modules"
            if [ X"$ARCH" = "Xx86_64" ] ; then
                APACHE_LIB="/usr/lib64/httpd/modules"
            fi
            APACHE_VERS="2.0.x"
            APACHE_RPM="httpd-2.0"
            ;;
        esac
        ;;
    fedora)
        case "$BUILD" in
            1|2|3)
                APACHE_LIB="/usr/lib/httpd/modules"
                if [ X"$ARCH" = "Xx86_64" ] ; then
                    APACHE_LIB="/usr/lib64/httpd/modules"
                fi
                APACHE_VERS="2.0.x"
                APACHE_RPM="httpd-2.0"
                ;;
        esac
        ;;
    suse)
        APACHE_LIB="/usr/lib/apache2-prefork"
        APACHE_BIN="/usr/sbin/httpd2"
        APACHE_VERS="2.0.x"
        APACHE_RPM="apache2-prefork"
        if [ X"$ARCH" = "Xx86_64" ] ; then
            APACHE_LIB="/usr/lib64/apache2-prefork"
        fi
        ;;
    sles)
        APACHE_LIB="/usr/lib/apache2-prefork"
        APACHE_BIN="/usr/sbin/httpd2"
        APACHE_VERS="2.0.x"
        APACHE_RPM="apache2-prefork"
        if [ X"$ARCH" = "Xx86_64" ] ; then
            APACHE_LIB="/usr/lib64/apache2-prefork"
        fi
        ;;
esac

#
# store off our canonical version of httpd.conf.orig
# we generate the real version in updateIP.sh, inserting the control station IP
#

$CP -f $TOMCATROOT/conf/httpd.conf-$APACHE_VERS \
       $TOMCATROOT/conf/httpd.conf.orig

# invoke script to insert network-configuration specific information

$CP "$SCSHOME/sbin/hostinstall.pl" "$TOMCATROOT/web/pub/hostinstall.pl.orig"
$SCSHOME/sbin/updateIP.sh

# create the proper link in TOMCATROOT to the apache modules

$LN -s "$APACHE_LIB" "$TOMCATROOT/modules"

#
# make sure atd is enabled for scheduling
#

printInfo "Enabling atd scheduler daemon"

for i in atd
do
    { "$INITTOOL" enable $i &&
      "$INITTOOL" start  $i ; } || \
        errorExit "Cannot enable $i"
    printInfo "Enabled $i"
done

#
# move uninstall scripts to /scs/uninstall
#
# TODO: move this in conf.rpm
printInfo "Moving uninstall scripts into place"
$MKDIR -p $SCSHOME/uninstall
$CP $BASEDIR/uninstall/* $SCSHOME/uninstall

#
# Move the README into place
#
printInfo "Moving README into place"
$CP $BASEDIR/README $SCSHOME/


#
# Move the license agreement into place
#
printInfo "Moving license agreement into place"
$CP $BASEDIR/install/license_agreement_long \
    $SCSHOME/license_agreement


#
# Make the backups directory
#
$MKDIR -p $SCSHOME/backups

#
# copy the postgres src rpm to the public web directory
#
#

cp -f $BASEDIR/pkgs/$POSTGRES_SRPM /var/tomcat4/web/pub/src

#
# Move the modules and install script to the local system
#
# Note: we do this because we may be doing a factory install
# and we want to do things consistantly.
#
printInfo "Moving init script into place"
DIR=/var/tmp/scs/install
$MKDIR -p $DIR
$CP $INSTDIR/$SCSINIT $DIR
$CP $MODDIR/* $DIR

#
# generate SSH keypair for host connections
#
if [ ! -x "$SSHKEYGEN" ] ; then
  errorExit "Cannot execute $SSHKEYGEN"
fi

if [ ! -d "$SCSHOME/etc/ssh" ] ; then
  $MKDIR -p $SCSHOME/etc/ssh
fi

if [ -f $SCSHOME/etc/ssh/csid_rsa ] ; then
  printInfo "SSH keypair already present in $SCSHOME/etc/ssh"
else
  printInfo "Generating SSH key"
  # no passphrase
  $SSHKEYGEN -q -t rsa -P "" -f $SCSHOME/etc/ssh/csid_rsa || errorExit "Could not generate key"
fi

# copy public key to a host-accessible place
$CP $SCSHOME/etc/ssh/csid_rsa.pub $TOMCATROOT/web/pub/ || errorExit "Could not copy public key to $TOMCATROOT/web/pub/"

# copy the correct list of modules
$CP $INSTDIR/module_list $DIR/module_list
  
# execute the script
/bin/sh $DIR/$SCSINIT start

exit 0
