#!/bin/bash
# Runs a given script, greps the output for given pattern(s),
# mails to specified users
#
# Version
#   $Revision: 1.2 $
#
# Date
#   $Date: 2004-03-17 18:28:06+00 $
#
# Modification History
#   $Log: mail_script_output.sh,v $
#   Revision 1.2  2004-03-17 18:28:06+00  shipleyk
#   fix report generation & mailing
#
#   Revision 1.1  2004-03-09 22:46:51+00  shipleyk
#   Initial revision
#
#

MAILUSERS=""
PATS=()

usage="Usage: mail_script_output.sh -M recipient(s) [ -S subj ] [ -G regexp ] script args..."
usage="$usage (multiple -M, -G options are allowed)"
while getopts "M:S:G:" OPTS ; do
        case $OPTS in 
                M) if [ -n "$MAILUSERS" ] ; then
                      MAILUSERS="$MAILUSERS,";
                   fi
                   MAILUSERS="$MAILUSERS$OPTARG";; # people to email
                S) SUBJECT=$OPTARG;; # email subject line
                G) PATS[${#PATS[*]}]="${OPTARG}";; # pattern(s) to search for
                *) echo $usage; exit 0;;
        esac
done

# function to determine if a given argument needs to be quoted.
needsquote(){
  val=$1
  # Check for argument delimeter (space)
  tmp=( ${val} )[0];
  if [ ${#tmp} -lt ${#val} ] ; then
    return 1
  else
    # Check for special characters
    # (not alphanumeric, dash, underscore, dot or slash)
    tmp=`echo "$val" | grep -e "[^-_./A-Za-z0-9]"`
    if [ -n "$tmp" ] ; then
      return 1
    else
      return 0
    fi
  fi
}


CMDLINE="$0"
i=$[$OPTIND-1]
while [ $i -gt 0 ] ; do
  arg=$1;
# check for arguments that need quoting
  needsquote "$arg"
  if [ $? -gt 0 ] ; then
    arg=`echo "$arg" | sed "s/\'/\'\\\\\'\'/g"`  # escape single-quotes
    CMDLINE="${CMDLINE} '${arg}'"  # quote the argument
  else
    CMDLINE="${CMDLINE} ${arg}"
  fi
  shift
  i=$[$i-1]
done

SCRIPT=$1
shift

# name the temporary file after the machine name & process id;
# this guarantees uniqueness
MACH=`uname -n`
PID="$$"
TMPFILE="/aim/temp/mailout-${MACH}${PID}.tmp"

# run the script on the args, send both stdout & stderr to TMPFILE
$SCRIPT "$@" 2>&1 | cat > $TMPFILE

# Get command line with quoted arguments, to show in output
cmdlin="$SCRIPT"
for arg in "$@" ; do
# check for arguments that need quoting
  needsquote "$arg"
  if [ $? -gt 0 ] ; then
    arg=`echo "$arg" | sed "s/\'/\'\\\\\'\'/g"`  # escape single-quotes
    cmdlin="${cmdlin} '${arg}'"  # quote the argument
  else
    cmdlin="${cmdlin} ${arg}"
  fi
done
CMDLINE="${CMDLINE} ${cmdlin}"

if [ -z "${SUBJECT}" ] ; then
   SUBJECT=$cmdlin;   # use the command line for SCRIPT as default subject
fi

# Create the report
REPORT=$(cat << EOF
Command line: $CMDLINE

----------
`eval 'i=0; while [ \$i -lt \${#PATS[*]} ] ; do \
         PAT=\${PATS[\$i]}; \
         echo "Lines that matched with \"\$PAT\":"; \
         if [ -n "\$PAT" ] ; then grep -n -e "\$PAT" \$TMPFILE ; fi; \
         echo "----------"; \
         i=\$[\$i+1]; done'`
Output:
----------
`eval 'cat $TMPFILE'`
EOF)

if [ -z "${MAILUSERS}" ] ; then
  echo "No mail recipients specified:"
  echo "$REPORT" | cat >&1   # send to stdout if no mail recipients
else
  echo "$REPORT" | /bin/mailx -s "$SUBJECT" $MAILUSERS  # send mail
fi

rm $TMPFILE
