#!/usr/bin/env bash
# License: GNU GPLv2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

. "$(librelib common.sh)"
. "$(librelib archroot.sh)"
. "$(librelib mount.sh)"

# $1: chroot
kill_chroot_process(){
    local prefix="$1" flink pid name
    for root_dir in /proc/*/root; do
        flink=$(readlink "$root_dir")
        if [ "x$flink" != "x" ]; then
            if [ "x${flink:0:${#prefix}}" = "x$prefix" ]; then
                # this process is in the chroot...
                pid=$(basename "$(dirname "$root_dir")")
                name=$(ps -p "$pid" -o comm=)
                msg2 "Killing chroot process: %s (%s)" "$name" "$pid"
                kill -9 "$pid"
            fi
        fi
    done
    sleep 1
}

# umask might have been changed in /etc/profile
# ensure that sane default is set again
umask 0022

working_dir=''
files=()

declare -g mount_args=()
mount_args=("-B:/etc/hosts:/etc/hosts")
unshare_args=("-Cfimu")

usage() {
	echo "Usage: ${0##*/} [options] working-dir [chroot arguments]"
	echo "A wrapper around chroot.  Provides support for pacman."
	echo
	echo ' options:'
	echo '    -C <file>     Location of a pacman config file'
	echo '    -M <file>     Location of a makepkg config file'
	echo '    -c <dir>      Set pacman cache'
	echo '    -f <file>     Copy file from the host to the chroot'
	echo '    -s            Do not run setarch'
	echo '    -b <list>     Bind mountargs'
	echo '                  List format [mntarg1:src1:dest1 ... mntargN:srcN:destN]'
	echo '    -N            Disable networking'
	echo '    -h            This message'
	exit 1
}

while getopts 'NhC:M:c:b:f:s' arg; do
	case "$arg" in
		C) pac_conf="$OPTARG" ;;
		M) makepkg_conf="$OPTARG" ;;
		c) cache_dir="$OPTARG" ;;
		f) files+=("$OPTARG") ;;
		s) nosetarch=1 ;;
		b) bindmounts+=("$OPTARG"); mount_args+=(${bindmounts[@]}) ;;
		N) unshare_args+=("-n") ;;
		h|?) usage ;;
		*) error "invalid argument '%s'" "$arg"; usage ;;
	esac
done
shift $((OPTIND - 1))

(( $# < 1 )) && die 'You must specify a directory.'
check_root

working_dir=$(readlink -f "$1")
shift 1

[[ -z $working_dir ]] && die 'Please specify a working directory.'

if [[ -z $cache_dir ]]; then
	cache_dirs=($(pacman -v 2>&1 | grep '^Cache Dirs:' | sed 's/Cache Dirs:\s*//g'))
else
	cache_dirs=("$cache_dir")
fi

pacconf_cmd=$(command -v pacman-conf || command -v pacconf)
# shellcheck disable=2016
host_mirror=$($pacconf_cmd --repo extra Server 2> /dev/null | head -1 | sed -r 's#(.*/)extra/os/.*#\1$repo/os/$arch#')
# shellcheck disable=2016
[[ $host_mirror == *file://* ]] && host_mirror_path=$(echo "$host_mirror" | sed -r 's#file://(/.*)/\$repo/os/\$arch#\1#g')

# {{{ functions
build_mount_args() {
	if [[ -n $host_mirror_path ]]; then
		mount_args+=("-B:$host_mirror_path:$host_mirror_path")
	fi

	mount_args+=("-B:${cache_dirs[0]}:${cache_dirs[0]}")

	for cache_dir in "${cache_dirs[@]:1}"; do
		mount_args+=("-Br:$cache_dir:$cache_dir")
	done
}

copy_hostconf () {
	cp -a /etc/pacman.d/gnupg "$working_dir/etc/pacman.d"
	echo "Server = $host_mirror" >"$working_dir/etc/pacman.d/mirrorlist"

	[[ -n $pac_conf ]] && cp "$pac_conf" "$working_dir/etc/pacman.conf"
	[[ -n $makepkg_conf ]] && cp "$makepkg_conf" "$working_dir/etc/makepkg.conf"

	local file
	for file in "${files[@]}"; do
		mkdir -p "$(dirname "$working_dir$file")"
		cp -T "$file" "$working_dir$file"
	done

	sed -r "s|^#?\\s*CacheDir.+|CacheDir = ${cache_dirs[*]}|g" -i "$working_dir/etc/pacman.conf"
}

chroot_extra_mount() {
	chroot_add_resolv_conf "${working_dir}"

	for arg in "${mount_args[@]}"; do
		local flag=${arg%%:*} dest=${arg##*:} src=${arg%:*}
		src=${src#*:}
		chroot_mount "${src}" "${working_dir}${dest}" "${flag}"
	done
}
# }}}

umask 0022

# Sanity check
if [[ ! -f "$working_dir/.arch-chroot" ]]; then
	die "'%s' does not appear to be an Arch chroot." "$working_dir"
elif [[ $(cat "$working_dir/.arch-chroot") != "$CHROOT_VERSION" ]]; then
	die "chroot '%s' is not at version %s.  Please rebuild." "$working_dir" "$CHROOT_VERSION"
fi

build_mount_args
chroot_api_mount "${working_dir}" || die "failed to setup API filesystems in chroot %s" "${working_dir}"
chroot_extra_mount
cache_dirs+=('/repo/')
copy_hostconf

eval "$(grep -a '^CARCH=' "$working_dir/etc/makepkg.conf")"
case "$CARCH" in
	armv7h) CARCH=armv7l;;
esac

[[ -z $nosetarch ]] || unset CARCH

${CARCH:+setarch "$CARCH"} unshare \
	 "${unshare_args[@]}" \
	--mount-proc \
	--setgroups allow \
	-- \
	chroot \
	"${working_dir}" \
	"$@"
ret=$?
kill_chroot_process "${working_dir}"
exit $ret
