#!/bin/bash
[[ -v AUR_DEBUG ]] && set -o xtrace
argv0=build--pkglist
PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

# default options
lint=0 mode=pkglist

usage() {
    printf >&2 'usage: %s [--srcinfo] [--lint] [--config <file>]' "$argv0"
    exit 1
}

# mollyguard for sourcing PKGBUILDs
if (( UID == 0 )) && [[ ! -v AUR_ASROOT ]]; then
    printf >&2 'warning: aur-%s is not meant to be run as root.\n' "$argv0"
    printf >&2 'warning: To proceed anyway, set the %s variable.\n' 'AUR_ASROOT'
    exit 1
fi

source /usr/share/makepkg/util/option.sh
source /usr/share/makepkg/util/parseopts.sh
source /usr/share/makepkg/util/config.sh
source /usr/share/makepkg/util/util.sh

# option parsing
opt_short='p:'
opt_long=('buildscript:' 'config:' 'lint' 'srcinfo')
opt_hidden=('dump-options')

if ! parseopts "$opt_short" "${opt_long[@]}" "${opt_hidden[@]}" -- "$@"; then
    usage
fi
set -- "${OPTRET[@]}"

unset buildscript build_user makepkg_conf
while true; do
    case "$1" in
        -p|--buildscript)
            shift; buildscript=$1 ;;
        --config)
            shift; makepkg_conf=$1 ;;
        --srcinfo)
            mode=srcinfo ;;
        --lint)
            lint=1 ;;
        --dump-options)
            printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
            printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
            exit ;;
        --) shift; break ;;
    esac
    shift
done

# source follows $PATH, compute absolute path to PKGBUILD (#1115)
buildscript="$(realpath --strip -- "${buildscript-PKGBUILD}")"

# Sourcing the PKGBUILD should be done without set -e or other modes
# to match makepkg behavior (e.g. aur/cemu, aur/nicotine-plus-git)
# shellcheck disable=SC1090
source_safe "$buildscript"
PKGDEST=${PKGDEST:-$PWD}
pkgbase=${pkgbase:-${pkgname[0]}}

# PKGEXT (packagelist), CARCH (lint)
load_makepkg_config "${makepkg_conf-}"

if (( lint )); then
    source /usr/share/makepkg/lint_pkgbuild.sh
    lint_pkgbuild
fi

case $mode in
    pkglist)
        source /usr/share/makepkg/util/pkgbuild.sh
        print_all_package_names
        ;;
    srcinfo) 
        source /usr/share/makepkg/srcinfo.sh
        write_srcinfo_content
        ;;
esac
