#!/bin/sh

#
# This is just to make it easy to start up the rmgdiff.awk script.
#
DIFF=/usr/bin/diff
NAWK=/usr/bin/nawk
FILE=/usr/bin/file
RMGDIFF_GUI=${RMGDIFF_GUI:-/usr/bin/X11/mgdiff}
RMGDIFF_AWK=`dirname $0`/rmgdiff.awk

#
# You shouldn't need to edit beneath here.
#

SHOW_FILE_TYPES="TRUE"
USE_CVS=""
DEBUG=""
USE_GUI="TRUE"
RMGDIFF_VERSION=""



Usage() {
cat <<-EOF

Usage: $0 [-b] [-c] [-d] [-g <gui>] [-n] [-v] <dir1> <dir2>

    -b: basic reporting (no file type info will be printed)
    -c: cvs files will be included in diff
    -d: print debugging information
    -g: which gui to use
    -n: no gui will pop up
    -v: version

EOF
}

while getopts "bcdg:nv" OPT
do
    case "$OPT" in
        b)  SHOW_FILE_TYPES=""
            ;;
        c)  USE_CVS="TRUE"
            ;;
        d)  DEBUG="TRUE"
            ;;
        g)  RMGDIFF_GUI="$OPTARG"
            ;;
        n)  USE_GUI=""
            ;;
        v)  RMGDIFF_VERSION="TRUE"
            ;;
        \?) Usage
            exit 1
            ;;
    esac
done
shift `expr $OPTIND - 1`

# If the user just wants the version ...
if [ -n "$RMGDIFF_VERSION" ]
then
    exec "$NAWK" -v version="$RMGDIFF_VERSION" -f "$RMGDIFF_AWK"
fi

if [ $# -lt 2 ] || [ $# -gt 2 ]
then
    Usage
    exit 1
fi

if [ ! -x "$RMGDIFF_GUI" ]
then
    echo "$0: $RMGDIFF_GUI is not executable." 1>&2
    exit 1
fi

if [ -d "$1" ]
then
    if [ -d "$2" ]
    then
        if [ ! -x "$DIFF" ]
        then
            echo "$0: \"$DIFF\" is not executable." 1>&2
            exit 1
        fi

        if [ ! -x "$NAWK" ]
        then
            echo "$0: \"$NAWK\" is not executable." 1>&2
            exit 1
        fi

        if [ ! -x "$FILE" ]
        then
            echo "$0: \"$FILE\" is not executable." 1>&2
            exit 1
        fi

        exec "$DIFF" -rq $1 $2 | "$NAWK" -v debug="$DEBUG" \
                                         -v dir1="$1" \
                                         -v dir2="$2" \
                                         -v file_cmd="$FILE" \
                                         -v rmgdiff_gui="$RMGDIFF_GUI" \
                                         -v show_file_types="$SHOW_FILE_TYPES" \
                                         -v use_cvs="$USE_CVS" \
                                         -v use_gui="$USE_GUI" \
                                         -v version="$RMGDIFF_VERSION" \
                                         -f "$RMGDIFF_AWK"
    else
        echo "$0: dir2=\"$2\" is not a directory." 1>&2
    fi
else
    echo "$0: dir1=\"$1\" is not a directory." 1>&2
fi
