:
#
# You have three choices here: 
#
# 1. Do nothing. The MAIL_TO variable will be set from the default
#    setting MailTo=xxxxx in [Unattended Backup Information] section 
#    of $BPDIR/bpinit/master.ini
#    This is the recommended choice (central configuration of settings).
#
# 2. Set MAIL_TO in this file to a hard coded email address
#    Email for any backup/restore done on this computer will go to this user
#
# 3. Set USE_OPERATOR_NAME=YES, optionally set DOMAIN
#    Email for any backup/restore done on this computer will go to the
#    $OPERATOR as determined by Backup Professional.  This person who queued
#    the task, or root for unattended scheduled backups.
#    If you set DOMAIN, all email will go to $OPERATOR@$DOMAIN where
#    $OPERATOR is the user who submitted the backup task.
#    Use this if you have a variety of operators submitting tasks 

MAIL_TO=""
USE_OPERATOR_NAME="NO"
#DOMAIN=""


MAILSUBJECT='BP $BACKUP Report for $NODE'
# Keep the above inside single quotes!!

# You should need to modify anything after this point
export MAIL_TO  USE_OPERATOR_NAME DOMAIN MAILSUBJECT

# Now set a few variables
AFTER_COMMAND=NO
BEFORE_COMMAND=NO

vf_count(){
  FCOUNT=`sed -n '/blocks/p' $1| wc -l 2>/dev/null| tr -d " "`
  ${ECHO} ${FCOUNT:=0}
}

mk_verify_summary(){
  # WARN TT may not always be true, could be TVfbkT
  if sed -n '2p;3q' $LOGF | grep "T.*T" >/dev/null
  then # Level 2 verify
    tail $LOGF > $TMP1
    if grep SUMMARY $TMP1 >/dev/null # Level 2 verify
    then 
      sed -n '/SUMMARY/,$p' $TMP1 | sed '/See the file:/d'
    else # Probably interrupted!!
      ${ECHO} "SUMMARY: Byte by Byte Verification"
      VFILES=`vf_count $LOGF`
      ${ECHO} "         $VFILES file(s) encountered"
      ${ECHO} "         VERIFY was interrupted or encountered a serious error!!"
      ${ECHO} "         Last file was: "
      sed -n '$p' $TMP1
    fi
  else # Level 1 verify, get last line only
      tail $LOGF |sed -n '$p'
  fi
  return 0
}

mk_restore_summary(){
  tail -300 $LOGF > $TMP1
  if grep "Permiss " $TMP1 >/dev/null
  then
    sed -n 's///g;/bkup:/p;/Permiss/,$p' $TMP1
  else
    if sed -n '1,30p;q' $LOGF | grep Permiss >/dev/null
    then # Sever error occured - show everything
      sed 's///g;/Serial Number/d;/Registered to/d;/Running as /d' $LOGF
    else # Too many files, show first 30 lines,  last 270  lines
      sed -n 's///g;/^Command/p;/bkup/p;10q' $LOGF
			${ECHO}
      sed -n 's///g;/Permiss /,30p;31q' $LOGF
      ${ECHO} "[more files] ..."
      tail -270 $LOGF > $TMP1
      sed 's///g;/Serial Number/d;/Registered to/d;/Running as /d' $TMP1
    fi
  fi
  return 0
}

set_ECHO() { 
  [ -d /usr/5bin ] && { PATH="/usr/5bin:${PATH}"; export PATH; }
  ECHO="echo -e"; 
  echo -e "hello" | grep "e h" >/dev/null && ECHO="echo"
  OS=`uname 2>/dev/null`
  case "$OS" in
      SunOS) [ -x /usr/5bin/echo ] && ECHO=/usr/5bin/echo ;;
  esac
  unset OS;
}


##############################
## Main program starts here ##
##############################

set_ECHO; export ECHO
if [ "$1" = stop ] 
then
  AFTER_COMMAND=YES
fi

if [ "$1" = start ]
then
  BEFORE_COMMAND=YES
fi

BTYPE=$2
OPERATOR=$3
CATALOG_FILE=$4

if [ -z "$BPDIR"  -a  -f /etc/default/bp.ini ]
then
      BPDIR=`grep BPDIR /etc/default/bp.ini|sed 's/^[	 ]*//;s/BPDIR=[  	]*//'`
fi

if [ ! -f "$CATALOG_FILE" ]
then
  CATALOG_FILE="$BPDIR/catalog.dir/$CATALOG_FILE"
fi
# This only works if you have NIS
DOMAIN=`domainname 2>/dev/null`
HOSTNAME=`hostname`
if [ -z "$DOMAIN" ]
then
  case "$HOSTNAME" in
    *.com) DOMAIN=`${ECHO} "$HOSTNAME" | sed 's/\.com//;s/.*\.//'` ;;
    *) USE_SHORT_NAME=YES;
  esac
fi

case "$BTYPE" in
  M)  BACKUP="Master Backup";;
  I)  BACKUP="Incremental";;
  S)  BACKUP="Selective Backup";;
  V)  BACKUP="Verify";;
  R)  BACKUP="Restore";;
esac

if [ "$AFTER_COMMAND"  = YES ]
then
  MSG=/tmp/msg.$$
  LOGF=$CATALOG_FILE; 
  TMP1=/tmp/bp_email.$$
  export MSG LOGF TMP1
  NODE=`uname -n`
  (
  ${ECHO} "This email message is being generated by Backup Professional." 
  ${ECHO} "It is from \"$BPDIR/bin/taskcmds\" on $NODE."
  ${ECHO} 
  ${ECHO} "Computer.......: $NODE"
  ${ECHO} "Backup type....: $BACKUP [$BTYPE]"
  ${ECHO} "Operator.......: $OPERATOR"
  ${ECHO} "Catalog file...: $CATALOG_FILE"
  ${ECHO} 
	if [ ! -f "$CATALOG_FILE" ]
	then
		${ECHO} "The catalog file \"$CATALOG_FILE\" is no longer "
		${ECHO} "present. Thus a summary cannot be generated."
		${ECHO} 
		${ECHO} "Consider checking the Backup History on the Server for details."
	fi
  case "$BTYPE" in
    M|I|S) tail -30 $CATALOG_FILE | sed -n '/==========/,$p' ;;
    R)  mk_restore_summary ;;  # Shows last ~200 files restored
    V)  mk_verify_summary ;;
  esac
  ) > $MSG 2>&1
  rm -f $TMP1
  if [ -z "$MAIL_TO" -a "$USE_OPERATOR_NAME" = NO ]
	then
			MAIL_TO=`$BPDIR/bin/bputil -g "Unattended Backup Information" MailTo "" $BPDIR/bpinit/master.ini 2>/dev/null`
			case "$MAIL_TO" in
				[Nn][Oo][Nn][Ee])
					rm -f $MSG 
					exit 0;;  # Set to "none", must exit!!
			esac
	fi
  if [ -z "$MAIL_TO" ]
  then
		USER=`${ECHO} $OPERATOR| sed 's/@.*//'`
		if [ "$USE_SHORT_NAME" = YES ]
		then
			MAIL_TO=`${ECHO} $OPERATOR| sed 's/@.*//'`
		else
			MAIL_TO="$USER"
		[ -n "$DOMAIN" ] && MAIL_TO="$USER@$DOMAIN"
		fi
  fi
  MAILSUBJECT=`eval ${ECHO} $MAILSUBJECT`
  mail -s "$MAILSUBJECT" $MAIL_TO < $MSG
  sleep 3

  # DEBUG - Uncomment below why this script is failing.
  # Look in the file /tmp/bp_email.msg for the answer
  # ${ECHO} "mail -s "$MAILSUBJECT" $MAIL_TO < $MSG" >/tmp/bp_email.msg
  # cat $MSG >> /tmp/bp_email.msg
  rm -f $MSG
fi
