#!/usr/bin/env bash

print_help() {
    (>&2 cat <<EOF
Build a package.
Usage: $0 [-h|-?|--help]
           Print this help text and exit.
     - $0 <package-name>
           Build the package.
EOF
)
}

main() {
    case $1 in
        -h|-?|--help)
            print_help
            ;;
        *)
            exec_subcommand src-db exists "$1"
            if [ $? -ne 0 ]; then
                # TODO: download if URL specified
                (>&2 echo "This package doesn't exist.")
                exit 1
            fi
            # assume it is already configured
            exec_subcommand pkgcmd build "$1"

            # now update the 'known latest version'
            exec_subcommand src-db reg "$1" $(exec_subcommand pkgcmd version "$1")
            ;;
    esac
}

if [ $# -eq 0 ]; then
    print_help
    exit
fi

main $@

