#!/bin/sh
set -Eeo pipefail

PROG_NAME="cabal-vendor"

COMMAND=""
AVAILABLE_COMMANDS="fetch convert"

help()
{
cat << EOF
Usage: $PROG_NAME [COMMAND] [OPTIONS]

$PROG_NAME is a tool for vendoring dependencies of a Cabal package
into a local repository

Commands:
    fetch                   Set up the vendor directory
    convert                 Generate a local repository (usable with Cabal)
                            from the vendor directory

Options:
    --help, -h              Show this help message
EOF
}

printerr() { printf "$PROG_NAME:\t %b\n" "$*" >&2; }

while [ "$1" != "" ]
do
    case "$1" in
    fetch )   COMMAND="fetch";
                shift
                break;;

    convert ) COMMAND="convert"
                shift
                break;;

    --help | -h ) help;exit;;

    *) printerr "$1 is not an option"; exit 1;;
    esac
done

[ -z "$COMMAND" ] && { help; exit 1; }

cabal-vendor_$COMMAND $@
