#!/sbin/sh

################################################################################
#
#	Copyright (c) 1999-2005 by Sun Microsystems, Inc. All Rights Reserved.
#	Copyright 1992-95 AT&T Global Information Solutions
#
# ident "@(#)lucancel.sh 5.6     05/10/24 SMI"
#
# USAGE:        lucancel
# FUNCTION:     Cancels a job that has been scheduled
# INPUT:        NONE
# OUTPUT:       NONE
# DEV:          JKJ
#
# Note: performance testing has proven that "/sbin/sh" provides the best performance over
# /bin/sh and /bin/ksh.
#
################################################################################

LU_PROG_FULL_PATH="$0"
LU_PROG_NAME="`basename ${LU_PROG_FULL_PATH}`"; export LU_PROG_NAME

################################################################################
# Name:		usage
# Description:	output command line usage information; then call exit to terminate execution.
# Local Prefix:	<none>
# Arguments:	$1 = exit code for script ("" defaults to "1").
# Example:	usage 1
# Returns:	<none> 
################################################################################

usage()
{
  ${LUPRINTF} -p2 "`gettext 'USAGE: %s [ -X ] [ -l error_log ] \
[ -o outfile ]'`" "${LU_PROG_NAME}"
  if [ -z "$1" ] ; then
    exit 1
  fi
  exit "$1"
}

################################################################################
# Name:		<main>
# Description:	Main code (outside of any function definitions) - executed at script startup.
# Local Prefix:	<none>
# Arguments:	$0...$n = All arguments specified by user on command line that invoked this script.
################################################################################

# Dot the defaults file.

if [ ! -s /etc/default/lu ] ; then
  echo "${LU_PROG_NAME}: ""`gettext 'ERROR: Live Upgrade not installed properly (/etc/default/lu not found).'`"
  exit 1
fi
. /etc/default/lu

LUBIN=${LUBIN:=/usr/lib/lu}
### LU_OPTFS=${LU_OPTFS:=/etc/lu/optfs}

# Dot the Live Upgrade library functions.

if [ ! -s $LUBIN/lulib ] ; then
  echo "${LU_PROG_NAME}: ""`gettext 'ERROR: The Live Upgrade product is not installed properly (${LUBIN}/lulib not found).'`"
  exit 1
fi

. $LUBIN/lulib

# Check for existence and non-zero size of lutab file.

#
# Load defaults
#
if [ -f /etc/default/lu ] ; then
	. /etc/default/lu
fi

LUBIN=${LUBIN:=/usr/lib/lu}
### LU_OPTFS=${LU_OPTFS:=/etc/lu/optfs}
. $LUBIN/lulib
if [ $? != 0 ] ; then
	echo "${LU_PROG_NAME}: `gettext \"ERROR: Live Upgrade not installed properly\"`" >&2
	exit 1
fi

  ######################################################################################
  ##################### Command line option and argument processing ####################
  ######################################################################################

# Reset all command line parse flags to default values.
flag_o="" # -o f - output file path.
flag_l="" # -l f - log file path.
flag_x="" # -x n = set debug level to n (PRIVATE).

while [ $# -ne 0 ] ; do
  while getopts l:o:x:X c
  do
    case $c in
      l) # -l f - error log file path.
	 # This overrides the LU_ERROR_LOG_FILE setting read from /etc/default/lu
	 lulib_cannot_duplicate_option "${flag_l}" "${OPTARG}" "-l"
	 ${LUPRINTF} -lp2D - "`gettext 'Verifying that the error log file <%s> specified can be created and appended to.'`" "${OPTARG}"
	 ERRMSG="`${LUPRINTF} -c \"${OPTARG}\" 2>&1`"
	 if [ $? -ne 0 ] ; then
	   [ -n "${ERRMSG}" ] && ${LUPRINTF} -elp2 '%s' "${ERRMSG}"
	   ${LUPRINTF} -Eelp2 "`gettext 'Argument <%s> to -l option may not be created or appended to.'`" "${OPTARG}"
	   exit 3
	 fi
	 flag_l="${OPTARG}"
	 lulib_set_error_log_file "${flag_l}"
	 ;;
      o) # -o f - output file path.
	 # This overrides the LU_SESSION_LOG_FILE setting read from /etc/default/lu
	 lulib_cannot_duplicate_option "${flag_o}" "${OPTARG}" "-o"
	 ${LUPRINTF} -lp2D -  "`gettext 'Verifying that the session log file <%s> can be created and appended to.'`" "${OPTARG}"
	 ERRMSG="`${LUPRINTF} -c \"${OPTARG}\" 2>&1`"
	 if [ $? -ne 0 ] ; then
	   [ -n "${ERRMSG}" ] && ${LUPRINTF} -elp2 '%s' "${ERRMSG}"
	   ${LUPRINTF} -Eelp2 "`gettext 'Argument <%s> to -o option may not be created or appended to.'`" "${OPTARG}"
	   exit 3
	 fi
	 flag_o="${OPTARG}"
	 lulib_set_session_log_file "${flag_o}"
	 ;;
      x) # -x n - set debug level to n (PRIVATE).
	 # This overrides the default setting read from /etc/default/lu
	 lulib_cannot_duplicate_option "${flag_x}" "${OPTARG}" "-x"
	 /bin/test "${OPTARG}" -ge 0 2>/dev/null
	 if [ $? -gt 1 ] ; then
	   ${LUPRINTF} -Eelp2 "`gettext 'Argument <%s> to -x option is not a number.'`" "${OPTARG}"
	   usage 3
	 fi
	 flag_x="${OPTARG}"
	 lulib_set_debug "${flag_x}"
	 ;;
      X) # -X - set XML output mode.
	  lulib_set_output_format 'xml'
	  ;;
      \?) # unknown - option.
	  usage 3
	  esac
  done

  # Found either end of arguments, +option, or non-option argument; shift out
  # what has been processed so far; if a non-option argument is present
  # capture it and continue processing the command line arguments.
  shift `/bin/expr $OPTIND - 1`
  OPTIND=1
  if [ $# -ne 0 -a "$1" = '+X' ] ; then
      # +X - set TEXT output mode.
      lulib_set_output_format 'text'
      shift
  else
    break
  fi
done

# Fixup debug, session log, and error log settings
lulib_fixup_startup_settings

  ######################################################################################
  ############ Validate all command line arguments and options as possible #############
  ######################################################################################

# If any command line arguments provided, exit with error.
if [ "$#" -ne "0" ] ; then
  ${LUPRINTF} -Eelp2 "`gettext 'Command line arguments <%s> not allowed.'`" "$*"
  usage 3
fi

trap "" 1 2 3 15

if [ ! -f $COPYLOCK ] ; then
  ${LUPRINTF} -lp2D - "`gettext 'No Copy Lock file <%s> exists.'`" "${COPYLOCK}"
  ${LUPRINTF} -lp2 "`gettext 'No jobs are currently scheduled.'`"
  exit 1
fi

lulib_validate_lulock

case "$?" in
  0) # COPY_LOCK doesn't exist
     ${LUPRINTF} -lp2D - "`gettext 'No Copy Lock file <%s> exists.'`" "${COPYLOCK}"
     ${LUPRINTF} -lp2 "`gettext 'No jobs are currently scheduled.'`"
     exit 1
     ;;

  1) #COPY_LOCK existed but was no longer valid  and hence removed
     ${LUPRINTF} -lp2D - "`gettext 'A previously existing copy lock <%s> was invalid and has been removed.'`" "${COPYLOCK}
     ${LUPRINTF} -lp2D - "`gettext 'No jobs are currently scheduled.'`"

  2) # COPY_LOCK exists and there is a valid scheduled job
     #DOT in INFO from COPY_LOCK
     . $COPYLOCK
 
     ${LUPRINTF} -lp2D - "`gettext 'A Copy Lock file <%s> exists: Status <%s> Source BE <%s> Target BE <%s>.'`" "${COPYLOCK}" "${CL_STATUS}" "${CL_SOURCE_BE}" "${CL_TARGET_BE}"
   
     at -l | grep $CL_AT_JOB_NUM > /dev/null 2>&1
     if [ $? = 0 ] ; then
        ERRMSG="`at -r $CL_AT_JOB_NUM $CL_UPGRADE_AT_JOB_NUM 2>&1`"
        if [ $? != 0 ] ; then
		[ -n "${ERRMSG}" ] && ${LUPRINTF} -Eelp2 '%s' "${ERRMSG}"
                ${LUPRINTF} -Eelp2 "`gettext 'Failed to cancel cron job.'`"
                rm -f $COPYLOCK
                exit 1
        fi
     else
        ${LUPRINTF} -Welp2 "`gettext 'The cron job for this copy does not exist. Assuming it has already been canceled.'`"
        rm -rf $COPYLOCK
        exit 1
     fi
     ;;
    
   3) #COPY_LOCK exists and a valid running job exists.
      #DOT in INFO from COPY_LOCK
      . $COPYLOCK

     if [ "$CL_STATUS" != "SCHEDULED" ] ; then
     	${LUPRINTF} -Eelp2 "`gettext 'The job is not in the scheduled state. Cannot cancel.'`"
     	exit 1
     fi
     ;;
    
   *) #Invalid switch

      ${LUPRINTF} -Eelp2 "`gettext 'Unable to cancel jobs: invalid return code from lulib_validate_lulock() (logicerror)'`"
      exit 1
      ;;

esac

# Exit with a good status
rm -f $COPYLOCK
exit 0
