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

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

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_jobs ) ) then
  touch ${GIG_D_ROOT}/projects/renmanadm/${curf}/queued_jobs
endif

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

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

set rendir = ${GIG_D_ROOT}"/projects/renmanadm/${curf}"

while ( 2 )

# checkpointing
#  if ( -e ${rendir}/cpremoveflag ) then                    # script was interupted
#    if ( -e ${rendir}/cpcopyflag && -e ${rendir}_* ) then  # a copy exists
#      /bin/cp -r ${rendir}_* ${rendir}                     # a move could go wrong
#      /bin/rm -f ${rendir}_*                               # now it is safe to remove ${rendir}_*
#      /bin/rm -f ${rendir}/cpremoveflag
#      /bin/rm -f ${rendir}/cpcopyflag
#    else if ( -e ${rendir}_* ) then                        # copy does exist but was not finished
#      /bin/rm -f ${rendir}_*
#      /bin/rm -f ${rendir}/cpremoveflag
#    else
#      
#    endif
#  endif
#
# copy whole directory
#  if ( -e ${rendir}_* ) then
#    set old_copy = ${rendir}_*
#    echo old_copy $old_copy
#  endif
#  set time_stamp = `date -u +%Y%j%H%M%S`
#  /bin/cp -r ${rendir} ${rendir}_${time_stamp}
#  touch ${rendir}/cpcopyflag
#
# get older copy and remove it
#  if ( $old_copy != '' ) then
#    echo /bin/rm -fr $old_copy
#    /bin/rm -fr $old_copy
#    touch ${rendir}/cpremoveflag
#  endif

  if ( -e ${rendir}/postponed_coms ) then

    touch ${rendir}/postpoflag_2
    echo 1 > ${rendir}/postpoturn
    set turn = `cat ${rendir}/postpoturn`
    while ( ( -e ${rendir}/postpoflag_1 ) && ( $turn == '1' ) )
      set turn = `cat ${rendir}/postpoturn`
    end

    set n_entries = `cat ${rendir}/postponed_coms | awk 'BEGIN {} {s+=1;} END {print s}' s=0 -`
    set i = 0
    while ( $i < $n_entries )
      set get_ent = `cat ${rendir}/postponed_coms | awk 'BEGIN{}{if(s==t){print $0;exit};s+=1;}END{}' s=0 t=$i -`
      $get_ent
      @ i += 1
    end

    /bin/rm -f ${rendir}/postponed_coms
    /bin/rm -f ${rendir}/postpoflag_2

  endif  

  if ( -e ${rendir}/add_queued_jobs ) then

    touch ${rendir}/jobaddflag_2
    echo 1 > ${rendir}/jobaddturn
    set turn = `cat ${rendir}/jobaddturn`
    while ( ( -e ${rendir}/jobaddflag_1 ) && ( $turn == '1' ) )
      set turn = `cat ${rendir}/jobaddturn`
    end

    cat ${rendir}/add_queued_jobs >> ${rendir}/queued_jobs

    /bin/rm -f ${rendir}/add_queued_jobs
    /bin/rm -f ${rendir}/jobaddflag_2

  endif  

  if ( -e ${rendir}/del_queued_jobs ) then

    touch ${rendir}/jobdelflag_2
    echo 1 > ${rendir}/jobdelturn
    set turn = `cat ${rendir}/jobdelturn`
    while ( ( -e ${rendir}/jobdelflag_1 ) && ( $turn == '1' ) )
      set turn = `cat ${rendir}/jobdelturn`
    end

    set n_entries = `cat ${rendir}/del_queued_jobs | awk 'BEGIN {} {s+=1;} END {print s}' s=0 -`
    set i = 0
    while ( $i < $n_entries )
      set get_ent = `cat ${rendir}/del_queued_jobs | awk 'BEGIN{}{if(s==t){print $0;exit};s+=1;}END{}' s=0 t=$i -`
      egrep -v -e "$get_ent" ${rendir}/queued_jobs >! ${rendir}/queued_jobs_tmp
      /bin/mv ${rendir}/queued_jobs_tmp ${rendir}/queued_jobs
      @ i += 1
    end

    /bin/rm -f ${rendir}/del_queued_jobs
    /bin/rm -f ${rendir}/jobdelflag_2

  endif  

  gigrs_startjob

  /bin/rm -f ${rendir}/cpremoveflag
  /bin/rm -f ${rendir}/cpcopyflag

  sleep 10

end
