#! /bin/ksh

# HWRF PORTABILITY SCRIPT
# Needed on Cirrus and Stratus para mode.

# This program is one half of a workaround for a problem involving
# preemption on CCS.  On CCS, when a job is preempted, it is killed
# and restarted under a different job id.  The biggest jobs tend to be
# preempted first, so our forecast job is the one that is preempted
# most often.  The forecast job submits the post processing jobs, so
# every time the forecast is submitted, four new post-processing jobs
# are submitted BUT the previous forecast job's post-processors are
# still running.

# To get those old post-processing jobs to die, we create a
# "simulation signature file" at the beginning of every forecast job.
# Every post-processor knows what the simulation signature should be
# ($SIMULATION_SIG), and checks the simulation signature file
# ($DATA/simulation.sig) to see the current forecast job's simulation
# signature.  If the signature has changed, the jobs exit by canceling
# themselves (err_exit).

# The other half of this workaround is in the top-level script that
# runs the forecast (kick_scripts/hwrf_forecast_kick.shell), which
# changes the signature file and then sleeps 4 minujtes to let the old
# post-processors die.

if [[ "$PARAFLAG" == YES && "$WHERE_AM_I" != jet && "$WHERE_AM_I" != zeus ]] ; then
    sync
    if [[ ! -z "$SIMULATION_SIG" && "$SIMULATION_SIG" != none ]] ; then
        sig=$( cat "$DATA/simulation.sig" )
        if [[ "$sig" != "$SIMULATION_SIG" ]] ; then
            sh ${utilscript}/setup.sh
            err=911
            err_exit "SIMULATION SIGNATURE CHANGED!! wanted=$SIMULATION_SIG now=$sig SCRIPT IS ABORTING DUE TO INCORRECT SIMULATION SIGNATURE"
        fi
    fi
fi
