#!/usr/bin/env bash

# Copyright (C) 2009-2013 Bob Hepple
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

check_and_process_opts() {
    ARGS="
ARGP_DELETE=quiet
ARGP_VERSION=$VERSION
ARGP_PROG=$PROG
##############################################################   
#OPTIONS:
#name=default   sname arg       type range           description
##############################################################   
FORCE=''        f     ''        b    ''      always display output
WATCH='$WATCH'  w     time      i    0:1000  time to watch the app. 0==forever.
##############################################################   
ARGP_ARGS=[--] $ARGUMENTS
ARGP_SHORT=$SHORT_DESC
ARGP_USAGE=$USAGE"

    exec 4>&1
    eval $(echo "$ARGS" | argp.sh "$@" 3>&1 1>&4 || echo exit $? )
    exec 4>&-

    export PREFER_X=$USE_X

    NEW_ARGS=( "$@" )
    return 0
}

PROG=$(basename $0)
PROGDIR=$(dirname $0)
VERSION="1.1"
VERBOSE=""
WATCH=5
ARGUMENTS="<x-command>"

USAGE="Run a X command capturing stdout and stderr. \
If it gives an error, popup a viewer with the messages. \
If it survives for $WATCH seconds, exit this script and delete \
any temporary files."

TMP=/tmp/$$
TMPOUT=$TMP.out
TMPERR=$TMP.err
TMPFILE=$TMP.file

trap "/bin/rm -f $TMPOUT $TMPERR $TMPFILE" EXIT

export POSIXLY_CORRECT=true

NEW_ARGS=( )
check_and_process_opts "$@" && set -- "${NEW_ARGS[@]}"
unset POSIXLY_CORRECT

# At this point, all the options have been processed and removed from
# the arg list. We can now process $@ as arguments to the program.

# MAIN PROCESSING
[[ "$VERBOSE" ]] && set -x

"$@" >$TMPOUT 2>$TMPERR &
JOBPID=$!

(( WATCH > 0 )) && {
    if kill -0 $JOBPID &>/dev/null; then
        sleep 1
        if kill -0 $JOBPID &>/dev/null; then
            sleep $WATCH
            if kill -0 $JOBPID &> /dev/null; then
                # job seems to be OK, let's die nicely:
                exit 0
            fi
        fi
    fi
}

wait $JOBPID
STATUS=$?

if [[ $STATUS != 0 || "$FORCE" ]]; then
    (
        echo "$@ exited with status $STATUS"
        echo
        echo "STDERR:"
        echo "-------"
        echo
        cat $TMPERR
        echo
        echo "STDOUT:"
        echo "-------"
        echo
        cat $TMPOUT
    ) > $TMPFILE
    zenity --text-info --width 400 --height 400 --filename $TMPFILE
    #gxmessage -file $TMPFILE
fi

exit $STATUS
