#!/bin/sh

# -------------------------------------------------------------
# DEFAULT VALUES ----------------------------------------------
# -------------------------------------------------------------
DEFAULT_SSI=localhost
DEFAULT_LD=${LUA51LIB}
DEFAULT_LIBS_DIR=./lib/
DEFAULT_SGA=`hostname`
DEFAULT_HOSTNAME=`hostname`
DEFAULT_SSI_PORT=7778
DEFAULT_SGA_CONFIG_DIR=./
#DEFAULT_RESTARTHOUR="0"  # O default e' nao reiniciar o SGA.
DEFAULT_LOG_DIR=../logs
DEFAULT_LOG_DUP_DIR=/tmp

# -------------------------------------------------------------
# INIT --------------------------------------------------------
# -------------------------------------------------------------
if test -d ../bin/${TEC_UNAME}; then
  sga_dir=../bin/${TEC_UNAME}
else
  sga_dir=../bin/${TEC_SYSNAME}
fi
sga_host=${DEFAULT_SGA}
sga_hostname=${DEFAULT_HOSTNAME}
ssi_host=${DEFAULT_SSI}
ssi_port=${DEFAULT_SSI_PORT}
sga_config_dir="${DEFAULT_SGA_CONFIG_DIR}"
extra_ld="${DEFAULT_LD}"
restarthour=${DEFAULT_RESTARTHOUR}
log_dir=${DEFAULT_LOG_DIR}
log_dup_dir=${DEFAULT_LOG_DUP_DIR}

# -------------------------------------------------------------
# MAIN --------------------------------------------------------
# -------------------------------------------------------------
  echo ""
  echo "======================================================="
  echo "SGA-Daemon - SGA Start Script"
  echo "Authors:"
  echo "    Ana Lúcia Moura (moura@tecgraf.puc-rio.br)"
  echo "    André Luiz Clinio (clinio@tecgraf.puc-rio.br)"
  echo "(c) Tecgraf/PUC-Rio, 2002"
  echo "======================================================="

  if test "$1" = "--help" || test "$1" = "--" ; then
     echo "Usage: sga-daemon [arguments]"
     echo ""
     echo "  --extra-ld <path> - seeks for dynamic libraries at <path>"
     echo "  --sga-directory <directory> - sets executable directory"
     echo "  --libs-directory <directory> - seeks for lua libraries at <directory>"
     echo "  --sandbox-directory <directory> - sets sandbox root directory"
     echo "  --proj-directory <directory> - sets projects directory (for all sga nodes)"
     echo "  --algo-directory <directory> - sets algorithms directory (for all sga nodes)"
     echo "  --sga <name> - sets the SGA name"
     echo "  --sga-addr <ip:port> - sets the SGA IP address and port"
     echo "  --ssi <server> - sets the SSI server machine"
     echo "  --ssi-port <port> - sets the SSI server port"
     echo "  --cnf-directory <directory> - sets configuration file directory"
     echo "  --restart <hour> - sets a restart hour"
     echo "  --log - redirects output into a file"
     echo "  --log-directory <directory> - sets logfile directory (depends on --log)"
     echo "  --log-dup - duplicates log to a temp directory (depends on --log)"
     echo "  --help or -- - shows this help message"
     echo ""
     echo ""
     exit
  fi

while test "$1" != ""; do
  case "$1" in
    --log)
     log=1
     shift
     ;;
    --log-directory)
     shift
     log_dir=$1
     echo "SGA log directory set to: $log_dir"
     shift
     ;;
    --log-dup)
     log_dup=1
     echo "Duplicates log to a temporary directory $log_dup_dir."
     shift
     ;;
    --extra-ld)
     shift
     extra_ld=$1
     echo "Extended LD path set to: $extra_ld"
     shift
     ;;
    --sga-directory)
     shift
     sga_dir=$1
     echo "SGA binary directory set to: $sga_dir"
     shift
     ;;
    --sga)
     shift
     sga_host=$1
     echo "SGA set to: $sga_host"
     shift
     ;;
    --sga-addr)
     shift
     sga_addr=$1
     echo "SGA address set to: $sga_addr"
     shift
     ;;
    --ssi)
     shift
     ssi_host=$1
     echo "SSI server set to: $ssi_host"
     shift
     ;;
    --ssi-port)
     shift
     ssi_port=$1
     echo "SSI server port set to: $ssi_port"
     shift
     ;;
    --cnf-directory)
     shift
     sga_config_dir=$1
     echo "Configuration file directory set to: $sga_config_dir"
     shift
     ;;
    --sandbox-directory)
     shift
     sga_sandbox_dir=$1
     echo "Sandbox directory set to: $sga_sandbox_dir"
     shift
     ;;
    --libs-directory)
     shift
     usr_libs_dir=$1
     echo "Libs directory set to: $usr_libs_dir"
     shift 
     ;; 
    --proj-directory)
     shift
     proj_dir=$1
     echo "Projects directory set to: $proj_dir"
     shift 
     ;;
    --algo-directory)
     shift
     algo_dir=$1
     echo "Algorithms directory set to: $algo_dir"
     shift 
     ;;
    --restart)
     shift
     restarthour=$1
     echo "restart hour set to: $restarthour"
     shift
     ;;
    *)
     echo ""
     echo "WARNING!!!"
     echo "Unrecognized argument <$1> found! Aborting..."
     echo "Try command: [sga-daemon --help] to check options."
     echo ""
     exit
  esac
done

logprint () {
  if test "$log_dup" = 1; then
    echo $* | tee -a $log_dup_file | tee -a $logfile
  else
    echo $* | tee -a $logfile
  fi
}

# -------------------------------------------------------------
# STARTING ----------------------------------------------------
# -------------------------------------------------------------

HOST=${sga_host}
export HOST

HOSTNAME=${sga_hostname}
export HOSTNAME

SSI_HOSTNAME=${ssi_host}
export SSI_HOSTNAME

SSI_PORT=${ssi_port}
export SSI_PORT

SGA_CONFIG_DIR=${sga_config_dir}
export SGA_CONFIG_DIR

RESTARTHOUR=${restarthour}
export RESTARTHOUR

LD_LIBRARY_PATH=${sga_dir}:${extra_ld}:${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH

SGA_LIBS_DIR=`cd ${DEFAULT_LIBS_DIR} && pwd` #exporta path absoluto das libs
export SGA_LIBS_DIR
lua_path="${SGA_LIBS_DIR}/?.lua"

if test "$usr_libs_dir" != ""; then
  USR_LIBS_DIR=`cd ${usr_libs_dir} && pwd` #exporta path absoluto das libs
  export USR_LIBS_DIR
  lua_path="${USR_LIBS_DIR}/?.lua;$lua_path"
fi

LUA_PATH="$lua_path;${LUA_PATH}"
export LUA_PATH

SGAD_DIR=${sga_dir}
export SGAD_DIR

CSBENCH_DIR=${sga_dir}
export CSBENCH_DIR

SGALOG_DIR=${log_dir}
export SGALOG_DIR

if test "$sga_sandbox_dir" != ""; then
  SGA_SANDBOX_DIR=${sga_sandbox_dir}
  export SGA_SANDBOX_DIR
fi

if test "$proj_dir" != ""; then
  SSI_PROJECT_DIR=${proj_dir}
  export SSI_PROJECT_DIR
fi

if test "$algo_dir" != ""; then
  SSI_ALGORITHM_DIR=${algo_dir}
  export SSI_ALGORITHM_DIR
fi

if test "$sga_addr" != ""; then
  SGA_ADDR=${sga_addr}
  export SGA_ADDR
fi

logfile=/dev/null
if test "$log" = 1; then
  if test ! -d ${log_dir}; then
    mkdir ${log_dir}
  fi
fi

test -z "$PWD" && export PWD=`pwd`

while [ 1 ]; do
  logFileName=sga-${sga_host}-`date +%Y%m%d%H%M%S`.dat
  logFileName=`echo ${logFileName} | sed "s/ /_/g"`
  if test "$log" = 1; then
    logfile=${log_dir}/${logFileName}
    if test "$log_dup" = 1; then
      log_dup_file=${log_dup_dir}/${logFileName}
    fi
  fi
  logprint ""
  logprint "Running SGA Script File..."
  logprint "   Running SGA: ${sga_host} (binary at ${sga_dir})"
  logprint "   (Using configuration file at ${sga_config_dir})"
  logprint "   (Using logfile at ${log_dir})"
  if test "$usr_libs_dir" != ""; then
  logprint "   (Using libs from ${usr_libs_dir})"
  fi
  logprint "   Extended dynamic libraries path: ${extra_ld}"
  logprint "   SSI Server: ${ssi_host}"
  logprint "   SSI Server Port: ${ssi_port}"
  logprint "   Restart hour: ${restarthour}"
  if test "$sga_addr" != ""; then
  logprint "   SGA ADDR: ${sga_addr}"
  fi
  if test "$log" = 1; then
  logprint "   LogFile: ${logfile}"
  fi
  if test "$log_dup" = 1; then
  logprint "   LogDupFile: ${log_dup_file}"
  fi

  logprint ""
  logprint ""
  
  sga_bin="${sga_dir}/sgad"
  if test ! -f "${sga_bin}"; then
    logprint "Arquivo não encontrado:" ${sga_bin}
    break
  fi
  if test ! -x "${sga_bin}"; then
    logprint "Arquivo não tem permissão de execução:" ${sga_bin}
    break
  fi

  if test "$log" = 1; then
    exitcodefile=.exitcode.${sga_host}
    if test "$log_dup" = 1; then
      ( ( ${sga_bin} 2>&1 ; echo $? > ${exitcodefile} ) | tee -a ${log_dup_file} ) | tee -a ${logfile} 
    else
      ( ${sga_bin} 2>&1 ; echo $? > ${exitcodefile} ) | tee -a ${logfile} 
    fi
    STATUS=`cat ${exitcodefile}`
    rm ${exitcodefile}
  else
    ${sga_bin}
    STATUS=$?
  fi
  if [ $STATUS -ne 1 ]; then #O valor 1 significa restart.
    if [ $STATUS -ne 2 ]; then #O valor 2 significa shutdown.
      logprint 'Status desconhecido:' $STATUS
    fi
    break
  fi
done
