#!/bin/sh
# This script is run as a pre-check to ensure that we have a rollback.tar.gz file for
# NDU purposes.  If not it tries to create one or fails.
LOCKFILE=/ifs/.ifsvar/upgrade/ROLLBACK_LOCK
ROLLBACKFILE=/ifs/.ifsvar/upgrade/rollback.tar.gz
ROLLBACKTMP=/ifs/.ifsvar/upgrade/rollback.tar.gz.tmp

# if run with 0 arguments, take the lock or block waiting.
# run ourselves again with 1 argument which does the actual
# work under the lock
# The name of the argument really doesn't matter, just the count of them.
case $# in
       0)
               exec lockf $LOCKFILE $0 under-lock
               ;;
       1)
               # if the rollback tar exists, we do nothing.
               if [ ! -e "$ROLLBACKFILE" ]
               then
                  rm "$ROLLBACKTMP" 2>/dev/null
                  /usr/sbin/isi_Upgrade_Agent.py CreateRollback "$ROLLBACKTMP" && mv "$ROLLBACKTMP" "$ROLLBACKFILE" || exit 1
                  # verify that we can install the rollback image
                  /usr/sbin/isi_Upgrade_Agent.py InstallFile "$ROLLBACKFILE" || exit 1
               fi
               ;;
esac
