#!/bin/sh

# Little helper which spits out output only if there was an error
# while running the desired command.  First argument taken as the
# message prefix
msg="$1"
name=$(echo "$msg" | tr ' ' '_')
[ -z "$VERBOSE" ] || echo "-------------------------------------------"
echo -n " I: $1"
shift

if [ -z "$DRY" ]
then
    if [ -z "$VERBOSE" ] || [ $VERBOSE -lt 2 ]; then
        tempfile=`mktemp -q --tmpdir ni-py.$name.$(date +"%y%m%d%H%M").XXXX`
        "$@" >$tempfile 2>&1 \
        	&& { echo -e "\tdone"; rm -f $tempfile; } \
        	|| { echo -en "\tfailed. ";
    			 [ -z "$VERBOSE" ] \
    				 && echo "See $tempfile" \
    				 || { echo "Details from $tempfile:"; cat $tempfile; }; }
	else
	    echo
	    "$@"
	fi
else
    echo -e "\tDRY: $*"
fi

