#! /bin/csh -f
#
#  get tok.yy combination (interactively if necessary) and 
#  write to <runid>ID.TMP ... <runid> is argv[1].
#
#  method:  (0) check existing <runid>ID.TMP file;
#                 if that doesn't work,
#           (1) check ENQUEUE_TOKYY environment variable;
#                 if that doesn't work,
#           (2) check cwd path for /tok or /tok.yy subdirectory names;
#                 if that doesn't work,
#           (3) prompt user
#
#  at end, validate that tok.yy combination is valid
#
  if ( $#argv < 1 ) then
    echo "?tokyy_id:  script error, wrong number of arguments."
    exit 1
  endif
#
  set silent = FALSE
  set no_exit = FALSE
  if ( $#argv > 1 ) then
    if ( "$argv[2]" == "silent" ) then
       set silent = TRUE
    else if ( "$argv[2]" == "no_namelist" ) then
       set no_exit = TRUE
    endif
  endif
#
  set runid = $argv[1]
  if ( ! -f ${runid}TR.DAT ) then
    if ( $no_exit == FALSE ) then
       echo "?tokyy_id:  namelist file missing:  ${argv[1]}TR.DAT"
       exit 2
    endif
  endif
#
#   try setting from file...
#
  set tokyy = xxxx.xx
  if ( -f ${runid}ID.TMP ) then
    set tokyy = `cat ${runid}ID.TMP`
  endif
#
#   try setting from environment variable...
#
  if ( "$tokyy" == "xxxx.xx" ) then
    if ( $?ENQUEUE_TOKYY ) then
      set tokyy = $ENQUEUE_TOKYY
      set tok = `echo $tokyy | sed 's#\..*##'`
      if ( "$tok" != "$tokyy" ) then
        echo "(tok.yy = $tokyy set from ENQUEUE_TOKYY environment variable)"
	set yy = `echo $tokyy | sed 's#.*\.##'`
      else
        echo "(tokamak = $tok set from ENQUEUE_TOKYY environment variable)"
# run $XE/set_yr to figure out year
	set_yr $runid $tok
        if ( -f ${runid}ID.TMP ) then
          set tokyy = `cat ${runid}ID.TMP`
	  set yy = `echo $tokyy | sed 's#.*\.##'`
        else
          input_yy
          set yy = $status
          set yy = `yy_string $yy`
          set tokyy = "${tok}.${yy}"
        endif
      endif
      tok_valid $tok silent
      if ( ! $status ) then
        echo "?tokyy_id:  $tok is not a valid tokamak id."
        set tokyy = xxxx.xx
      endif
      yy_valid $yy silent
      if ( ! $status ) then
        echo "?tokyy_id:  $yy is not a valid two digit year code."
        set tokyy = xxxx.xx
      endif
    else
      set tokyy = xxxx.xx
    endif
  endif

#
#   if not done, try setting from $cwd
#
  if ( "$tokyy" == "xxxx.xx" ) then
    set zdir = $cwd
    while ( $zdir:h != $zdir:t ) 
      set tail = $zdir:t
      set zdir = $zdir:h
      set tok = `echo $tail | sed 's#\..*##'`
      set tok = `toupper $tok`
#
      tok_valid $tok silent
      set tvalid = $status
#
      set yvalid = 1
      if ( $tok != $tail ) then
	set yy = `echo $tail | sed 's#.*\.##'`
	yy_valid $yy silent
	set yvalid = $status
        if ( $tvalid && $yvalid ) then
	  echo "(tok.yy = ${tok}.${yy} set from cwd:  $cwd)"
	endif
      else if ( $tvalid ) then
	echo "(tokamak = $tok set from cwd:  $cwd)"
	set_yr $runid $tok
        if ( -f ${runid}ID.TMP ) then
          set tokyy = `cat ${runid}ID.TMP`
	  set yy = `echo $tokyy | sed 's#.*\.##'`
        else
          input_yy
          set yy = $status
          set yy = `yy_string $yy`
        endif
      endif
#
      if ( $tvalid && $yvalid ) then
        set tokyy = ${tok}.${yy}
        break
      endif
      set tokyy = xxxx.xx
#
    end
  endif

#
#  if not done, prompt user; also validate tok.yy obtained (by whatever means)
#
while(1)
  if ( "$tokyy" == "xxxx.xx" ) then
    while(1) 
      echo " *tokyy_id: enter tokamak id:"
      set tok = $<
      set tok = `toupper $tok`
      tok_valid $tok
      if ( $status) then
        break
      endif
    end
    set_yr $runid $tok
    if ( -f ${runid}ID.TMP ) then
      set tokyy = `cat ${runid}ID.TMP`
      set yy = `echo $tokyy | sed 's#.*\.##'`
    else
      input_yy
      set yy = $status
      set yy = `yy_string $yy`
      set tokyy = ${tok}.${yy}
    endif
  endif
#
  if ( $silent == TRUE ) then
    break
  else
    ask_yesno " *tokYY_id: verify run tok.yy = $tokyy"
    if ( $status ) then
      break
    else
      # ask for year
      set tokyy = "${tok}.xx"
      input_yy
      set yy = $status
      set yy = `yy_string $yy`
      set tokyy = "${tok}.${yy}"
    endif
  endif
end
#
#  write result to ${runid}ID.TMP file
#
  echo "$tokyy" > ${runid}ID.TMP
