#!/bin/csh -f

set prog = $0
set prog = $prog:t

#------------------------------------------------------------
#  check op sys
#------------------------------------------------------------
set op_sys = `uname -s | tr '[a-z]' '[A-Z]'`
#------------------------------------------------------------
#  make_mz4 only handles linux, unix, OS X, or AIX op sys
#------------------------------------------------------------
if( $op_sys == LINUX || $op_sys == UNIX || $op_sys == DARWIN ) then
  set op_sys = LINUX
  set mpi_cmd = mpif90
else if( $op_sys == AIX ) then
  set mpi_cmd = mpxlf95_r
else
  echo "$prog only works for Linux, Unix, OS X, or AIX op systems"
  exit 1
endif

#------------------------------------------------------------
#  check for configuration file
#------------------------------------------------------------
if( $?MZ4_ROOT ) then
  set cfg_file = "$MZ4_ROOT/configure/MZ4.cfg"
else
  set cfg_file = "../../configure/MZ4.cfg"
endif
if( ! -e $cfg_file ) then
  echo "Can't find configuration file $cfg_file"
  echo "Run the configuration utility, MZ4_ROOT/configuration cfg_mz4; $prog termination"
  exit -1
else
  set cfgs = `cat $cfg_file`
  if( ! $?MZ4_ROOT ) then
    set MZ4ROOT = $cfgs[4]
  else
    set MZ4ROOT = $MZ4_ROOT
  endif
endif

set stop_opts = 0
#------------------------------------------------------------
#  setup variables
#------------------------------------------------------------
set top_opts = ("Simulation type" "Simulation timing" "Stop")
set nopts = $#top_opts
set sim_tim_prompt = ("Enter simulation start time (yyyymmdd:secs)" \
                      "Enter simulation duration time" \
                      "Enter simulation time step (seconds)" \
                      "Enter calendar (365,gregorian)")

#------------------------------------------------------------
#  top level options selection loop
#------------------------------------------------------------
top_level_sel:
set ndx   = 1
echo " "
while ( $ndx <= $nopts )
  if( $ndx < 10 ) then
    echo " ($ndx) $top_opts[$ndx]"
  else
    echo "($ndx) $top_opts[$ndx]"
  endif
  @ ndx++
end

echo " "; echo -n "Select option from above list: "
set opt = $<

if( $opt < 1 || $opt > $nopts ) then
  echo "Option $opt is not in above list"
  goto top_level_sel
else if( $opt != $nopts ) then
  set stop_opts = 0
else
  set stop_opts = 1
endif

#------------------------------------------------------------
#  top level options loop
#------------------------------------------------------------
while ( ! $stop_opts )
redo_1:
  if( $opt == 1 ) then
    echo " "; echo -n "Enter simulation type {initial,restart}: "
    set sub_opt = $<
    set type = `echo $sub_opt | tr '[a-z]' '[A-Z]'`
    if( $type != INITIAL && $type != RESTART ) then
      echo "$sub_opt is not {initial,restart}"
      goto redo_1
    endif
  else
sub_level_sel:
    set sub_ndx   = 1
    switch ( $opt )
      case 2
        set sub_opts = ("Start time" "Duration" "Time step" "Calendar{365,gregorian}" "Finished")
    endsw
    set nsub_opts = $#sub_opts
    echo " "
    while ( $sub_ndx <= $nsub_opts )
      if( $sub_ndx < 10 ) then
        echo " ($sub_ndx) $sub_opts[$sub_ndx]"
      else
        echo "($sub_ndx) $sub_opts[$sub_ndx]"
      endif
      @ sub_ndx++
    end

    echo " "; echo -n "Select option from above list: "
    set sub_opt = $<

    if( $sub_opt < 1 || $sub_opt > $nsub_opts ) then
      echo "Option $sub_opt is not in above list"
      goto sub_level_sel
    else if( $sub_opt != $nsub_opts ) then
      set finished = 0
    else
      set finished = 1
    endif
    if( ! $finished ) then
      echo " "; echo -n $sim_tim_prompt[$sub_opt] ": "
      set inps = $<
      echo $inps
      switch ( $sub_opt )
        case 1
          breaksw
        case 2
          breaksw
        case 3
          breaksw
        case 4
      endsw
      goto sub_level_sel
    else
      goto top_level_sel
    endif
  endif
  goto top_level_sel
end

exit 0
