#!/bin/bash
# vim: set ts=4 sw=4 et:

print_cross_targets() {
    local f
    for f in common/cross-profiles/*.sh; do
        f=${f%.sh}; f=${f##*/}; printf "\t$f\n"
    done
}

usage() {
    cat << _EOF
$(basename $0): [options] <target> [arguments]

Targets: (only one may be specified)

binary-bootstrap [arch]
    Install bootstrap packages from host repositories into <masterdir>.
    If the optional 'arch' argument is set, it will install bootstrap packages
    from this architecture, and its required xbps utilities. The <masterdir>
    will be initialized for chroot operations.

bootstrap
    Build and install from source the bootstrap packages into <masterdir>.

bootstrap-update
    Updates bootstrap packages with latest versions available from registered
    repositories in the XBPS configuration file.

consistency-check
    Runs a consistency check on all packages

chroot
    Enter to the chroot in <masterdir>.

clean-repocache
    Removes obsolete packages from <hostdir>/repocache.

fetch <pkgname>
    Download package source distribution file(s).

extract <pkgname>
    Extract package source distribution file(s) into the build directory.
    By default set to <masterdir>/builddir.

patch <pkgname>
    Patch the package sources and perform other operations required to
    prepare a package for configuring and building

configure <pkgname>
    Configure a package (fetch + extract + patch + configure).

build <pkgname>
    Build package source (fetch + extract + patch + configure + build).

check <pkgname>
    Run the package check(s) after building the package source.

install <pkgname>
    Install target package into <destdir> but not building the binary package
    and not removing build directory for inspection purposes.

pkg <pkgname>
    Build binary package for <pkgname> and all required dependencies.

clean [pkgname]
    Removes auto dependencies, cleans up <masterdir>/builddir and <masterdir>/destdir.
    If <pkgname> argument is specified, package files from <masterdir>/destdir and its
    build directory in <masterdir>/buiddir are removed.

list
    Lists installed packages in <masterdir>.

remove <pkgname>
    Remove target package from <destdir>. If <pkgname>-<version> is not matched
    from build template nothing is removed.

remove-autodeps
    Removes all package dependencies that were installed automatically.

purge-distfiles
    Removes all obsolete distfiles in <hostdir>/sources.

show <pkgname>
    Show information for the specified package.

show-avail <pkgname>
    Returns 0 if package can be built for the given architecture,
    any other error otherwise.

show-build-deps <pkgname>
    Show required build dependencies for <pkgname>.

show-deps <pkgname>
    Show required run-time dependencies for <pkgname>. Package must be
    installed into destdir.

show-files <pkgname>
    Show files installed by <pkgname>. Package must be installed into destdir.

show-hostmakedepends <pkgname>
    Show required host build dependencies for <pkgname>.

show-makedepends <pkgname>
    Show required target build dependencies for <pkgname>.

show-options <pkgname>
    Show available build options by <pkgname>.

show-shlib-provides <pkgname>
    Show list of provided shlibs for <pkgname>. Package must be installed into destdir.

show-shlib-requires <pkgname>
    Show list of required shlibs for <pkgname>. Package must be installed into destdir.

show-var <var>
    Prints the value of <var> if it's defined in xbps-src.

show-repo-updates
    Prints the list of outdated packages in XBPS repositories.

show-sys-updates
    Prints the list of oudated packages in your system.

sort-dependencies <pkg> <pkgN+1> ...
    Given a list of packages specified as additional arguments, a sorted dependency
    list will be returned to stdout.

update-bulk
    Rebuilds all packages in the system repositories that are outdated.

update-sys
    Rebuilds all packages in your system that are outdated and updates them.

update-check <pkgname>
    Check upstream site of <pkgname> for new releases.

update-hash-cache
    Update the hash cache with existing source distfiles.

zap
    Removes a masterdir but preserving ccache, distcc and host directories.

Options:

-a  <target>
    Cross compile packages for this target machine. Supported targets:

$(print_cross_targets)

-C  Do not remove build directory, automatic dependencies and
    package destdir after successful install.

-E  If a binary package exists in a local repository for the target package,
    do not try to build it, exit immediately.

-f  Force running the specified stage (configure/build/install/pkg)
    even if it ran successfully.

-G  Enable XBPS_USE_GIT_REVS (see etc/defaults.conf for more information).

-Q  Enable running the check stage.

-g  Enable building -dbg packages with debugging symbols.

-H  <hostdir>
    Absolute path to a directory to be bind mounted at <masterdir>/host.
    The host directory stores binary packages, sources and package dependencies
    downloaded from remote repositories.
    If unset defaults to void-packages/hostdir.

-h  Usage output.

-I  Ignore required dependencies, useful for extracting/fetching sources.

-j  Number of parallel build jobs to use when building packages.

-L  Disable ASCII colors.

-m  <masterdir>
    Absolute path to a directory to be used as masterdir.
    The masterdir is the main directory to build/store/compile packages.
    If unset defaults to void-packages/masterdir.

-N  Disable use of remote repositories to resolve dependencies.

-o  <opt,~opt2,...>
    Enable or disable (prefixed with ~) package build options. Note this overrides
    options set via XBPS_PKG_OPTIONS from 'etc/conf'. This effectively has the same
    effect than setting 'XBPS_PKG_OPTIONS_<pkgname>' in 'etc/conf'.
    Supported options can be shown with the 'show-options' target.

-r  <repo>
    Use an alternative local repository to store generated binary packages.
    If unset defaults to <hostdir>/binpkgs. If set the binpkgs will
    be stored into <hostdir>/binpkgs/<repo>.
    This alternative repository will also be used to resolve dependencies
    with highest priority order than others.

-t  Create a temporary masterdir to not pollute the current one. Note that
    the existing masterdir must be fully populated with binary-bootstrap first.
    Once the target has finished, this temporary masterdir will be removed.
    This flag requires xbps-uchroot(1).

_EOF
}

check_reqhost_utils() {
    local broken

    [ -n "$IN_CHROOT" ] && return 0

    for f in ${REQHOST_UTILS}; do
        if ! command -v ${f} &>/dev/null; then
            echo "${f} is missing in your system, can't continue!"
            broken=1
        fi
    done
    [ -n "$broken" ] && exit 1
    [ -z "$1" ] && return 0

    for f in ${REQHOST_UTILS_BOOTSTRAP}; do
        if ! command -v ${f} &>/dev/null; then
            echo "${f} is missing in your system, can't continue!"
            broken=1
        fi
    done
    [ -n "$broken" ] && exit 1
}

check_config_vars() {
    if [ -s "$XBPS_CONFIG_FILE" ]; then
        . $XBPS_CONFIG_FILE &>/dev/null
    fi
    if [ -z "$XBPS_MASTERDIR" ]; then
        export XBPS_MASTERDIR="${XBPS_DISTDIR}/masterdir"
    fi
    if [ -z "$XBPS_HOSTDIR" ]; then
        export XBPS_HOSTDIR="${XBPS_DISTDIR}/hostdir"
        [ ! -d $XBPS_HOSTDIR ] && mkdir -p $XBPS_HOSTDIR
    fi
    if [ -d "$XBPS_MASTERDIR" -a ! -w "$XBPS_MASTERDIR" ]; then
        echo "ERROR: not enough perms for masterdir $XBPS_MASTERDIR."
        exit 1
    fi
}

check_build_requirements() {
    local found

    for f in $XBPS_SHUTILSDIR/*.sh; do
        [ -r $f ] && . $f
    done
    case "$XBPS_TARGET" in
        *bootstrap*) found=1;;
        *) ;;
    esac
    if [ -z "$found" ]; then
        xbps-uhelper cmpver "$XBPS_VERSION" "$XBPS_VERSION_REQ"
        if [ $? -eq 255 ]; then
            echo "ERROR: requires xbps>=${XBPS_VERSION_REQ}"
            echo "Bootstrap packages must be updated with 'xbps-src bootstrap-update'"
            exit 1
        fi
    fi
    # Set XBPS_REPOSITORY to our current branch.
    if [ -z "$XBPS_ALT_REPOSITORY" ]; then
        pushd "$PWD" &>/dev/null
        cd $XBPS_DISTDIR
        if [ -n "$IN_CHROOT" ]; then
            _gitbranch="$(chroot-git symbolic-ref --short HEAD 2>/dev/null)"
        else
            _gitbranch="$($XBPS_GIT_CMD symbolic-ref --short HEAD 2>/dev/null)"
        fi
        if [ -n "${_gitbranch}" -a "${_gitbranch}" != "master" ]; then
            export XBPS_ALT_REPOSITORY="${_gitbranch}"
            export XBPS_REPOSITORY="${XBPS_REPOSITORY}/${_gitbranch}"
            if [ -z "$IN_CHROOT" ]; then
                msg_normal "Using \`$XBPS_REPOSITORY\' as local repository.\n"
            fi
        fi
        popd &>/dev/null
    else
        export XBPS_REPOSITORY="${XBPS_REPOSITORY}/${XBPS_ALT_REPOSITORY}"
        if [ -z "$IN_CHROOT" ]; then
            msg_normal "Using \`$XBPS_REPOSITORY\' as local repository.\n"
        fi
    fi
}

install_bbootstrap() {
    [ -n "$CHROOT_READY" ] && return
    if [ "$1" = "bootstrap" ]; then
        unset XBPS_TARGET_PKG XBPS_INSTALL_ARGS
    else
        XBPS_TARGET_PKG="$1"
    fi
    if [ -n "$XBPS_SKIP_REMOTEREPOS" ]; then
        unset XBPS_INSTALL_ARGS
    fi
    # binary bootstrap
    msg_normal "Installing bootstrap from binary package repositories...\n"
    # XBPS_TARGET_PKG == arch
    if [ -n "$XBPS_TARGET_PKG" ]; then
        _bootstrap_arch="env XBPS_TARGET_ARCH=$XBPS_TARGET_PKG"
        if [ "${XBPS_TARGET_PKG}" != "${XBPS_TARGET_PKG#*-}" ]; then
            _subarch="-${XBPS_TARGET_PKG#*-}"
        fi
    fi
    mkdir -p $XBPS_MASTERDIR/var/db/xbps/keys
    cd $XBPS_MASTERDIR
    cp -f $XBPS_COMMONDIR/repo-keys/*.plist $XBPS_MASTERDIR/var/db/xbps/keys
    ${_bootstrap_arch} $XBPS_INSTALL_CMD ${XBPS_INSTALL_ARGS:+-S $XBPS_INSTALL_ARGS} -y base-chroot${_subarch}
    if [ $? -ne 0 ]; then
        msg_error "Failed to install bootstrap packages!\n"
    fi
    # Reconfigure base-files to create dirs/symlinks.
    if xbps-query -r $XBPS_MASTERDIR base-files &>/dev/null; then
        XBPS_ARCH=$XBPS_TARGET_PKG xbps-reconfigure -r $XBPS_MASTERDIR -f base-files &>/dev/null
    fi

    msg_normal "Installed bootstrap successfully!\n"
    chroot_prepare $XBPS_TARGET_PKG || msg_error "Failed to initialize chroot!\n"
    chroot_check
    chroot_handler clean
}

reconfigure_bootstrap_pkgs() {
    local statefile="$XBPS_MASTERDIR/.xbps_chroot_configured"
    local pkgs="glibc-locales ca-certificates"
    [ -z "$IN_CHROOT" -o -e $statefile ] && return 0
    # Reconfigure ca-certificates.
    msg_normal "Reconfiguring bootstrap packages...\n"
    for f in ${pkgs}; do
        if xbps-query $f &>/dev/null; then
            xbps-reconfigure -f $f
        fi
    done
    touch -f $statefile
}

bootstrap_update() {
    if [ -z "$CHROOT_READY" ]; then
        return
    fi
    remove_pkg_autodeps
    msg_normal "xbps-src: cleaning up masterdir...\n"
    rm -rf $XBPS_MASTERDIR/builddir $XBPS_MASTERDIR/destdir
    msg_normal "xbps-src: updating $XBPS_MASTERDIR ...\n"
    ${XBPS_INSTALL_CMD} ${XBPS_INSTALL_ARGS} -Syu
    return $?
}

masterdir_zap() {
    for f in bin boot builddir destdir dev etc home lib lib32 lib64 mnt \
        opt proc root run sbin sys tmp usr var host media xbps \
        void-packages .xbps_chroot_init .xbps_chroot_configured; do
        if [ -d "$XBPS_MASTERDIR/$f" ]; then
            echo "Removing directory $XBPS_MASTERDIR/$f ..."
            rm -rf $XBPS_MASTERDIR/$f
        elif [ -h "$XBPS_MASTERDIR/$f" ]; then
            echo "Removing link $XBPS_MASTERDIR/$f ..."
            rm -f $XBPS_MASTERDIR/$f
        elif [ -f "$XBPS_MASTERDIR/$f" ]; then
            echo "Removing file $XBPS_MASTERDIR/$f ..."
            rm -f $XBPS_MASTERDIR/$f
        fi
    done
    echo "$XBPS_MASTERDIR masterdir cleaned up."
}

exit_func() {
    wait
    if [ -n "$sourcepkg" ]; then
        remove_pkg $XBPS_CROSS_BUILD
    fi
    if [ -z "$IN_CHROOT" ]; then
        msg_red "xbps-src: a failure has ocurred! exiting...\n"
    fi
    exit 2
}

basename_cwd() {
    echo $(basename $(pwd))
}

read_pkg() {
    if [ -z "${XBPS_TARGET_PKG}" ]; then
        [ ! -r ./template ] && msg_error "missing build template in $(pwd).\n"
        XBPS_TARGET_PKG=$(basename_cwd)
    fi
    setup_pkg "$XBPS_TARGET_PKG" "$XBPS_CROSS_BUILD" "$1"
}

setup_distfiles_mirror() {
    local mirror scheme path

    # Scheme file:// mirror locations only work with uchroot or proot
    for mirror in $XBPS_DISTFILES_MIRROR; do
        scheme="file"
        if [[ "$mirror" == *://* ]]; then
            scheme="${mirror%%://*}"
            path="${mirror#${scheme}://}"
        else
            path="$mirror"
        fi
        [ "$scheme" != "file" ] && continue
        if [ "$XBPS_CHROOT_CMD" == "uchroot" -o "$XBPS_CHROOT_CMD" == "proot" ]; then
            if [ ! -d "$path" ]; then
                msg_warn "Invalid path in XBPS_DISTFILES_MIRROR ($mirror)\n"
                continue
            fi
            mkdir -p "$XBPS_MASTERDIR/$path"
            XBPS_CHROOT_CMD_ARGS+=" -b $path:$path"
        else
            msg_warn "File URLs ($mirror) don't work with '$XBPS_CHROOT_CMD'\n"
        fi
    done
}

readonly XBPS_VERSION_REQ="0.46"
readonly XBPS_VERSION=$(xbps-uhelper -V|awk '{print $2}')
readonly XBPS_SRC_VERSION="113"
export XBPS_MACHINE=$(xbps-uhelper arch)

#
# main()
#
XBPS_OPTIONS=
XBPS_OPTSTRING="1a:CEfgGhH:iIj:Lm:No:qQr:tV"

# Preprocess arguments in order to allow options before and after XBPS_TARGET.
eval set -- $(getopt "$XBPS_OPTSTRING" "$@");

while getopts "$XBPS_OPTSTRING" opt; do
    case $opt in
        1) export XBPS_BUILD_ONLY_ONE_PKG=yes; XBPS_OPTIONS+=" -1";;
        a) readonly XBPS_CROSS_BUILD="$OPTARG"; XBPS_OPTIONS+=" -a $OPTARG";;
        C) readonly XBPS_KEEP_ALL=1; XBPS_OPTIONS+=" -C";;
        E) readonly XBPS_BINPKG_EXISTS=1; XBPS_OPTIONS+=" -E";;
        f) readonly XBPS_BUILD_FORCEMODE=1; XBPS_OPTIONS+=" -f";;
        G) readonly XBPS_USE_GIT_REVS=1; XBPS_OPTIONS+=" -G";;
        g) readonly XBPS_DEBUG_PKGS=1; XBPS_OPTIONS+=" -g";;
        H) readonly XBPS_HOSTDIR="$(readlink -f $OPTARG 2>/dev/null)"; XBPS_OPTIONS+=" -H $XBPS_HOSTDIR";;
        h) usage && exit 0;;
        i) export XBPS_INFORMATIVE_RUN=1; XBPS_OPTIONS+=" -i";;
        I) readonly XBPS_SKIP_DEPS=1; XBPS_OPTIONS+=" -I";;
        j) export XBPS_MAKEJOBS="$OPTARG"; XBPS_OPTIONS+=" -j $OPTARG";;
        L) export NOCOLORS=1; XBPS_OPTIONS+=" -L";;
        m) readonly XBPS_MASTERDIR=$(readlink -f $OPTARG 2>/dev/null); XBPS_OPTIONS+=" -m $XBPS_MASTERDIR";;
        N) readonly XBPS_SKIP_REMOTEREPOS=1; XBPS_OPTIONS+=" -N";;
        o) readonly XBPS_PKG_OPTIONS="$OPTARG"; XBPS_OPTIONS+=" -o $OPTARG";;
        q) export XBPS_QUIET=1; XBPS_OPTIONS+=" -q";;
        Q) export XBPS_CHECK_PKGS=1; XBPS_OPTIONS+=" -Q";;
        r) readonly XBPS_ALT_REPOSITORY="$OPTARG"; XBPS_OPTIONS+=" -r $OPTARG";;
        t) export XBPS_TEMP_MASTERDIR=1; XBPS_OPTIONS+=" -t -C";;
        V) echo $XBPS_SRC_VERSION && exit 0;;
        --) shift; break;;
    esac
done
shift $(($OPTIND - 1))

[ $# -eq 0 ] && usage && exit 1

umask 022

#
# Check for required utilities in host system.
#
# Required utilities in host system for the bootstrap target.
readonly REQHOST_UTILS_BOOTSTRAP="make gawk bash bison sed gcc g++ msgfmt makeinfo \
    perl tar xz gzip bzip2 patch flex automake libtool pkg-config"

# Required utilities in host system for chroot ops.
readonly REQHOST_UTILS="xbps-install xbps-query xbps-rindex xbps-uhelper \
    xbps-reconfigure xbps-remove xbps-create xbps-uchroot xbps-uunshare"

check_reqhost_utils

# Try using chroot-git then git from the host system
if command -v chroot-git &>/dev/null; then
	XBPS_GIT_CMD=$(command -v chroot-git)
elif command -v git &>/dev/null; then
	XBPS_GIT_CMD=$(command -v git)
else
    echo "neither chroot-git or git are available in your system!"
    exit 1
fi

readonly XBPS_GIT_CMD

if [ -n "$IN_CHROOT" ]; then
    readonly XBPS_CONFIG_FILE=/etc/xbps/xbps-src.conf
    readonly XBPS_DISTDIR=/void-packages
else
    _distdir="$(readlink -f $(dirname $0))"
    if [ "${_distdir}" = "." ]; then
        readonly XBPS_DISTDIR="$(pwd -P)"
    else
        readonly XBPS_DISTDIR="${_distdir}"
    fi
    # Read defaults and then the local configuration file
    if [ -f $XBPS_DISTDIR/etc/defaults.conf ]; then
        . $XBPS_DISTDIR/etc/defaults.conf
    fi
    if [ -s $XBPS_DISTDIR/etc/conf ]; then
        # Read user configuration first...
        readonly XBPS_CONFIG_FILE=$XBPS_DISTDIR/etc/conf
    elif [ -s $HOME/.xbps-src.conf ]; then
        # ... fallback to ~/.xbps-src.conf otherwise.
        readonly XBPS_CONFIG_FILE=$HOME/.xbps-src.conf
        . $XBPS_CONFIG_FILE
    fi
fi

#
# Check configuration vars before anyting else, and set defaults vars.
#
check_config_vars

if [ -n "$XBPS_HOSTDIR" ]; then
    export XBPS_REPOSITORY=$XBPS_HOSTDIR/binpkgs
    readonly XBPS_SRCDISTDIR=$XBPS_HOSTDIR/sources
else
    export XBPS_REPOSITORY=$XBPS_MASTERDIR/host/binpkgs
    readonly XBPS_SRCDISTDIR=$XBPS_MASTERDIR/host/sources
fi
if [ -n "$IN_CHROOT" ]; then
    readonly XBPS_UHELPER_CMD="xbps-uhelper"
    readonly XBPS_INSTALL_CMD="xbps-install"
    readonly XBPS_QUERY_CMD="xbps-query"
    readonly XBPS_RINDEX_CMD="xbps-rindex"
    readonly XBPS_RECONFIGURE_CMD="xbps-reconfigure"
    readonly XBPS_REMOVE_CMD="xbps-remove"
    readonly XBPS_SRCPKGDIR=/void-packages/srcpkgs
    readonly XBPS_COMMONDIR=/void-packages/common
    readonly XBPS_DESTDIR=/destdir
    readonly XBPS_BUILDDIR=/builddir
    readonly XBPS_SHUTILSDIR=$XBPS_COMMONDIR/xbps-src/shutils
    readonly XBPS_TRIGGERSDIR=$XBPS_SRCPKGDIR/xbps-triggers/files
    readonly XBPS_CROSSPFDIR=$XBPS_COMMONDIR/cross-profiles
    readonly XBPS_BUILDSTYLEDIR=$XBPS_COMMONDIR/build-style
    readonly XBPS_LIBEXECDIR=$XBPS_COMMONDIR/xbps-src/libexec
    readonly XBPS_BUILDHELPERDIR=$XBPS_COMMONDIR/build-helper
else
    readonly XBPS_UHELPER_CMD="xbps-uhelper -r $XBPS_MASTERDIR"
    readonly XBPS_INSTALL_CMD="xbps-install -C /dev/null -c $XBPS_HOSTDIR/repocache --repository=$XBPS_REPOSITORY -r $XBPS_MASTERDIR"
    readonly XBPS_QUERY_CMD="xbps-query -C /dev/null -c $XBPS_HOSTDIR/repocache -i --repository=$XBPS_REPOSITORY -r $XBPS_MASTERDIR"
    readonly XBPS_RINDEX_CMD="xbps-rindex"
    readonly XBPS_RECONFIGURE_CMD="xbps-reconfigure -r $XBPS_MASTERDIR"
    readonly XBPS_REMOVE_CMD="xbps-remove -r $XBPS_MASTERDIR"
    readonly XBPS_SRCPKGDIR=$XBPS_DISTDIR/srcpkgs
    readonly XBPS_COMMONDIR=$XBPS_DISTDIR/common
    readonly XBPS_SHUTILSDIR=$XBPS_COMMONDIR/xbps-src/shutils
    readonly XBPS_DESTDIR=$XBPS_MASTERDIR/destdir
    readonly XBPS_BUILDDIR=$XBPS_MASTERDIR/builddir
    readonly XBPS_TRIGGERSDIR=$XBPS_SRCPKGDIR/xbps-triggers/files
    readonly XBPS_CROSSPFDIR=$XBPS_COMMONDIR/cross-profiles
    readonly XBPS_BUILDSTYLEDIR=$XBPS_COMMONDIR/build-style
    readonly XBPS_LIBEXECDIR=$XBPS_COMMONDIR/xbps-src/libexec
    readonly XBPS_BUILDHELPERDIR=$XBPS_COMMONDIR/build-helper
fi
readonly XBPS_FETCH_CMD="xbps-uhelper fetch"
readonly XBPS_DIGEST_CMD="xbps-uhelper digest"
readonly XBPS_CMPVER_CMD="xbps-uhelper cmpver"

readonly XBPS_TARGET="$1"
if [ -n "$2" ]; then
    XBPS_TARGET_PKG="${2##*/}"
fi

# Check if stdout is a tty; if false disable colors.
test -t 1 || export NOCOLORS=1
# http://no-color.org
if [ -n "${NO_COLOR+x}" ]; then
    export NOCOLORS=1
fi

chroot_check() {
    if [ -f $XBPS_MASTERDIR/.xbps_chroot_init ]; then
        export CHROOT_READY=1
    fi
}

chroot_check

if [ -z "$XBPS_REINIT" -a -s $XBPS_MASTERDIR/.xbps_chroot_init ]; then
    export XBPS_ARCH=${XBPS_ARCH:-$(cat $XBPS_MASTERDIR/.xbps_chroot_init)}
    if [[ $XBPS_MACHINE == x86_64* ]] && [[ $XBPS_ARCH == i686* ]]; then
        # reconfigure pkgs via linux32
        linux32 xbps-reconfigure -r ${XBPS_MASTERDIR} -a &>/dev/null
        # reexec itself via linux32
        export XBPS_REINIT=1
        exec linux32 $0 ${XBPS_OPTIONS} $@
    fi
fi
if [ -n "$XBPS_ARCH" ]; then
    export XBPS_MACHINE=$XBPS_ARCH
fi
if [ -z "$XBPS_TARGET_MACHINE" ]; then
    export XBPS_TARGET_MACHINE=${XBPS_ARCH:=$XBPS_MACHINE}
fi

export XBPS_SHUTILSDIR XBPS_CROSSPFDIR XBPS_TRIGGERSDIR \
    XBPS_SRCPKGDIR XBPS_COMMONDIR XBPS_BUILDDIR XBPS_REPO_DELTAS \
    XBPS_REPOSITORY XBPS_ALT_REPOSITORY XBPS_SRCDISTDIR XBPS_DIGEST_CMD \
    XBPS_UHELPER_CMD XBPS_INSTALL_CMD XBPS_QUERY_CMD XBPS_BUILD_ONLY_ONE_PKG \
    XBPS_RINDEX_CMD XBPS_RECONFIGURE_CMD XBPS_REMOVE_CMD \
    XBPS_CMPVER_CMD XBPS_FETCH_CMD XBPS_VERSION XBPS_APIVER XBPS_BUILDSTYLEDIR \
    XBPS_CPPFLAGS XBPS_CFLAGS XBPS_CXXFLAGS XBPS_FFLAGS XBPS_LDFLAGS \
    XBPS_MAKEJOBS XBPS_BUILD_FORCEMODE XBPS_USE_GIT_REVS XBPS_DEBUG_PKGS \
    XBPS_CHECK_PKGS XBPS_CCACHE XBPS_DISTCC XBPS_DISTCC_HOSTS XBPS_SKIP_DEPS \
    XBPS_SKIP_REMOTEREPOS XBPS_CROSS_BUILD XBPS_PKG_OPTIONS XBPS_CONFIG_FILE \
    XBPS_KEEP_ALL XBPS_HOSTDIR XBPS_MASTERDIR XBPS_SRC_VERSION \
    XBPS_DESTDIR XBPS_MACHINE XBPS_TEMP_MASTERDIR XBPS_BINPKG_EXISTS \
    XBPS_LIBEXECDIR XBPS_DISTDIR XBPS_DISTFILES_MIRROR XBPS_ALLOW_RESTRICTED \
    XBPS_USE_GIT_COMMIT_DATE XBPS_PKG_COMPTYPE XBPS_BUILDHELPERDIR

for i in REPOSITORY DESTDIR BUILDDIR SRCDISTDIR; do
    eval val="\$XBPS_$i"
    if [ ! -d "$val" ]; then
        mkdir -p $val
    fi
    unset val
done

# A temporary masterdir requires xbps-uchroot(8) and -O to use overlayfs
# on tmpfs (available with xbps-0.45).
if [ -z "$IN_CHROOT" -a -n "$XBPS_TEMP_MASTERDIR" ]; then
    export XBPS_CHROOT_CMD="uchroot"
    export XBPS_CHROOT_CMD_ARGS+=" -O"
fi
#
# Sanitize PATH.
#
if [ -z "$IN_CHROOT" ]; then
    # In non chroot case always prefer host tools.
    MYPATH="$XBPS_MASTERDIR/usr/bin:$XBPS_MASTERDIR/usr/sbin"
    export PATH="$PATH:$MYPATH"
else
    MYPATH="/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
    export PATH="$MYPATH"
    if [ -n "$XBPS_CCACHE" ]; then
        CCACHEPATH="/usr/lib/ccache/bin"
        export CCACHE_DIR="$XBPS_HOSTDIR/ccache"
        # Avoid not using cached files just due to compiler mtime
        # changes when e.g. bootstrapping
        export CCACHE_COMPILERCHECK=content CCACHE_COMPRESS=1
        export PATH="$CCACHEPATH:$PATH"
        mkdir -p $CCACHE_DIR
    fi
    if [ -n "$XBPS_DISTCC" ]; then
        if [ -n "$XBPS_CCACHE" ]; then
            export CCACHE_PREFIX="/usr/bin/distcc"
        else
            DISTCCPATH="/usr/lib/distcc/bin"
            export PATH="$DISTCCPATH:$PATH"
        fi
        export DISTCC_DIR="$XBPS_HOSTDIR/distcc-${XBPS_CROSS_BUILD:-${XBPS_MACHINE}}"
        export DISTCC_HOSTS="$XBPS_DISTCC_HOSTS"
        mkdir -p $DISTCC_DIR
    fi
fi

check_build_requirements

if [ -z "$IN_CHROOT" ]; then
    trap 'exit_func' INT TERM
    if [ -n "$XBPS_DISTFILES_MIRROR" ]; then
        setup_distfiles_mirror
    fi
fi

reconfigure_bootstrap_pkgs

#
# Main switch.
#
case "$XBPS_TARGET" in
    binary-bootstrap)
        install_bbootstrap ${XBPS_TARGET_PKG:=$XBPS_MACHINE}
        ;;
    bootstrap)
        # bootstrap from sources
        # check for required host utils
        check_reqhost_utils bootstrap
        [ ! -d $XBPS_SRCPKGDIR/base-chroot ] && \
            msg_error "Cannot find $XBPS_SRCPKGDIR/base-chroot directory!\n"
        bootstrap_vpkg=${XBPS_MASTERDIR}/etc/xbps.d/bootstrap-vpkgs.conf
        mkdir -p ${XBPS_MASTERDIR}/etc/xbps.d
        if [ ! -s ${bootstrap_vpkg} ]; then
            # Fool xbps to resolve dependencies.
            echo 'virtualpkg=libgcc-4.4.0_1:base-files' >> ${bootstrap_vpkg}
            echo 'virtualpkg=libstdc++-4.4.0_1:base-files' >> ${bootstrap_vpkg}
        fi
        $XBPS_LIBEXECDIR/build.sh base-chroot base-chroot $XBPS_TARGET || exit 1
        [ -d $XBPS_MASTERDIR ] && rm -rf $XBPS_MASTERDIR
        install_bbootstrap bootstrap
        ;;
    bootstrap-update)
        bootstrap_update
        ;;
    chroot)
        chroot_handler chroot dummy
        ;;
    clean)
        if [ -z "$XBPS_TARGET_PKG" ]; then
            if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
                chroot_handler remove-autodeps
            else
                remove_pkg_autodeps
            fi
            msg_normal "xbps-src: cleaning up masterdir...\n"
            chmod -R +wX $XBPS_MASTERDIR/builddir # Needed to remove Go Modules
            rm -rf $XBPS_MASTERDIR/builddir $XBPS_MASTERDIR/destdir
            rm -f $XBPS_MASTERDIR/tmp/* >/dev/null 2>&1
        else
            read_pkg
            if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
                chroot_handler $XBPS_TARGET $XBPS_TARGET_PKG || exit $?
            else
                remove_pkg_wrksrc $wrksrc
                remove_pkg_statedir
                if declare -f do_clean >/dev/null; then
                    run_func do_clean
                fi
            fi
            remove_pkg $XBPS_CROSS_BUILD
        fi
        ;;
    clean-repocache)
        export XBPS_TARGET_ARCH="${XBPS_CROSS_BUILD:-$XBPS_TARGET_MACHINE}"
        $XBPS_REMOVE_CMD -C /dev/null -c $XBPS_HOSTDIR/repocache -O
        ;;
    consistency-check)
        consistency_check
        ;;
    remove-autodeps)
        if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
            chroot_handler remove-autodeps
        else
            remove_pkg_autodeps
        fi
        ;;
    fetch|extract|patch|configure|build|check|install|pkg)
        if [ "$XBPS_TARGET" = "check" ]; then
            export XBPS_CHECK_PKGS=1
        fi
        read_pkg
        if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
            chroot_handler $XBPS_TARGET $XBPS_TARGET_PKG
        else
            [ -z "$XBPS_KEEP_ALL" -a -z "$XBPS_SKIP_DEPS" ] && remove_pkg_autodeps
            $XBPS_LIBEXECDIR/build.sh $XBPS_TARGET_PKG $XBPS_TARGET_PKG \
                $XBPS_TARGET $XBPS_CROSS_BUILD || exit $?
        fi
        ;;
    remove|remove-destdir)
        read_pkg
        remove_pkg $XBPS_CROSS_BUILD
        ;;
    list)
        $XBPS_QUERY_CMD -l
        ;;
    purge-distfiles)
        purge_distfiles
        ;;
    show)
        read_pkg ignore-problems
        show_pkg
        ;;
    show-avail)
        read_pkg
        show_avail
        ;;
    show-files)
        read_pkg ignore-problems
        show_pkg_files
        ;;
    show-deps)
        read_pkg ignore-problems
        show_pkg_deps
        ;;
    show-build-deps)
        read_pkg ignore-problems
        show_pkg_build_deps
        ;;
    show-hostmakedepends)
        read_pkg ignore-problems
        show_pkg_hostmakedepends
        ;;
    show-makedepends)
        read_pkg ignore-problems
        show_pkg_makedepends
        ;;
    show-pkg-var)
        read_pkg ignore-problems
        for sub_name in $subpackages; do
            if [ $sub_name = $XBPS_TARGET_PKG ]; then
                ${sub_name}_package
            fi
        done
	while IFS= read -r pkg_var; do
		print_var=
		case "${pkg_var}" in
		    bootstrap) print_var="$bootstrap ";;
		    broken) print_var="$broken ";;
		    depends) print_var="$depends ";;
		    hostmakedepends) print_var="$hostmakedepends ";;
		    makedepends) print_var="$makedepends ";;
		    noarch) print_var="$noarch ";;
		    nocross) print_var="$nocross ";;
		    only_for_archs) print_var="$only_for_archs ";;
		    pkgname) print_var="$pkgname ";;
		    restricted) print_var="$restricted ";;
		    revision) print_var="$revision ";;
		    subpackages) print_var="$subpackages ";;
		    version) print_var="$version ";;
		    provides) print_var="$provides ";;
		    lib32disabled) print_var="$lib32disabled ";;
		    nodebug) print_var="$nodebug ";;
		esac # the space at the end of each is essential for unset vars
		if [ -n "$print_var" ]; then
			printf "%s\n" "$(printf "${print_var% }" | tr -t '\n\r' ' ')"
		fi # The trailing space gets stripped before printing anyway
	done
        ;;
    show-options)
        read_pkg ignore-problems
        show_pkg_build_options
        ;;
    show-shlib-provides)
        read_pkg ignore-problems
        show_pkg_shlib_provides
        ;;
    show-shlib-requires)
        read_pkg ignore-problems
        show_pkg_shlib_requires
        ;;
    show-var)
        for f in ${XBPS_COMMONDIR}/environment/setup/*.sh; do
            source $f
        done
        source ${XBPS_COMMONDIR}/build-profiles/${XBPS_MACHINE}.sh
        eval value="\${$XBPS_TARGET_PKG}"
        echo $value
        ;;
    show-repo-updates)
        bulk_build
        ;;
    show-sys-updates)
        bulk_build -i
        ;;
    sort-dependencies)
        bulk_sortdeps ${@/$XBPS_TARGET/}
        ;;
    update-bulk)
        bulk_update
        ;;
    update-sys)
        bulk_update -i
        ;;
    update-check)
        read_pkg ignore-problems
        update_check
        ;;
    update-hash-cache)
        update_hash_cache
        ;;
    zap)
        masterdir_zap
        ;;
    *)
        msg_red "xbps-src: invalid target $XBPS_TARGET.\n"
        usage && exit 1
        ;;
esac

exit_and_cleanup $?