#!/bin/sh
#=========================================================================
# name:         abortbu
# description:  abort a backup that is currently running.
# author:       G.R.Keech
# date:         1998-08-17
# peer review:  Pending.
# $Id: abortbu,v 1.1 1999/03/04 02:56:01 rkeech Exp $
#=========================================================================
version () {
  ver='$Revision: 1.1 $'
  echo $ver|sed 's/\$//g; s/Revision://g'|awk '{print $1}'
}
#--------------------------------------------------------------------------
usage () {
  echo usage: $PROGNAME [-c configuration-name]
  echo eg:    $PROGNAME -c test
  echo Abort a backup that is currently running using a SIGQUIT.
  echo Version `version`.
  exit 1
}
#---------------------------------------------------------------------------
# Test that command is available.
available () {
  cmd=`type $1 2>/dev/null`
  if [ $? -ne 0 ]
  then
    echo "${PROGNAME}: Command $1 does not seem to be available." >> $MAINLOG
    exit 1
  fi
}
#============================================================================
#                 Start of main block of script.
#----------------------------------------------------------------------------
# Define variables.
os=`uname -s` # Operating system.

BASEDIR=/usr/  # The base directory for location of scripts and configs.
CHECK_RUN=${BASEDIR}sbin/checkbu-run

CONFIG=default  # sets default
PROGNAME=`basename $0`

#----------------------------------------------------------------------------
# Check availablility of pre-requisite scripts.
available $CHECK_RUN
#----------------------------------------------------------------------------
# Process command line arguments.
while [ $# -ne 0 ]
do
  case $1 in
    "-c") # configuration
	   shift
          # Manage the configuration name.
          if [ $# = 1 ]
          then
            CONFIG=$1
	      shift
	    fi
	    ;;
    "--version") 
	  echo $PROGNAME Version `version`.; 
	  echo Written by G.Richard Keech, 1998.
	  exit
	    ;;
    *) usage;;
  esac
done # processing command line arguments

#----------------------------------------------------------------------------
# Check if the backup is currently running.
pid=`$CHECK_RUN -c $CONFIG`
state=$? # if this gets set to zero then the backup is currently running.

#----------------------------------------------------------------------------
# Abort the backup if appropriate.
case $state in
  0) # Backup is currently running.
    kill -3 $pid
    echo Sent SIGQUIT to the currently running backup process ${pid}.
    echo This may not take effect immediately.
    n=0
    while [ $n -le 10 ]
    do
      if [ -f /var/tmp/backup-${CONFIG}.pid ]
      then
	sleep 1
      else
	echo Backup successfully aborted.
	exit 0
      fi
      n=`expr $n + 1`
    done
    echo Backup still has not aborted.
    exit 1
    ;;

  *) # Backup not running.
    echo Backup for configuration \"${CONFIG}\" is not currently running.
    ;;
esac