#! /bin/ksh

set -e -u -x
nidymdh="$1"

# This script should only ever be called by hwrf_output.sh.  This handles the
# archiving of the output.

if [[ "$WHERE_AM_I" == jet || "$WHERE_AM_I" == zeus ]] ; then
    gnutar=tar
else
    gnutar=/opt/freeware/bin/tar
fi

if [[ "$WHERE_AM_I" == jet ]] ; then
    export PATH=$PATH:/opt/hpss
fi

function store {
    set -x
    target=$1
    shift 1
    five=$( echo "$ARCHIVE_TARGET_DIR" | cut -c1-5 )
    
    if [[ "$five" == hpss: ]] ; then
        ARCHIVE_TARGET_DIR=$( echo "$ARCHIVE_TARGET_DIR" | cut -c6- )
        DEST=hpss
    elif [[ "$five" == disk: ]] ; then
        ARCHIVE_TARGET_DIR=$( echo "$ARCHIVE_TARGET_DIR" | cut -c6- )
        DEST=disk
    elif [[ "$WHERE_AM_I" == jet ]] ; then
        DEST=disk
    else
        DEST=hpss
    fi

    if [[ "$DEST" == disk ]] ; then
        # Create a gzipped tar archive on MSS:
        $gnutar -czvf $ARCHIVE_TARGET_DIR/${target}.gz $*
        ret="$?"
    else
        # Compressed archives are forbidden on HPSS, so just tar results:
        echo '======================== RUN HPSSTAR ========================'
        export VERBOSE_HPSSTAR=yes
        hpsstar put $ARCHIVE_TARGET_DIR/${target} $*
        ret="$?"
        echo '======================== BACK FROM HPSSTAR ========================'
        echo "RETURN STATUS $ret"
    fi
    return $ret
}

donefile="$DATA/com-archive.done"
rm -f "$donefile"
if [ $PARAFLAG == YES ]; then
    # Copy LOG files 
    for x in ${DATA}/hwrf*err ; do
        y=$( basename "$x" )
        /bin/cp -f $x ${COMOUT}/${nidymdh}.$y
    done



    pdir=$CDNOSCRUB/${user}/$EXPT
    sdir=$CDNOSCRUB/${user}/cycstatus/$EXPT/
    ddir=$CDNOSCRUB/${user}/diagtrak/$EXPT/

    $USHhwrf/hwrf_mkdirp.sh $pdir $sdir $ddir

    # Put the atcfunix file on noscrub:
    cp -p ${COMOUT}/${nidymdh}.trak.hwrf.atcfunix $pdir/.
    cp -p ${COMOUT}/${nidymdh}.diagtrak.hwrf.3hourly* $ddir/.
    cp -p ${COMOUT}/*.htcf $ddir/.
    cp -p ${COMOUT}/a*.dat $ddir/.

    # Copy the cycle status file to noscrub
    /bin/cp -pf ${COMOUT}/${nidymdh}.cycle.status $sdir/.

    # Get the timings from all *.out files:
    grep -i timing "$DATA"/hwrf_*.out > $sdir/$nidymdh.timings

    # Archive the results on HPSS or MSS.
    cd $COMOUT

    if [[ ! -z "${GRIB_ARCHIVE_PATH:-}" ]] ; then
        if ( ! echo "$GRIB_ARCHIVE_PATH"  | grep / > /dev/null ) ; then
            echo "$( date ): ignoring $GRIB_ARCHIVE_PATH because it is not a path"
            echo "$( date ): include at least one slash (/) or I'll ignore your path"
        else

        if [[ ! -d "$GRIB_ARCHIVE_PATH" ]] ; then
            $USHhwrf/hwrf_mkdirp.sh "$GRIB_ARCHIVE_PATH"
        fi
        $gnutar -cvzf "$GRIB_ARCHIVE_PATH/${nidymdh}.tar.gz" $( ls -1 | \
            perl -ne '
                /wrfout|grb10th|wrfinput|out4wave|wrfanl|wrfbdy/ and next;
                /eprs_i|prs_m|sat_m|GRADS|hwrfesat_/ and next;
                /\.(?:T|U|V|EL|TXY|WTSW|GRADS)\./ and next ; 
                /(?:\.err|fort\.65|\.a|\.b)/ and next;
                print' );
        ret="$?"
        if [[ "$ret" == 0 ]] ; then
            echo "$( date ): GRIB ARCHIVE DONE" >> "$donefile"
        else
            sh ${utilscript}/setup.sh
            err=911
            err_exit "ABORT: COULD NOT CREATE $GRIB_ARCHIVE_PATH/${nidymdh}.tar.gz: return status from tar was $ret"
        fi
        fi
    fi

    # NOTE: $MAKE_AB_ARCHIVE is not set to anything as of the writing
    # of this script, so *.ab.tar or *.ab.tar.gz archive will never be
    # made.  That is intentional, to conserve space.
    now=$( ndate )
    if [ $OCEAN == HYCOM ]; then
        # Create a gzipped tar archive of everything except .a and .b files:
        if ( store ${nidymdh}.tar $( ls -1 | egrep -v '[0-9]\.[ab]$' ) ) ; then
            echo "$( date ): ARCHIVE DONE" >> "$donefile"
        fi                
        
        # Store track and all hourly .a and .b files (not restart
        # files) in a .ab.tar archive if $MAKE_AB_ARCHIVE is set:
        if [[ "$MAKE_AB_ARCHIVE" == YES ]] ; then
            store ${nidymdh}.ab.tar $( ls -1 | egrep '[0-9]\.[ab]$' ) ${nidymdh}.trak.hwrf.atcfunix
        fi
    else
        cd $COMOUT
        if ( store ${nidymdh}.tar ./* ) ; then
            echo "$( date ): ARCHIVE DONE" >> "$donefile"
        fi
    fi
fi
