#!/bin/tcsh -f
# JLdL 15Apr07.
#
# Copyright (C) 2007 by Jorge L. deLyra <delyra@fma.if.usp.br>.
# This program may be copied and/or distributed freely. See the
# _ terms and conditions in /usr/share/doc/<package>/copyright.
#
# A shell wrapper for the mult.bin program, to handle options
# _ and command-line arguments.
#
# Record the name this script was called with.
set name = `basename $0`
#
# Define the path to the binary program.
set prog = /usr/lib/arith-utils/mult.bin
#
# Initialize a variable for the list of files.
set files = ""
#
# Process the command-line arguments.
foreach cla ( $* )
    #
    # Detect options.
    if ( "`echo -n $cla | cut -c 1`" == "-" ) then
	#
	# Now process the options.
	switch ( $cla )
	case "-h":
	case "--help":
	    #
	    # Print a usage message.
	    echo "usage: $name [<file> <file>... ]"
	    echo "       multiply numbers received from stdin or found within"
	    echo "       a set of files submitted as command-line arguments;"
	    echo "       each '<file>' must be the name of an existing file;"
	    echo "       to get all the details run the command 'man $name'"
	    exit 0
	    breaksw
	default:
	    #
	    # Print an error message.
	    echo "${name}: ERROR: unknown option $cla; try -h to get help"
	    exit 1
	    breaksw
	endsw
    #
    # Process non-option arguments.
    else
	#
	# Get and accumulate the list of files.
	set files = ( $files $cla )
    endif
end
#
# If there are no arguments, execute the program and wait for stdin.
if ( "$files" == "" ) then
    $prog
#
# If there are arguments, interpret them as filenames.
else
    #
    # Verify whether the files exist.
    foreach file ( $files )
	if ( ! -f $file ) then
	    echo "${name}: ERROR: file $file not found"
	    exit 1
	endif
    end
    #
    # Pipe the files through the program.
    cat $files | $prog
endif
