#! /bin/sh

# Copyright (c)2011 Laurence Tratt <http://tratt.net/laurie>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# http://tratt.net/laurie/src/supuner
#
# supuner [-e] [-o <filename>] <command>
#
# This script takes a command and executes it, manipulating its stderr and
# stdout. Its name is short for SUPress UNless ERror (supuner).
#
# By default sunuper suppresses all output to stderr and stdout unless an
# error occurs in which case a combined copy of stderr and stdout is sent
# to stderr. With the -e ('echo') switch, the command's normal stdout and
# stderr are sent to the console. This allows one to both interactively
# view and log a commands output simultaneously
#
# If -o is specified, the captured stderr / stdout is combined and sent to
# that file.
#
# The exit code of supuner is the exit code of <command>.
#
# This is useful for "chatty" scripts used in cron jobs which output
# irrelevant information on stderr, which clogs up cron's output. This
# script allows one to only see the errors such scripts cause.


prog_name=`echo $0 | tr "/" "\n" | tail -1`

ec=0  # Set to 1 for 'echo to stdout'
op=0  # output path
dop=1 # Delete output path when script finishes
ae=0  # Set to != 0 to indicate an error with the programs arguments
while getopts ":ho:e" f
do
    case "$f" in
        e)   ec=1;;
        o)   op="$OPTARG"; dop=0;;
        [?]) ae=1; break;;
    esac
done

if [ $ae -eq 0 ]; then
    shift $((OPTIND-1))

    if [ $# -eq 0 ]; then
        ae=1
    else
        echo "$1" | grep "^\-" > /dev/null 2> /dev/null
        if [ $? -eq 0 ]; then
            ae=1
        else
            prog=$1
            shift
        fi
    fi
fi

if [ $ae -ne 0 ]; then
    echo "Usage: $prog_name [-e] [-o <filename>] <command>" >&2
    exit 1
fi

if [ $ec -eq 1 ]; then
    # We have to make sure the commands output is sent to the real
    # stderr / stdout as well as captured. This code is inspired by
    # http://www.travishartwell.net/blog/2006/08/19_2220
    p=`mktemp -u` # pipe
    mkfifo $p

    if [ $dop -eq 1 ]; then
        op=`mktemp`
        trap 'rm -f $op $p' 1 2 3 13 15
    else
        trap 'rm -f $p' 1 2 3 13 15
    fi

    exec 3>&1 4>&2
    tee $op < $p >&3 &
    tpid=$!
    exec > $p 2>&1

    $prog ${1+"$@"}
    ec=$?

    exec 1>&3 3>&- 2>&4 4>&-
    wait $tpid

    rm -f $p
else
    # Simple: execute the command and only spit something to stdout if a
    # non-zero exit code is returned.
    if [ $dop -eq 1 ]; then
        op=`mktemp`
        trap 'rm -f $op' 1 2 3 13 15
    fi
    
    exec 3>&1 4>&2
    exec > $op 2>&1
    $prog ${1+"$@"}
    ec=$?
    exec 1>&3 3>&- 2>&4 4>&-

    if [ $ec -ne 0 ]; then
		cat $op >&2
    fi
fi

if [ $dop -eq 1 ]; then
    rm -f $op
fi
exit $ec