#!/bin/csh -f
#
#       %W% %E%
#

set mach = `uname -m`
set host = `uname -n`
set curf = curf_${host}

switch ($mach)
  case i586:
  case i686:
    set psarg = -auxwwww # make sure we do not miss it
    breaksw
#  case sun4:
#  case sun4[a-z0-9]:
#    set psarg = -au
#    breaksw
  default:
    set psarg = -ef
    breaksw
endsw

if ( !(-e ${GIG_D_ROOT}/projects/renmanadm ) ) then
  mkdir ${GIG_D_ROOT}/projects/renmanadm
endif

if ( !(-e ${GIG_D_ROOT}/projects/renmanadm/${curf} ) ) then
  mkdir ${GIG_D_ROOT}/projects/renmanadm/${curf}
endif

if ( !(-e ${GIG_D_ROOT}/projects/renmanadm/${curf}/current_nodes ) ) then
  touch ${GIG_D_ROOT}/projects/renmanadm/${curf}/current_nodes
endif

if ( !(-e ${GIG_D_ROOT}/projects/renmanadm/${curf}/queued_anim ) ) then
  touch ${GIG_D_ROOT}/projects/renmanadm/${curf}/queued_anim
endif

if ( !(-e ${GIG_D_ROOT}/projects/renmanadm/${curf}/gigdonejobs ) ) then
  touch ${GIG_D_ROOT}/projects/renmanadm/${curf}/gigdonejobs
endif

set druns = `ps $psarg | grep gigrs_while_2 | grep -v grep | awk '{print $2}'`
if ( $druns == '' ) then
  gigrs_while_2 >>&! ${GIG_D_ROOT}/projects/renmanadm/${curf}/gigrs_logfile &
endif

while ( $druns == '' )
  set druns = `ps $psarg | grep gigrs_while_2 | grep -v grep | awk '{print $2}'`
  sleep 1
end
echo gigrs_while_2_pid $druns >> ${GIG_D_ROOT}/projects/renmanadm/${curf}/gigrs_logfile

while ( 1 )
  echo command \?

  set f_command = `stty erase ^H;awk '{print $0; exit}' -`
  set l_command = `echo $f_command | awk '{print NF; exit}'`

  set f_match = `echo $f_command | awk '{print $1; exit}' `

  if ( $f_match == 'exit' || $f_match == 'quit'  || $f_match == 'q' ) then
    exit

  else if ( $f_match == 'curserver' && $l_command == 1 ) then
    gigrs_renderstatus

  else if ( $f_match == 'addserver' ) then
    if ( $l_command == 2 ) then
      echo adding server $f_command[2]
      gigrs_comhandler "gigrs_renderserver -a $f_command[2]"
    else if ( $l_command == 3 ) then
      echo adding server $f_command[2] executable ${f_command[3]}
      gigrs_comhandler "gigrs_renderserver -m $f_command[2] -p $f_command[3]"
    endif 

  else if ( $f_match == 'delserver' && $l_command == 2 ) then
    set s_match = `echo $f_command | awk '{print $2; exit}' `
    echo deleting server $s_match
    gigrs_comhandler "gigrs_renderserver -d $s_match"

#  else if ( $f_match == 'susserver' && $l_command == 2 ) then
#    set s_match = `echo $f_command | awk '{print $2; exit}' `
#    echo suspending server $s_match
#    gigrs_comhandler "gigrs_renderserver -s $s_match"

#  else if ( $f_match == 'resserver' && $l_command == 2 ) then
#    set s_match = `echo $f_command | awk '{print $2; exit}' `
#    echo resuming server $s_match
#    gigrs_comhandler "gigrs_renderserver -r $s_match"

  else if ( $f_match == 'addjob' && $l_command > 3 ) then
    set s_match = `echo $f_command | awk '{{for(i=2;i<=NF;i++) print $i}; exit}'`
    set results = `gigrs_testargs $s_match`
    if ( $results[1] == 'okie' ) then
      gigrs_jobhandler add $s_match
    else
      echo $results
    endif

  else if ( $f_match == 'deljob' && $l_command > 3 ) then
    set s_match = `echo $f_command | awk '{{for(i=2;i<=NF;i++) print $i}; exit}'`
    gigrs_jobhandler del $s_match

  else if ( $f_match == 'getinfo' && $l_command > 3 ) then
    set s_match = `echo $f_command | awk '{{for(i=2;i<=NF;i++) print $i}; exit}'`
    echo getting info on job $s_match
    gigrs_getjobinfo $s_match

  else if ( $f_match == 'listjobs' ) then
    cat ${GIG_D_ROOT}/projects/renmanadm/${curf}/queued_anim

  else if ( $f_match == 'renderhours'&& $l_command == 4 ) then
    set s_match = `echo $f_command | awk '{{for(i=2;i<=NF;i++) print $i}; exit}'`
    gigrs_comhandler "gigrs_renderserver -m $s_match[1] -b $s_match[2] -e $s_match[3]"

  else if ( $f_match == 'freehours'&& $l_command == 2 ) then
    set s_match = `echo $f_command | awk '{{for(i=2;i<=NF;i++) print $i}; exit}'`
    gigrs_comhandler "gigrs_renderserver -f $s_match[1]"

  else
    echo available commands are :
    echo addserver '   '\'hostname\''                    'Add server
    echo addserver '   '\'hostname\''  '\'gig_sa_path\''     'Add server with different architecture
    echo delserver '   '\'hostname\''                    'Delete server
#    echo susserver '   '\'hostname\''                    'Suspend server
#    echo resserver '   '\'hostname\''                    'Resume server
    echo curserver '                                 'Get info on current servers
    echo addjob '      '\'jobname\''                     'Add job
    echo deljob '      '\'jobname\''                     'Delete job
    echo getinfo '     '\'jobname\''                     'Get info on specified job
    echo listjobs '                                  'List queued jobs
    echo renderhours ' '\'hostname\''                    'Specify desired rendering hours on working days
    echo freehours '   '\'hostname\''                    'No limitations regarding rendering hours
    echo exit or quit
    echo

  endif
end
