#!/bin/sh
#=========================================================================
# name:         checkbu-run
# description:  check if a backup is running
# author:       G.R.Keech
# date:         1998-07-15
# peer review:  Pending.
# $Id: checkbu-run,v 1.1 1999/03/04 02:56:02 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][-v]
  echo eg:    $PROGNAME -c test
  echo This shows the process id of the backup
  echo returns:
  echo 0    backup running.
  echo 1    backup not running.
  echo 2    pid corresponds to an active process that is not backup.
  echo 3    pid does not correspond to an active process.
  echo
  echo Use configuration name of default if no other configuration is indicated.
  echo 
  echo -c   configuration name
  echo -v   verbose
  echo
  echo Version `version`.
  exit 1
}

#============================================================================
#                 Start of main block of script.
#----------------------------------------------------------------------------
os=`uname -s` # Operating system.

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

while [ $# -ne 0 ]
do
  case $1 in
    "-v")  # verbose
	    shift
	    echo Determine state of backups.
	    echo This program is meant to be called from other programs
	    echo and has output that is deliberately not very user-friendly.
	    echo Version `version`.
	    ;;
    "-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

pid_file=/var/tmp/backup-${CONFIG}.pid

if [ -f $pid_file -a -s $pid_file ]
then
  # The PID file exists.  Test that it corresponds to a running backup.
  pid=`grep "pid:" $pid_file|awk '{print $2}'`
  if [ ${pid}x != x -a ${pid}x = `ps aux|awk '{print $2}'| grep $pid`x ]
  then
    # The pid corresponds to a current process reported by ps.
    if [ ${os}x = "SMP_DC.OSxx" ]
    then
      full_progname=`ps -ef|awk '{print $2, $8}'| grep "^$pid"|awk '{print $2}' `
    else
      full_progname=`ps auxw|awk '{print $2, $12}'| grep "^$pid"|awk '{print $2}' `
    fi

    progname=`basename $full_progname`
    if [ ${progname}x = rkbackupx ]
    then
      # The process identified by the PID is an instance of the backup program.
      echo $pid
    else
      #The pid corresponds to an active process that is not backup.
      exit 2
    fi
  else
    # The pid does not correspond to an active process.
    exit 3
  fi
else
  # The PID file does not exist or is empty, so no backup with this 
  # configuration is running.
  if [ ! -s $pid_file ]
  then
    # File is empty.  This should never be the case, so remove.
    rm -f $pid_file 2>/dev/null
  fi
  exit 1
fi
