#!/usr/bin/bash
# Update go vendor archive and config using the options go2rpm uses

usage() {
    echo "Usage: $0 [OPTION]... <SPEC_FILE>"
    echo "Update go vendor archive and config for the <SPEC_FILE> using the options go2rpm."
    echo
    echo "Options:"
    echo "  -u Ignore unlicensed mods"
    echo "  -h Print this message and exit"
    exit ${1:0}
}

while getopts "uh" flag; do
    case "$flag" in
        u) IGNORE_UNLICENSED_ARG=" --ignore-unlicensed-mods";;
        h) usage;;
        *) echo "Option $flag is unrecognized"; usage 1;;
    esac
done

SPEC_FILE="${@:$OPTIND:1}"

if [ "$SPEC_FILE" == "" ] || ! [[ "$SPEC_FILE" =~ .*.\.spec$ ]] || [ ! -f "$SPEC_FILE" ]; then
    echo "Required spec parameter missing or not a readable file ending in .spec"
    usage 1
fi

go_vendor_archive create --config go-vendor-tools.toml --write-config "$SPEC_FILE"
go_vendor_license --config go-vendor-tools.toml --path "$SPEC_FILE" report --write-config --prompt --autofill auto --update-spec$IGNORE_UNLICENSED_ARG
