#! /bin/sh

me=`basename $0`

targets="build | binary | binary-regular | binary-historic | binary-basic | clean | source | diff | dist"

usage() {
    >&2 echo usage: $me "[ -v, --version ] [ -h, --help ] [ -n, --nocheck ] [ --special ] [ -f, --makefile makefile ] [ -b, --buildtarget build ] [ -i, --installtarget install ] [ -c, --cleantarget clean ] [ $targets ] ..."
    if [ -z "$1" ]
    then
	exit 1
    else
	exit 0
    fi
}

special=0
tseen=0

while [ $# -ne 0 ]
do
    case "$1" in
	-v|--version)
	    echo "$me version 1.0 by Yves Arrouye <arrouye@debian.org>"
	    exit 0
	    ;;
	-n|--nocheck)
	    nocheck=yes
	    ;;
	--special)
	    special=1
	    ;;
	-h)
	    usage -h
	    ;;
	-f|--makefile)
	    test $# -gt 1 || usage
	    if [ ! -e "$2" -o -d "$2" ]
	    then
		>&2 echo "$me: \`$2' cannot be used as a Makefile, aborting"
		exit 2
	    fi
	    makefilearg="'cmakefile=-f $2'"
	    shift
	    ;;
	-b|--buildtarget)
	    test $# -gt 1 || usage
	    makeargs="$makeargs buildtarget=$2"
	    shift
	    ;;
	-i|--installtarget)
	    test $# -gt 1 || usage
	    makeargs="$makeargs installtarget=$2"
	    shift
	    ;;
	-c|--cleantarget)
	    test $# -gt 1 || usage
	    makeargs="$makeargs cleantarget=$2"
	    shift
	    ;;
	build|binary|binary-regular|binary-historic|binary-basic|clean|source|diff|dist)
	    tseen=1
	    target="$target $1"
	    ;;
	*) 
	    target="$target $1"
	    ;;
    esac
    shift
done

if [ $tseen -eq 0 -a $special -eq 0 ]
then
    target="binary $target"
fi

check=compress.c
if [ -z "$nocheck" -a ! -f $check ]
then
    cat <<EOF >&2
$me: not sure to be in the correct directory: it does not look
like the current directory contains the sources for a BSD compress
package: there is no \`$check' file here; please change to a correct
directory and retry, or if you are really in a correct directory
please consider reporting this message as a bug.
EOF
    exit 1
fi

rules=/usr/lib/compress-package/debian/rules

if [ -e "$rules" ]
then
    exec make -f ${rules} $target $makeargs $makefilearg
else
    >&2 echo $me: cannot execute \`"$rules"\'
    exit 2
fi

