#!/bin/sh

# This logviewer invokes the log viewer either through user's request
# or based on the suffix of the log file chosen.

VIEWERS_DIR=/usr/local/mpich/share

alogviewer=$VIEWERS_DIR/upshot/bin/upshot
clogviewer=$VIEWERS_DIR/jumpshot-2/bin/jumpshot
slogviewer=$VIEWERS_DIR/jumpshot-3/bin/jumpshot

progname="`echo $0 | sed 's:^\./\./:\./:'`"
print_usage() {
cat << EOF
Goal  : ${progname} invokes the appropriate log viewer based on suffix
        of the logfile or as instructed by user through option, -logtype.

Usage : ${progname} [-help] [-logtype=LOGFORMAT] [logfile]
-help               - print this help message
-logtype=LOGFORMAT  - invoke the log viewer to view logfile of type LOGFORMAT. 
                      LOGFORMAT can be one of the following : clog, slog, alog
                      Only one of the above 3 log formats is needed here.
 logfile            - the name of the logfile.  If option, -logtype=, is
                      NOT given, the log format will be determined by the
                      suffix of the logfile name.
EOF
}

if [ $# -eq 0 ] ; then
    echo "Error: Requires at least 1 argument, either -logtype or logfile !"
    echo
    print_usage
    exit 1
fi

logtype=
filename=
for arg in "$@" ; do
    case "$arg" in
        -help | --help | -h | --h )
            print_usage
            exit 0
            ;;
        --logtype=* | -logtype=* | logtype=* )
            logtype="`echo X$arg | sed -e 's/X-*logtype=//g'`"
            if [    "$logtype" != "alog" \
                 -a "$logtype" != "clog" \
                 -a "$logtype" != "slog" ] ; then
                echo "Error: Log format, $arg, is NOT supported !"
                echo
                print_usage
                exit 1
            fi
            ;;
        *.clog )
            if [ -z "$logtype" ] ; then
                logtype=clog
            fi
            filename=$arg
            ;;
        *.slog )
            if [ -z "$logtype" ] ; then
                logtype=slog
            fi
            filename=$arg
            ;;
        *.alog )
            if [ -z "$logtype" ] ; then
                logtype=alog
            fi
            filename=$arg
            ;;
        * )
            if [ -z "$logtype" ] ; then
                echo "Error: Cannot determine the log format of the input file, $arg !"
                echo
                print_usage
                exit 1
            fi
            filename=$arg
            ;;
    esac
done

# echo "The logtype = $logtype ; filename = $filename."
# exit 0

if [ "$logtype" = "clog" ] ; then
    if [ -x $clogviewer -a -s $clogviewer ] ; then
        echo "* If the clog file being processed is bigger than 4MB, you may"
        echo "* want to convert it to slog file using clog2slog. The logviewer"
        echo "* for slog, jumpshot-3, is better in handling big logfile."
        echo
	$clogviewer $filename
    else
        echo "$clogviewer is NOT present to process the $logtype file"
	exit 1
    fi
elif [ "$logtype" = "slog" ] ; then
    if [ -x $slogviewer -a -s $slogviewer ] ; then
	$slogviewer $filename
    else
	echo "$slogviewer is NOT present to process the $logtype file"
	exit 1
    fi
elif [ "$logtype" = "alog" ] ; then
    if [ -x $alogviewer -a -s $alogviewer ] ; then
	$alogviewer $filename
    else
	echo "$alogviewer is NOT present to process the $logtype file"
	exit 1
    fi
else
    echo "The logtype, $logtype, of input file, $filename, is NOT known"
    exit 1
fi
