#!/bin/bash
# aur-chroot - build packages with systemd-nspawn
[[ -v AUR_DEBUG ]] && set -o xtrace
set -o errexit
argv0=chroot
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
machine=$(uname -m)

# default arguments
directory=/var/lib/aurbuild/$machine
suffix=extra
makechrootpkg_args=()
makechrootpkg_makepkg_args=()

# default options
update=0 build=0 create=0 print_path=0

args_csv() {
    # shellcheck disable=SC2155
    local str=$(printf '%s,' "$@")
    printf '%s' "${str%,}"
}

usage() {
    printf 'usage: %s [-BU] [--create] [-CDM path] [package...]\n' "$argv0"
    exit 1
}

opt_short='C:D:M:x:BU'
opt_long=('directory:' 'pacman-conf:' 'makepkg-conf:' 'build' 'update'
          'create' 'suffix:' 'bind:' 'bind-rw:' 'path' 'makepkg-args:'
          'makechrootpkg-args:' 'margs:' 'cargs:')
opt_hidden=('dump-options')

if opts=$(getopt -o "$opt_short" -l "$(args_csv "${opt_long[@]}" "${opt_hidden[@]}")" -n "$argv0" -- "$@"); then
    eval set -- "$opts"
else
    usage
fi

unset bindmounts_ro bindmounts_rw makepkg_conf pacman_conf
while true; do
    case "$1" in
        -B|--build)
            build=1 ;;
        -U|--update)
            update=1 ;;
        --create)
            create=1 ;;
        --path)
            print_path=1 ;;
        -x|--suffix)
            shift; suffix=$1 ;;
        # devtools options
        -C|--pacman-conf)
            shift; pacman_conf=$1 ;;
        -D|--directory)
            shift; directory=$1 ;;
        -M|--makepkg-conf)
            shift; makepkg_conf=$1 ;;
        --bind)
            shift; bindmounts_ro+=("$1") ;;
        --bind-rw)
            shift; bindmounts_rw+=("$1") ;;
        --makechrootpkg-args|--cargs)
            shift; IFS=, read -a arg -r <<< "$1"
            makechrootpkg_args+=("${arg[@]}") ;;
        --makepkg-args|--margs)
            shift; IFS=, read -a arg -r <<< "$1"
            makechrootpkg_makepkg_args+=("${arg[@]}") ;;
        # other options
        --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

# additional makechrootpkg arguments if none are specified
if (( ! ${#makechrootpkg_args[@]} )); then
    makechrootpkg_args=(-c)
fi

# default paths depend on devtools version (#1072)
# XXX: use installation prefix (aur-chroot.in)
if [[ -d /usr/share/devtools/makepkg.conf.d ]]; then
    makepkg_conf_default=/usr/share/devtools/makepkg.conf.d/$machine.conf
else
    makepkg_conf_default=/usr/share/devtools/makepkg-$machine.conf
fi

if [[ -d /usr/share/devtools/pacman.conf.d ]]; then
    pacman_conf_default=/usr/share/devtools/pacman.conf.d/$suffix.conf
else
    pacman_conf_default=/usr/share/devtools/pacman-$suffix.conf
fi

# required for chroot creation and update
# if specified, copied to the container by arch-nspawn
makepkg_conf=${makepkg_conf:-$makepkg_conf_default}

if [[ ! -f $makepkg_conf ]]; then
    printf >&2 '%s: %q is not a file\n' "$argv0" "$makepkg_conf"
    exit 2
elif [[ ! -r $makepkg_conf ]]; then
    printf >&2 '%s: %q could not be read\n' "$argv0" "$makepkg_conf"
    exit 1
fi

# required for chroot creation and update
# if specified, copied to the container by arch-nspawn
pacman_conf=${pacman_conf:-$pacman_conf_default}

if [[ ! -f $pacman_conf ]]; then
    printf >&2 '%s: %q is not a file\n' "$argv0" "$pacman_conf"
    exit 2
elif [[ ! -r $pacman_conf ]]; then
    printf >&2 '%s: %q could not be read\n' "$argv0" "$pacman_conf"
    exit 1
fi

# Custom elevation command (#1024)
unset auth_args
AUR_PACMAN_AUTH=${AUR_PACMAN_AUTH:-sudo}

case $AUR_PACMAN_AUTH in
    sudo) auth_args+=('--preserve-env=GNUPGHOME,SSH_AUTH_SOCK,PKGDEST') ;;
esac

# bind mount file:// paths to container (#461)
# required for update/build steps
while read -r key _ value; do
    case $key=$value in
        Server=file://*)
            bindmounts_rw+=("${value#file://}") ;;
    esac
done < <(pacman-conf --config "$pacman_conf")
wait "$!"

# create new container, required for update/build steps
if (( create )); then
    # default to base-devel or multilib-devel, unless packages are
    # specified on the command-line.
    # available packages depend on the configured pacman configuration
    if (( $# )); then
        base_packages=("$@")

    # XXX: use pacini to not process Include directives in pacman.conf
    # (not supported by devtools)
    elif [[ $(pacini --section=multilib "$pacman_conf") ]] && [[ $machine == "x86_64" ]]; then
        base_packages=('base-devel' 'multilib-devel')
    else
        base_packages=('base-devel')
    fi

    # parent path is not created by mkarchroot (#371)
    if [[ ! -d $directory ]]; then
        # shellcheck disable=SC2086
        $AUR_PACMAN_AUTH install -d "$directory" -m 755 -v
    fi

    if [[ ! -d $directory/root ]]; then
        # shellcheck disable=SC2086
        $AUR_PACMAN_AUTH mkarchroot -C "$pacman_conf" -M "$makepkg_conf" "$directory"/root "${base_packages[@]}"
    fi
fi

# arch-nspawn makes no distinction between a missing working directory
# and one which does not exist
if [[ ! -d $directory/root ]]; then
    printf >&2 '%s: %q is not a directory\n' "$argv0" "$directory"/root
    printf >&2 '%s: did you run aur chroot --create?\n' "$argv0"
    exit 20
fi

# print path for processing by other tools (e.g. makepkg --packagelist)
if (( print_path )); then
    realpath -- "$directory"/root
    exit $?
fi

if (( update )); then
    # locking is done by systemd-nspawn
    # shellcheck disable=SC2086
    $AUR_PACMAN_AUTH arch-nspawn -C "$pacman_conf" -M "$makepkg_conf" "$directory"/root \
         "${bindmounts_ro[@]/#/--bind-ro=}" \
         "${bindmounts_rw[@]/#/--bind=}" pacman -Syu --noconfirm "$@"
fi

if (( build )); then
    # use makechrootpkg -c as default build command (sync /root container)
    # arguments after -- are used as makechrootpkg arguments
    # shellcheck disable=SC2086
    $AUR_PACMAN_AUTH "${auth_args[@]}" makechrootpkg -r "$directory" \
         "${bindmounts_ro[@]/#/-D}" "${bindmounts_rw[@]/#/-d}" \
         "${makechrootpkg_args[@]}" -- "${makechrootpkg_makepkg_args[@]}"
fi

# vim: set et sw=4 sts=4 ft=sh:
