#!/bin/sh
#* ----------------------------------------------------------------------
#*               Proprietary and Confidential Information
#*
#* This material contains, and is part of a computer software program
#* which is, proprietary and confidential information owned by COMPUTER
#* ASSOCIATES INTERNATIONAL, INC.  The program, including this material,
#* may not be duplicated, disclosed or reproduced in whole or in part for
#* any purpose without the express written authorization of COMPUTER
#* ASSOCIATES. All authorized reproductions must be marked with this
#* legend.
#*
#*                          Copyright (c) 1992-2005
#*             Computer Associates International, Incorporated
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
#*  SCCS Module Control Stamp
#* ----------------------------------------------------------------------
#
#* "@(#)unistart	1.67  08/10/05 09:45:02"
#* "Copyright (C) 1992-2005 Computer Associates International, Inc.  All rights reserved."
#
#

BASH_ENV=""
export BASH_ENV

Extras=""
Params=""
LogFile=""
while [ "$1" != "" ]
do
   if [ "$1" = "-h" -o "$1" = "-?" -o "$1" = "-v" -o "$1" = "-q" ]; then
      Extras="${Extras}${1} "
   elif [ "$1" = "-f" ]; then
      Extras="${Extras}${1} ${2} "
      LogFile="$2"
      shift 1
   else
      Params="${Params}${1} "
   fi
   shift 1
done

[ -f /etc/profile.CA ] && . /etc/profile.CA 

if [ "$CASHCOMP" = "" ];then
  echo "CASHCOMP is not set in $0."
  echo "Exiting."
  exit 4
fi
# CCSINS.272 - magka04:
# Test to see if NSM/CCS r11 is installed
r11_installed=0
ccsinstalled=`lsm -l -O PIF | grep CommonServices | wc -l`
nsminstalled=`lsm -l -O PIF | grep UnicenterNSM | wc -l`
[ $nsminstalled -eq 1 -o $ccsinstalled -eq 1 ] && r11_installed=1
[ $r11_installed -eq 0 -a ! -f "${CAIGLBL0000}/scripts/unistart" ] && r11_installed=1


if [ -f "$CASHCOMP/csutils/scripts/lcl_echo_func" ];then
        . "$CASHCOMP/csutils/scripts/lcl_echo_func"
fi

deprecated_complist=""
broken_complist=""
getall=0

add_deprecated_depcomp()
{  testrc=1
   comparg=$1
   addtype=$2
   case $1 in
      ingres*)
           testcomp="ingresEI";;
      dna|DNA)
           testcomp="CA-diadna";;
      ukb|UKB)
           testcomp="CA-diaukb";;
      ams|AMS)
           testcomp="CA-alert";;
      AIS)
           testcomp="CA-ais";;
      cam)
           testcomp="CA-cam";;
      *)
           testcomp="$comparg" ;;
   esac
   if [ -x $CASHCOMP/csutils/bin/casrvc ]; then
      $CASHCOMP/csutils/bin/casrvc test $Extras $testcomp > /dev/null 2>&1
      testrc=$?
   fi
   if [ "$testrc" != "0" ]; then
      if [ -d $CASHCOMP/ccs/$comparg/scripts -a -x $CASHCOMP/ccs/$comparg/scripts/rc -a -f $CASHCOMP/ccs/$comparg/inst_log ]; then
         deprecated_complist="$deprecated_complist $comparg"
      else
         [ "$addtype" = "mandatory" ] && broken_complist="$broken_complist $comparg"
      fi
   fi
}

validate_comps()
{
   comps_requested="$*"

   for comp in $comps_requested 
   do
      case $comp in
      ingres*)
           compname="ingresEI";;
      dna|DNA)
           compname="CA-diadna";;
      ukb|UKB)
           compname="CA-diaukb";;
      ams|AMS)
           compname="CA-alert";;
      cam)
           compname="CA-cam";;
      AIS)
           compname="CA-ais";;
      *)
          compname="$comp" ;;
      esac
      if [ "$compname" = "all" -o "$compname" = "ALL" ]; then
         getall=1
      elif [ "$compname" = "nsm3x" ]; then
         getold=1
      elif [ ! -x $CASHCOMP/csutils/bin/casrvc ]; then
         deprecated_complist="$deprecated_complist $comp"
      else
         $CASHCOMP/csutils/bin/casrvc test $Extras $compname >/dev/null 2>&1
         if [ $? -eq 0 ]; then
            ############################################
            # This is the standard, preferred solution #
            ############################################
            complist="$complist $compname"
         else
            add_deprecated_depcomp $comp mandatory
         fi
      fi
   done
}

### START OF MAIN ###

complist=""
validate_comps "$Params"

rc=0

ordered_depcomp="enf ino sessman cci emsrvc opr star snmp"
if [ "$getold" = "1" ]; then
   # CCSINS.272 - magka04:
   # NSM r11 is not installed, and the NSM 3x utility is
   # available use it to start
   if [ $r11_installed -eq 0 ];then
      $CAIGLBL0000/scripts/unistart all
      rc=$?
      exit $rc
   fi

   # We need to walk through list of entries not currently known about for
   # possible deprecated_list addition
   for depcomp in $ordered_depcomp
   do
      add_deprecated_depcomp $depcomp optional
   done
   for depcomp in `ls $CASHCOMP/ccs`
   do
      if [ -d $CASHCOMP/ccs/$depcomp ]; then
         found=no
         for i in $ordered_depcomp
         do
            if [ "$depcomp" = "$i" ]; then
               found=yes
            fi
         done
         if [ "$found" = "no" ]; then
            add_deprecated_depcomp $depcomp optional
         fi
      fi
   done
fi

if [ "$getall" = "1" ]; then
   complist="$complist all"
fi

if [ "$deprecated_complist" = "" -a "$getold" != "1" ]; then
   oldrun_unistart=1
fi
   
[ "$broken_complist" != "" ] && complist="$complist $broken_complist"
if [ "$complist" != "" -o "$oldrun_unistart" = "1" ]; then
   CA_WRAPPER_NAME=unistart; export CA_WRAPPER_NAME
   $CASHCOMP/csutils/bin/casrvc start $Extras $complist
   temprc=$?
   [ "$complist" = "" ] && temprc=1
   [ "$temprc" != "0" ] && rc="`expr $rc + $temprc`"
fi

if [ "$deprecated_complist" != "" ]; then
   # Here we need to go through a bunch of the old stuff for components not
   # supporting r11 install methodology
   if [ "$getold" = "1" ]; then
      # Already ordered properly
      new_deprecated_complist="$deprecated_complist"
   else
      new_deprecated_complist=""
      for depcomp in $ordered_depcomp
      do
         found=no
         for i in $deprecated_complist
         do
            if [ "$depcomp" = "$i" ]; then
               found=yes
            fi
         done
         if [ "$found" = "yes" ]; then
            new_deprecated_complist="$new_deprecated_complist $depcomp"
         fi
      done
      for depcomp in $deprecated_complist
      do
         found=no
         for i in $ordered_depcomp
         do
            if [ "$depcomp" = "$i" ]; then
               found=yes
            fi
         done
         if [ "$found" = "no" ]; then
            new_deprecated_complist="$new_deprecated_complist $depcomp"
         fi
      done
   fi
   for depcomp in $new_deprecated_complist
   do
      [ -f $CASHCOMP/ccs/$depcomp/scripts/envset ] && . $CASHCOMP/ccs/$depcomp/scripts/envset
      [ -f $CASHCOMP/ccs/$depcomp/scripts/envsetlocal ] && . $CASHCOMP/ccs/$depcomp/scripts/envsetlocal
      [ -x $CASHCOMP/ccs/$depcomp/scripts/rc ] && $CASHCOMP/ccs/$depcomp/scripts/rc
      temprc=$?
      [ "$temprc" != "0" ] && rc="`expr $rc + $temprc`"

      #  Allow cci time to come up
      if [ "$depcomp" = "cci" ]; then
         sleep 5
      fi

   done
fi

exit $rc
