#!/bin/sh
#
# Startup script for the Common Console Framework (CCFW).
# This is intended to be run from /etc/inittab with a "once" entry.
# Arguments:
# - optional keyword "initTab" to indicate if invoked from /etc/inittab

export PATH=/opt/IBMJava/jre/bin:$PATH:/opt/hsc/bin

export LD_LIBRARY_PATH=/opt/hsc/lib:$LD_LIBRARY_PATH

# The following two exports are required for tasklets-on-pHMC:
export JITC_COMPILEOPT=NINLINING{java/math/BigInteger}{oddModPow}
hscnls=`echo /opt/hsc/jars/i18n/hsc_*.jar | sed -e 's/ /:/g'`

export CLASSPATH=$CLASSPATH:/usr/websm/codebase/pluginjars/hmcdebug.jar:/usr/websm/codebase/pluginjars/hsc.jar:$hscnls:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/rmcjava.jar:/usr/websm/codebase/pluginjars/aca.jar:/usr/websm/codebase/pluginjars/hsc_bundles.jar:/usr/websm/codebase/pluginjars/bundles.jar:/opt/ccfw/ccfw.jar:/usr/websm/codebase/pluginjars/sniacimom.jar:/usr/websm/codebase/pluginjars/IBNMGUI.jar

# Suppress listening on the JDI debugging port
# 8/4/04 - For performance reasons, disable the port for now
export HMC_DEBUG_OFF=1

# Allow the CCFW Manager to connect to the CIM Server if started in SSL mode.
if [ $1 -a $1 = "initTab" ]; then
   startedfrominittab='yes'
else
   startedfrominittab='no'
fi
export HMC_JVM_EXTRA_OPTS="-Dorg.snia.wbem.cimom.properties=/opt/hsc/data/cim.properties -DUSE_PLAIN_SOCKETS_FOR_IPC=true -DSTARTED_FROM_INITTAB=$startedfrominittab"

# Initialize some miscellaneous variables.
TOP_DIR='/opt/ccfw'           # Top-level CCFW directory
CONSOLE_PATH=$TOP_DIR/        # Needed for some hmcfunctions functions such as queryFileLocation
OUT_FILE="$TOP_DIR/ccfw.out"  # stdout/stderr log file for us and the JVM
TRACE_CWD_PGM='iqzdtcre'      # CCFW trace program that uses cwd
TRACE_DDFC_PGM='iqzdtcon'     # CCFW trace program
. $TOP_DIR/hmcfunctions       # Get functions defined in hmcfunctions
TRACE_DDFC_FILE="$(queryFileLocation iqzdtrac.trm)iqzdtrac.trm"  # Full path to CCFW trace output file
TRACE_MASKS_FILE='/console/data/iqzdtctl.trm'                    # Full path to CCFW trace masks file

# Remove the TowersStarted file since we will be starting CCFW to recreate it.
rm -f $TOP_DIR/TowersStarted > /dev/null 2>&1

# Save the previous CCFW output collection file before starting a new one
rotatefile $OUT_FILE 3 10

# Start a new CCFW output collection file; log our start date and time.
echo -e "Starting CCFW at $(date) ($(date --utc))\n" >> $OUT_FILE

# If requested, start tracing to file if not already being performed. Any existing trace
# file will be copied first if tracing to file is started here.
if [ -f /var/hsc/log/.DEBUG_HSC ]; then
    # See if trace is already running.
    creLines=$(pstree -lp|grep $TRACE_CWD_PGM)
    if [ -z "$creLines" ]; then
       conLines=$(pstree -lp|grep $TRACE_DDFC_PGM)
       if [ -z "$conLines" ]; then
          echo "No $TRACE_CWD_PGM or $TRACE_DDFC_PGM program(s) running." >> $OUT_FILE

          # Rotate the CCFW trace file if one already exists.
          echo "Rotating $TRACE_DDFC_FILE" >> $OUT_FILE
          rotatefile $TRACE_DDFC_FILE 1 5

          # Start tracing for all masks and T,F,D,L level traces.
          echo '*      TFDL' > $TRACE_MASKS_FILE
          /usr/bin/$TRACE_DDFC_PGM -s90000 -p >> $OUT_FILE 2>&1 &
          echo "Started $TRACE_DDFC_PGM" >> $OUT_FILE
       else
          echo "$TRACE_DDFC_PGM program(s) already running." >> $OUT_FILE
       fi
    else
       echo "$TRACE_CWD_PGM program(s) already running." >> $OUT_FILE
    fi
fi

# Set console userid environment variable.
export "CONSOLE_USERID=ccfw";

# Start CCFW in the background, piping all of its stdout and stderr to the tracepipe
# program, which adds timestamps and writes it to OUT_FILE.
$TOP_DIR/runccfw $TOP_DIR "${CONSOLE_USERID}" 2>&1 | $TOP_DIR/timedread 2>&1 | $TOP_DIR/tracepipe -a $OUT_FILE &

#Start Pegasus in the background.
$TOP_DIR/native/scripts/startPegasus.sh &
