#!/usr/bin/env bash
set -eE
# Copyright (C) 2011-2012 Nicolás Reynolds <fauno@parabola.nu>
# Copyright (C) 2012-2013, 2015, 2017-2018, 2024 Luke T. Shumaker <lukeshu@parabola.nu>
#
# If you don't see the string "EMBEDLIB.SH" below, but see function
# definitions for panic() et al., then this is a generated file, and
# contains some code from messages.sh/common.sh.  See the source
# distribution for full copyright information.
#
# License: GNU GPLv3+
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Performs chroot cleanup smartly, it only removes the unneeded packages or
# leaves you with a clean system

export TEXTDOMAIN='libretools'

# Begin embedlib.sh ############################################################

if type gettext &>/dev/null; then
	_() { gettext "$@"; }
else
	_() { echo -n "$@"; }
fi

. /usr/share/makepkg/util.sh

declare -ir EXIT_SUCCESS="0"
declare -ir EXIT_FAILURE="1"
declare -ir EXIT_INVALIDARGUMENT="2"
_l () 
{ 
    TEXTDOMAIN='librelib' "$@"
}
_p () 
{ 
    TEXTDOMAIN='pacman-scripts' "$@"
}
panic () 
{ 
    local mesg="$(_l _ 'General error')";
    if [[ $# -gt 0 ]]; then
        mesg="$(print "$@")";
    fi;
    _l print '%s: BUG: please report this to Parabola: %s' "${0##*/}" "$mesg" 1>&2;
    local i;
    for ((i = 0; i < ${#FUNCNAME[@]}; i++))
    do
        _l print '\t%s()\tcalled at %s:%s' "${FUNCNAME[$i]}" "${BASH_SOURCE[$((i + 1))]:-$0}" "${BASH_LINENO[$i]}" 1>&2;
    done;
    exit $EXIT_FAILURE
}
print () 
{ 
    [[ $# -ge 1 ]] || panic;
    local mesg;
    mesg="$(_ "$1")";
    shift;
    printf -- "$mesg\n" "$@"
}
gnuerror () 
{ 
    [[ $# -ge 1 ]] || panic;
    local fmt mesg;
    fmt="$(_ "$1")";
    shift;
    printf -v mesg -- "$fmt" "$@";
    printf -- '%s: %s\n' "${0##*/}" "$mesg" 1>&2
}
whitespace_collapse () 
{ 
    [[ $# == 0 ]] || panic;
    tr '\n' '\r' | sed -E -e 's/\r/  /g' -e 's/\t/ /g' -e 's/(^|[^.!? ]) +/\1 /g' -e 's/([.!?])  +/\1  /g' -e 's/\s+$//'
}
prose () 
{ 
    [[ $# -ge 1 ]] || panic;
    local mesg;
    mesg="$(_ "$(whitespace_collapse <<< "$1")")";
    shift;
    printf -- "$mesg" "$@" | fmt -u
}
bullet () 
{ 
    [[ $# -ge 1 ]] || panic;
    local mesg;
    mesg="$(_ "$(whitespace_collapse <<< "$1")")";
    shift;
    printf -- "$mesg" "$@" | fmt -u -w 71 | sed -e '1s/^/  - /' -e '2,$s/^/    /'
}
flag () 
{ 
    _flag "$@"
}
_flag () 
{ 
    local args=("$@");
    declare -i flaglen=0;
    while [[ $# -gt 0 ]]; do
        if [[ $1 == *: ]]; then
            shift 1;
        else
            if [[ ${#1} -gt $flaglen ]]; then
                flaglen=${#1};
            fi;
            shift 2;
        fi;
    done;
    set -- "${args[@]}";
    declare -i indent=12;
    while [[ $indent -lt $flaglen ]]; do
        indent+=8;
    done;
    local fmt2 fmt1;
    fmt2="  %-${indent}s  %s\n";
    printf -v fmt1 "  %-${indent}s  %%s\n" '';
    while [[ $# -gt 0 ]]; do
        if [[ $1 == *: ]]; then
            printf -- ' %s\n' "$(_ "$1")";
            shift;
        else
            [[ $# -gt 1 ]] || panic;
            local flag=$1;
            local desc;
            desc="$(_ "$(whitespace_collapse <<< "$2")")";
            shift 2;
            local lines;
            IFS='
' lines=($(fmt -u -w $((71 - indent)) <<< "$desc"));
            printf -- "$fmt2" "$flag" "${lines[0]}";
            [[ ${#lines[@]} -lt 2 ]] || printf -- "$fmt1" "${lines[@]:1}";
        fi;
    done
}
eval "$(
	fns=(
		plain
		msg
		msg2
		ask

		warning
		error
		plainerr
	)

	# declare _makepkg_${fn} as a copy of ${fn}
	declare -f "${fns[@]}" | sed 's/^[a-z]/_makepkg_&/'

	# re-declare ${fn} as a wrapper around _makepkg_${fn}
	printf '%s() { local mesg; mesg="$(_ "$1")"; local QUIET=${QUIET:-0}; _p _makepkg_"${FUNCNAME[0]}" "$mesg" "${@:2}" >&2; }\n' \
	       "${fns[@]}"
)"

# End embedlib.sh ##############################################################

# User interface ###############################################################
usage() {
	print 'Usage: [DRYRUN={true|false}] %s [OPTIONS]' "${0##*/}"
	print "Clean packages from the chroot that aren't nescessary to build ./PKGBUILD."
	echo
	print 'Options:'
	flag \
		'-h, --help' 'Show this message'
}

mode=run
if ! args="$(getopt -n "${0##*/}" -o 'h' -l 'help' -- "$@")"; then
	mode=errusage
else
	eval "set -- $args"
	while true; do
		flag=$1
		shift
		case "$flag" in
			-h | --help) mode=usage ;;
			--) break ;;
			*) panic 'unhandled flag: %q' "$flag" ;;
		esac
	done
	if [[ $mode == run && $# -gt 0 ]]; then
		gnuerror 'Extra arguments: %s' "$*"
		mode=errusage
	fi
fi
case "$mode" in
	errusage)
		print "Try '%s --help' for more information." "${0##*/}" >&2
		exit $EXIT_INVALIDARGUMENT
		;;
	usage)
		usage
		exit $EXIT_SUCCESS
		;;
	run) : ;;
	*) panic 'invalid mode: %q' "$mode" ;;
esac

DRYRUN=${DRYRUN:-false}
if [[ ! -f /.arch-chroot ]] && ! ${DRYRUN}; then
	error "(chcleanup): Must be run inside of a chroot"
	exit $EXIT_FAILURE
fi

# Load configuration ###########################################################
CHROOTPKG=(base-devel)
# Note: the in-chroot pkgconfdir is non-configurable, this is
# intentionally hard-coded.
source /etc/libretools.d/chroot.conf
# If we're running makepkg
if [[ -f ./PKGBUILD ]]; then
	if [[ ! -f ./.SRCINFO || ./PKGBUILD -nt ./.SRCINFO ]]; then
		sudo -u "#$(stat -c %u -- ./PKGBUILD)" sh -c 'makepkg --printsrcinfo > .SRCINFO'
	fi
	CARCH="$(
		. /etc/makepkg.conf
		printf '%s' "$CARCH"
	)"
	mapfile -t DEPENDS < <(sed -nE -e "s/^\\s+(|make|check)depends(|_${CARCH}) = //p" -e '/^\s*pkgname/q' <.SRCINFO)
else
	DEPENDS=()
fi

# Main #########################################################################

msg "Cleaning chroot..."

# Sync the local repo with pacman (a limited form of `pacman -Sy`)
cp /repo/repo.db /var/lib/pacman/sync/repo.db

# Setup the temporary directory
TEMPDIR="$(mktemp --tmpdir -d "${0##*/}.XXXXXXXXXX")"
trap "rm -rf -- ${TEMPDIR@Q}" EXIT

# Set up a scratch pacman DB
mkdir -- "$TEMPDIR/db" "$TEMPDIR/db/local" "$TEMPDIR/hooks"
cp -a -t "${TEMPDIR}/db" -- /var/lib/pacman/sync
{
	echo /usr/share/libalpm/hooks
	pacman-conf HookDir
} | while read -r dir; do
	for hook in "$dir"/*.hook; do
		ln -sfT -- /dev/null "$TEMPDIR/hooks/${hook##*/}"
	done
done
pacman=(pacman --dbpath="$TEMPDIR/db" --hookdir="$TEMPDIR/hooks")

# Get the full list of packages needed by dependencies, including the base system
msg2 "Creating a full list of packages..."
for var in CHROOTPKG CHROOTEXTRAPKG DEPENDS; do
	declare -n pkgsref="$var"
	if [[ $var == DEPENDS ]]; then
		mapfile -t pkgs < <("${pacman[@]}" -T -- "${pkgsref[@]}")
	else
		pkgs=("${pkgsref[@]}")
	fi
	if ((${#pkgs[@]} == 0)); then
		continue
	fi
	"${pacman[@]}" -S --dbonly --noscriptlet --needed --noconfirm -- "${pkgs[@]}" </dev/null >&"$TEMPDIR/pacman.txt" || ret=$?
	if ((ret != 0)); then
		error "Could not create a full list of packages, exiting."
		plain "This is likely caused by a dependency that could not be found."
		sed 's/^/ >/' <"$TEMPDIR/pacman.txt" >&2
		exit $ret
	fi
done
"${pacman[@]}" -Qq >"$TEMPDIR/pkglist.txt"

# Diff installed packages against a clean chroot then remove leftovers
packages=($(comm -23 \
	<(pacman -Qq | sort -u) \
	<(sort -u "$TEMPDIR/pkglist.txt")))
if [[ ${#packages[@]} == 0 ]]; then
	msg2 "No packages to remove"
else
	msg2 "Removing %d packages" ${#packages[@]}

	if ${DRYRUN}; then
		echo "${packages[*]}"
	else
		# Only remove leftovers, -Rcs removes too much
		pacman --noconfirm -R --nosave "${packages[@]}"
	fi
fi

packages=($(comm -13 \
	<(pacman -Qq | sort -u) \
	<(sort -u "$TEMPDIR/pkglist.txt")))
if [[ ${#packages[@]} == 0 ]]; then
	msg2 "No packages to add"
else
	msg2 "Adding %d packages" ${#packages[@]}

	if ${DRYRUN}; then
		echo "${packages[*]}"
	else
		pacman --noconfirm -S "${packages[@]}"
	fi
fi
