#!/bin/sh
#
# Invoke Cook with useful options.  Arguments are passed to Cook
# except for the ones listed here.  
#
#    --clean
#     -c
#              Remove the `bin' directory first. 
#
#    --debug
#     -d
#              Enable debugging mode. 
#
#    --none
#     -o       Set $build_type to `none'. 
#
#    --no-nice
#     -n
#              Run without `nice'.  By default Cook is run at a lower
#              priority. 
#
#    --no-continue
#     -k    
#              Don't use the `-Continue' option of Cook (which corresponds to
#              the `-k' option of Make.)
#
#    --print
#     -p
#              Print the content of the files built. 
#
# Options cannot be grouped.  All other options are passed to Cook.   
#

. sh/tools.sh

args="-Verbose"
# args for Cook

nice="nice" 
# set to empty through option 

continue_option=-Continue
# set to empty through option

need_clean= 
# set to non-empty through option 

targets=
# Passed to Cook and optionally printed. 

print=
# Set to non-empty by option. 


while [ -n "$1" ]
do
    case $1 in

	--clean | -c )
	    need_clean=t
	    shift
	    ;;

	--debug | -d )
	    build_type=debug
	    shift
	    ;;

	--none | -o )
	    build_type=none
	    shift
	    ;;

	--no-nice | -n )
	    nice=
	    shift
	    ;;

	--no-continue | -k )
	    continue_option=
	    shift
	    ;;

	--print | -p )
	    print=t
	    shift
	    ;;

	-* )
	    args="$args $1"
	    shift
	    ;;

	* )
	    targets="$targets $1"
	    shift
	    ;;
    esac
done

verbose=t

export build_type
export verbose

if [ "$need_clean" ]
then
    sh/do rm -fr bin
#    sh/do sh/clean
#    sh/do time $nice cook $args clean
fi

if sh/do time $nice cook $continue_option $args $targets
then
    true
else
    exit 1
fi

if [ "$print" ]
then
    echo
    echo $targets:
    echo ----------------------------
    cat $targets
    echo ----------------------------
    echo
fi

exit 0
