#!/bin/bash

_help_command_description="Build image(s) or namespace(s)"

# ARG_POSITIONAL_INF([target-id],[Namespace or image to build, i.e. myns or myns/myimage],[1])
# ARG_OPTIONAL_BOOLEAN([interactive],[i],[Starts an interactive phase 1 build container. Note: It's parent image/builder has to be built already])
# ARG_OPTIONAL_BOOLEAN([no-deps],[n],[Ignore all parent images and only build passed target-id(s), needs fully qualified target_ids])
# ARG_OPTIONAL_BOOLEAN([force-image-build],[f],[Rebuild any existing images of current dependency graph])
# ARG_OPTIONAL_BOOLEAN([force-full-image-build],[F],[Same as -f but also repeat the first build phase if a cached rootfs.tar exists])
# ARG_OPTIONAL_BOOLEAN([clear-build-container],[c],[Force rebuild of required build container(s) if existing])
# ARG_OPTIONAL_BOOLEAN([clear-everything],[C],[Force rebuild of required build container(s) and their respective stage3 images])
# ARG_OPTIONAL_BOOLEAN([skip-gpg-check],[s],[Don't verify downloads with GPG, sha512 is still checked])
# ARG_OPTIONAL_BOOLEAN([verbose-build],[v],[Show all build output])
# ARG_OPTIONAL_REPEATED([exclude],[e],[Exclude given image from dependency graph for this run])
# ARG_HELP([])
# ARGBASH_WRAP([opt-global])
# ARGBASH_SET_INDENT([    ])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.3.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info

# THE DEFAULTS INITIALIZATION - POSITIONALS
_positionals=()
_arg_target_id=('' )
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_interactive=off
_arg_interactive_no_deps=off
_arg_no_deps=off
_arg_force_image_build=off
_arg_force_full_image_build=off
_arg_clear_build_container=off
_arg_clear_everything=off
_arg_skip_gpg_check=off
_arg_verbose_build=off
_arg_exclude=()
_arg_working_dir=
_arg_debug=off

print_help ()
{
    printf 'Usage: %s build [--interactive] [--interactive-no-deps] [--no-deps] [--force-image-build] [--force-full-image-build] [--clear-build-container] [--clear-everything] [--skip-gpg-check] [-e|--exclude <arg>] [-w|--working-dir <arg>] [--debug] <target-id-1> [<target-id-2>] ... [<target-id-n>] ...\n' "${_KUBLER_BIN}"
    printf "\t%s\n" "<target-id>: Namespace or image to build, i.e. myns or myns/myimage"
    printf "\t%s\n" "-i,--interactive: Starts an interactive phase 1 build container."
    printf "\t%s\n" "-I,--interactive-no-deps: Same as -i but skip any parents. Note: The parent image/builder has to be built already"
    printf "\t%s\n" "-n,--no-deps: Ignore all parent images and only build passed target-id(s), needs fully qualified target_ids"
    printf "\t%s\n" "-f,--force-image-build: Rebuild any existing images of current dependency graph"
    printf "\t%s\n" "-F,--force-full-image-build: Same as -f but also repeat the first build phase if a cached rootfs.tar exists"
    printf "\t%s\n" "-c,--clear-build-container: Force rebuild of required build container(s) if existing"
    printf "\t%s\n" "-C,--clear-everything: Force rebuild of required build container(s) and their respective stage3 images"
    printf "\t%s\n" "-s,--skip-gpg-check: Don't verify downloads with GPG, sha512 is still checked"
    printf "\t%s\n" "-v,--verbose-build: Show all build output"
    printf "\t%s\n" "-e,--exclude: Exclude given image from dependency graph for this run (repeatable)"
    printf "\t%s\n" "-h,--help: Prints help"
    printf "\t%s\n" "-w,--working-dir: Where to look for namespaces or images, default: current directory"
}

# THE PARSING ITSELF
while test $# -gt 0
do
    _key="$1"
    case "$_key" in
        -i*|--no-interactive|--interactive)
            _arg_interactive="on"
            _next="${_key##-i}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-i" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_interactive="off"
            ;;
        -I*|--no-interactive-no-deps|--interactive-no-deps)
            _arg_interactive_no_deps="on"
            _next="${_key##-I}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-I" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_interactive_no_deps="off"
            ;;
        -n*|--no-no-deps|--no-deps)
            _arg_no_deps="on"
            _next="${_key##-n}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-n" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_no_deps="off"
            ;;
        -f*|--no-force-image-build|--force-image-build)
            _arg_force_image_build="on"
            _next="${_key##-f}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-f" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_force_image_build="off"
            ;;
        -F*|--no-force-full-image-build|--force-full-image-build)
            _arg_force_full_image_build="on"
            _next="${_key##-F}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-F" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_force_full_image_build="off"
            ;;
        -c*|--no-clear-build-container|--clear-build-container)
            _arg_clear_build_container="on"
            _next="${_key##-c}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-c" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_clear_build_container="off"
            ;;
        -C*|--no-clear-everything|--clear-everything)
            _arg_clear_everything="on"
            _next="${_key##-C}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-C" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_clear_everything="off"
            ;;
        -s*|--no-skip-gpg-check|--skip-gpg-check)
            _arg_skip_gpg_check="on"
            _next="${_key##-s}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-s" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_skip_gpg_check="off"
            ;;
        -V*|--no-verbose-build|--verbose-build)
            _arg_verbose_build="on"
            _next="${_key##-V}"
            test -n "$_next" && test "$_next" != "$_key" && shift && set -- "-V" "-${_next}" "$@"
            test "${1:0:5}" = "--no-" && _arg_verbose_build="off"
            ;;
        -e*|--exclude|--exclude=*)
            _val="${_key##--exclude=}"
            _val2="${_key##-e}"
            if test "$_val" = "$_key"
            then
                test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
                _val="$2"
                shift
            elif test "$_val2" != "$_key" -a -n "$_val2"
            then
                _val="$_val2"
            fi
            _arg_exclude+=("$_val")
            ;;
        -h*|--help)
            print_help
            exit 0
            ;;
        -w*|--working-dir|--working-dir=*)
            _val="${_key##--working-dir=}"
            _val2="${_key##-w}"
            if test "$_val" = "$_key"
            then
                test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
                _val="$2"
                shift
            elif test "$_val2" != "$_key" -a -n "$_val2"
            then
                _val="$_val2"
            fi
            _arg_working_dir="$_val"
            _args_opt_global_opt+=("${_key%%=*}" "$_arg_working_dir")
            ;;
        --no-debug|--debug)
            _arg_debug="on"
            _args_opt_global_opt+=("${_key%%=*}")
            test "${1:0:5}" = "--no-" && _arg_debug="off"
            ;;
        *)
            _positionals+=("$1")
            ;;
    esac
    shift
done

_positional_names=('_arg_target_id' )
_required_args_string="'target-id'"
[[ ${_arg_help} != on ]] && test ${#_positionals[@]} -lt 1 && _PRINT_HELP=yes die "Not enough positional arguments - we require at least 1 (namely: $_required_args_string), but got only ${#_positionals[@]}." 1
_our_args=$((${#_positionals[@]} - ${#_positional_names[@]}))
for (( ii = 0; ii < _our_args; ii++))
do
    _positional_names+=("_arg_target_id[(($ii + 1))]")
done

for (( ii = 0; ii < ${#_positionals[@]}; ii++))
do
    eval "${_positional_names[ii]}=\${_positionals[ii]}" || die "Error during argument parsing, possibly an Argbash bug." 1
done

# OTHER STUFF GENERATED BY Argbash
_args_opt_global=("${_args_opt_global_opt[@]}" "${_args_opt_global_pos[@]}")

### END OF CODE GENERATED BY Argbash (sortof) ### ])
