#! /bin/csh -f


#exit status:
#  0 - ok
#  1 - error
#  2 - wrong syntax



if ( $#argv == 1 ) then
  if ( $1 == list ) then
    ls *.pid >& /dev/null
    if ( ${status} == 0 ) then
      echo TANGO Interactive servers are running on ports:
      ls *.pid |sed -e 's/\.pid//g'
    else
      echo There is no TANGO Interactive server running.
    endif
    exit 0
  else
    echo "Usage: $0 start|stop|stat <port#>"
    echo "       $0 list"
    exit 2
  endif
else if( $#argv == 2 ) then
  set comd=$1
  set port=$2
  set pidFile=${port}.pid
  switch ( ${comd} ) 
    case start
      if( -f ${pidFile} ) then
        echo TANGO Interactive server is already running at port ${port}!
        exit 1
      else
        setenv CLASSPATH /usr/java/classes:tango.zip
        set logFile=`date +%y.%m.%d_%H:%M`.log
        set startCmd="nohup /usr/java/bin/java -jit -tran webwisdom.tango.server.TangoServer ${port} ${logFile}"
        ${startCmd} &
        if ( ${status} == 0 ) then
          echo ${child} `date +%y.%m.%d` `date +%H:%M` ${logFile} ${CLASSPATH} ${startCmd} > ${pidFile}
          echo TANGO Interactive server started on port ${port}.
          exit 0
        else
          rm -f ${pidFile}
          echo Starting TANGO Interactive server failed!
          exit 1
        endif
      endif
      breaksw
    case kill
    case stop
      set pidNum=`cat ${pidFile} | awk '{print $1}'`
      ps -ef |grep ${pidNum} >&! /dev/null
      if ( ${status} == 0 ) then
        kill ${pidNum}
        rm ${pidFile}
        echo TANGO Interactive server killed on port ${port}.
        exit 0
      else
        echo There is no TANGO Interactive server running on port ${port}!
        exit 1
      endif
      breaksw
    case stat
      set pidNum=`cat ${pidFile} | awk '{print $1}'`
      ps -ef |grep ${pidNum} >&! /dev/null
      if ( ${status} == 0 ) then
        echo ""TANGO Interactive server on port ${port} is alive:
        echo "  "started on `cat ${pidFile} | awk '{print $2}'` at `cat ${pidFile} | awk '{print $3}'`
        echo "  "with CLASSPATH=`cat ${pidFile} | awk '{print $5}'`
        echo "  "by command `cat ${pidFile} | sed -e 's/^.*nohup/nohup/p'`
        echo "  "running with PID=${pidNum}
        echo "  "loging in file `pwd`/`cat ${pidFile} | awk '{print $4}'`
        echo "  "meta data stored in `pwd`/${pidFile}
        ps -ef |head -1
        ps -ef |grep `cat ${pidFile} | awk '{print $1}'`
        exit 0
      else
        echo There is no TANGO Interactive server running on port ${port}!
        exit 1
      endif
      breaksw
    default
      echo "Usage: $0 start|stop|stat <port#>"
      echo "       $0 list"
      exit 2
  endsw
else
  echo "Usage: $0 start|stop|stat <port#>"
  echo "       $0 list"
  exit 2
endif





