#!/bin/sh

unset BOOT_DIR
unset SHOW_HELP

# Set the default boot directory.
if [ "$MODEL_TYPE" = "2200" -o "$MODEL_TYPE" = "2800" ]; then
   BOOT_DIR="/"
elif [ "$MODEL_TYPE" = "N70" -o "$MODEL_TYPE" = "N50" -o "$MODEL_TYPE" = "BIOS" ]; then
   BOOT_DIR=/mnt/serverhost
fi

while getopts ":d:h" OPTION ; do

   case "$OPTION" in
      d):
         MODEL_TYPE=SERVER
         BOOT_DIR=$OPTARG ;;
      ?|h|*):
         SHOW_HELP=true ;;
   esac
   
#   echo "OPTION : $OPTION"
#   echo "OPTARG : $OPTARG"
#   echo "OPTIND : $OPTIND"
#   echo

done

let num=${OPTIND}-1
shift $num



if [ -z "$BOOT_DIR" ]; then

   echo "Directory not specified for server execution."
   echo "Use -d when running from a server."
   SHOW_HELP=true

elif [ ! -f $BOOT_DIR/IBM/version ]; then

   echo "Directory \"$BOOT_DIR\" is not a valid IBM NetVista Linux Thin Client boot directory."
   SHOW_HELP=true

fi

if [ "$BOOTSOURCE" = "flash" -o "$BOOTSOURCE" = "peer" ]; then

   echo "Executable only on a server booted thin client or an IBM NetVista Linux Thin Client boot server."
   echo
   echo "Exiting."
   exit 1

fi

if [ "`id -u`" != "0" ]; then

   echo "Only root can execute this command."
   echo
   echo "Exiting."
   exit 1

fi

if [ "$SHOW_HELP" = "true" ]; then
   echo
   echo "Syntax: `basename $0` [-h] [-d DIRECTORY] "
   echo "  -h            show help"
   echo "  -d DIRECTORY  use the server boot DIRECTORY (Forces server boot mode)"
   echo

   exit 1
fi

export BOOT_DIR












if [ -z "$*" ]; then
   # Create base.cramfs.
   # Note: This must be done BEFORE the call to the Java application that updates the IDFs and SDFs
   # since base.cramfs is referenced in at least one SDF.
#   mkdir -p ${BOOT_DIR}/IBM/update/base
#   echo "Creating base.cramfs"
#   ${BOOT_DIR}/IBM/bin/createBaseCramfs.sh

   update_arguments="/IBM/update/sdf /IBM/update/idf"
else
   update_arguments="$*"
fi




# Make sure the IDF/SDF tool can access necessary libs & paths.
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PATH:/usr/sbin:/usr/bin:/bin:/sbin

# If running on a multiple processor machine, turn off the JIT and increase
# the initial heap size in an attempt to eliminate JVM hangs.
cpus=`grep -c -e '^cpu[0-9]\+' /proc/stat`
echo "Number of CPUs: $cpus"

if [ $cpus -gt 1 ] ; then
  export JAVA_COMPILER=NOJIT
  initial_heap_size="-Xms64m"
  max_heap_size="-Xmx128m"
fi


# The following code updates all SDFs and IDFS and creates the intermediate SDF tar files 
# needed for flash recovery.  
echo "chroot ${BOOT_DIR} /opt/IBMJava2-13/jre/bin/java $initial_heap_size $max_heap_size -cp /IBM/update/LCMT.jar UpdateIDF $update_arguments"
chroot ${BOOT_DIR} /opt/IBMJava2-13/jre/bin/java $initial_heap_size $max_heap_size -cp /IBM/update/LCMT.jar UpdateIDF $update_arguments 



#if [ "$MODEL_TYPE" = "SERVER" ]; then
#if [ -z "$*" ]; then
   # Create the base image files, base_2x00.tgz, and base_n70.tgz
   # Note: This must be done AFTER the call to the Java application that updates the IDFs and SDFs.
#   export BOOT_DIR=$BOOT_DIR
#   cd $BOOT_DIR
   #Common/scripts/createBase.sh 2>&1 | tee -a $image_log
#   ${BOOT_DIR}/IBM/bin/createBase.sh 
#fi

