#! /bin/ksh

if [[ ! -d "$USHhwrf" ]] ; then
    echo "Please set \$USHhwrf to the location of the hwrf ush/ directory." 1>&2
    echo "SCRIPT IS EXITING DUE TO INCORRECT ENVIRONMENT VARIABLES" 1>&2
    exit 9
fi

me="$0"

function die {
    echo "$*" 1>&2
    sh ${utilscript}/setup.sh
    err=911
    err_exit "ABORTING --- $*"
    exit
}

function usage {
    cat<<EOF 1>&2
This script parses hwrf_namelist.input to create the wrf.exe's namelist.input
file.  Execution syntax:

    $me [ options ]

Options:

    -s YYYYMMDDHH -- start time (Default: \$YMDH)
    -h N -- length of simulation in hours.  Default: 126
    -s -- do a 1 minute simulation

    -a -- turn on analysis mode (set analysis=YES).  Implies -s
    -g -- turn on ghost mode (set ghost==YES).  Implies -a and -s

    -i2 N -- domain d02 i_parent_start location
    -j2 N -- domain d02 j_parent_start location
    -i3 N -- domain d03 i_parent_start location
    -j3 N -- domain d03 j_parent_start location

    -i infile -- location of input hwrf_namelist.input file (use - for stdin)
                 Default: \$PARMhwrf/hwrf_namelist.input
    -o outfile -- location of output file.  Default: send to stdout stream.

If more than one of -a, -g, -s and -h are specified, the forecast
length will be decided by the last in the argument list.
EOF

    while [[ $# -gt 0 ]] ; do
        echo "$1" 1>&2
        shift 1
    done
    die
}

function two {
    if [[ $# -lt 2 ]] ; then
        usage "The $1 option requires an argument ($#<2)." \
              "SCRIPT IS ABORTING DUE TO INCORRECT ARGUMENTS"
    fi
}

ndate="${NDATE_PATH:-${NWPROD}/util/exec/ndate}"

export start=$YMDH
export endmin=''
length=126
infile="$PARMhwrf/hwrf_namelist.input"
outfile='-' # "-" means stdout
export istart_d02=''
export istart_d03=''
export jstart_d02=''
export jstart_d03=''
export anlname=''
export analysis=NO

# Defaults are set for operational setup.  All of these variables
# should be set upon entry in non-operational simulations:
export IO_FORM="${IO_FORM:-1}"
export ATMOS_DOMAINS="${ATMOS_DOMAINS:-2}"
export RUN_PREP_HYBRID="${RUN_PREP_HYBRID:-YES}"
export GFS_SOURCE="${GFS_SOURCE:=GRIB_1x1_REDUCED}"
export GWD="${GWD:-YES}"

while [[ "$#" -gt 0 ]] ; do
    case "$1" in
        -i2) two $* ; export istart_d02="$2" ; shift 2 ;;
        -j2) two $* ; export jstart_d02="$2" ; shift 2 ;;
        -i3) two $* ; export istart_d03="$2" ; shift 2 ;;
        -j3) two $* ; export jstart_d03="$2" ; shift 2 ;;
        -i)  two $* ;        infile="$2"     ; shift 2 ;;
        -o)  two $* ;        outfile="$2"    ; shift 2 ;;

        # Special modes for analysis and ghosting:
        -a)           export analysis=YES
                             length=0
                      export endmin=1        ; shift 1 ;;
        -g)           export ghost=YES
                      export analysis=YES
                             length=0
                      export endmin=1        ; shift 1 ;;

        # -h sets end ymdh, discards end minute:
        -h)  two $* ;        length="$2"
                      export endmin=''       ; shift 2 ;;

        # -s sets end ymdh to start ymdh, sets end minute to 1:
        -s)           export length=0   # end ymdh same as start
                      export endmin=1   # but end minute is 1 minute later
                                            shift 1 ;;

        *) usage "don't know what to do with argument $1" \\
                 "SCRIPT IS EXITING DUE TO INCORRECT ARGUMENTS"
    esac
done

# ensure that the start J is odd if it is set
if [[ ! -z "$jstart_d02" ]] ; then
    js=`expr ${jstart_d02} + 1`
    jstart_d02=`expr ${js} / 2 \* 2 - 1`
fi
if [[ ! -z "$jstart_d03" ]] ; then
    js=`expr ${jstart_d03} + 1`
    jstart_d03=`expr ${js} / 2 \* 2 - 1`
fi

export end=$( ndate $length $start )

if [[ ! -s "$infile" && "$infile" != "-" ]] ; then
    die "ERROR: $infile does not exist."
fi

if [[ "$outfile" != '-' ]] ; then
    rm -f "$outfile"
    if [[ -e "$outfile" || -L "$outfile" ]] ; then
        die "ERROR: unable to delete file $outfile"
    fi
fi

"$USHhwrf/hwrf_stream_parse.pl" "$infile" "$outfile"

if [[ "$outfile" != '-' ]] ; then
    if [[ ! -s "$outfile" ]] ; then
        die "ERROR: stream parser failed: $outfile is empty or non-existant."
    fi
fi
