#!/bin/bash -e

DESCRIPTION="Jump into a debug schroot environment to debug a (failed) package build."

MBD_BUILDS_DIR_REL="var/shared/builds"
MBD_DEBUG_DIR_REL="var/shared/debug"
MBD_BUILDS_DIR="${HOME}/${MBD_BUILDS_DIR_REL}"
MBD_DEBUG_DIR="${HOME}/${MBD_DEBUG_DIR_REL}"

case "${1}" in
	""|"-h"|"--help")
		cat <<EOF
Usage: [MBD_CODENAME=foo] mini-buildd-debug-build <BUILD> (as user mini-buildd)

${DESCRIPTION}

<BUILD> is a relative path below

${MBD_BUILDS_DIR_REL}

Best cd to there to make us of directory completion.

MBD_CODENAME is computed automatically (via buildrequest), but you may
also override it via env.

Example:

mini-buildd-debug-build foo/1.2.3-1~test11+1/<TIMECODE>/amd64

You will work in new schroot sessions (prefixed by 'debug-mini-buildd').

You will work on copies of the builds directory, going to

${MBD_DEBUG_DIR_REL}

Both sessions and copies need to be cleaned up manually.

EOF
	exit 0
esac

[ $(id --user --name) == "mini-buildd" ] || { printf "E: Needs to be run as mini-buildd.\nTry 'mini-buildd-debug-build --help'.\n" >&2; exit 1; }

MBD_BUILD="${1%/}"  # Remove trailing slash, else cut magic below won't work.
MBD_SOURCE=$(echo -n "${MBD_BUILD}" | cut -d/ -f1)
MBD_ARCH=$(echo -n "${MBD_BUILD}" | rev | cut -d/ -f1 | rev)
MBD_BUILD_DIR="${MBD_BUILDS_DIR}/${MBD_BUILD}"
MBD_DSC="$(find ${MBD_BUILD_DIR} -name "*.dsc" -printf "%f")"
MBD_DST="${MBD_SOURCE}-dst"
MBD_ID="$(printf "%s" ${RANDOM} | md5sum | head --bytes=32)"
MBD_BUILDREQUEST="$(find ${MBD_BUILD_DIR} -name "*mini-buildd-buildrequest_${MBD_ARCH}.changes" -printf "%f")"
: ${MBD_CODENAME:="$(grep "^Distribution" "${MBD_BUILD_DIR}/${MBD_BUILDREQUEST}" | cut -d":" -f2 | cut -d"-" -f1 | tr --delete '[:space:]')"}

printf "\nI: Will create schroot session and builds copy with ID: %s\n\n" "${MBD_ID}"
read -p"Debug ${MBD_SOURCE} (${MBD_DSC}) on ${MBD_CODENAME}/${MBD_ARCH} (Ctrl-c to abort)? " DUMMY

mkdir --verbose --parents "${MBD_DEBUG_DIR}"
MBD_SESSION="debug-mini-buildd-${MBD_CODENAME}-${MBD_ARCH}-${MBD_SOURCE}-${MBD_ID}"
MBD_DEBUG_TMPDIR=$(mktemp --directory "${MBD_DEBUG_DIR}/${MBD_SOURCE}-${MBD_ID}-XXX")
cp --verbose --archive "${MBD_BUILD_DIR}" "${MBD_DEBUG_TMPDIR}"
chmod --verbose u+w "${MBD_DEBUG_TMPDIR}/${MBD_ARCH}/${MBD_DSC}"  # ro by sbuild (?) (will avoid builds).

schroot --begin-session --chroot="mini-buildd-${MBD_CODENAME}-${MBD_ARCH}" --session-name="${MBD_SESSION}"
schroot --run-session --chroot="${MBD_SESSION}" --user=root -- run-parts "${MBD_DEBUG_TMPDIR}/${MBD_ARCH}/.setup.d/"
schroot --run-session --chroot="${MBD_SESSION}" --directory="${MBD_DEBUG_TMPDIR}/${MBD_ARCH}" -- dpkg-source -x "${MBD_DSC}" "${MBD_DST}"  # Note: Using '-x' not '--extract' to be compatible with older dpkg tools.
schroot --run-session --chroot="${MBD_SESSION}" --user=root -- apt-get --yes --option=Dpkg::Options::=--force-confdef install devscripts
schroot --run-session --chroot="${MBD_SESSION}" --user=root --directory="${MBD_DEBUG_TMPDIR}/${MBD_ARCH}/${MBD_DST}" -- mk-build-deps --tool="apt-get --yes -o Debug::pkgProblemResolver=yes --no-install-recommends" --install --remove

printf "\nI: Debug schroot session: %s\n" "${MBD_SESSION}"
printf "I: Copied builds data   : %s\n\n" "${MBD_DEBUG_TMPDIR}"
schroot --run-session --chroot="${MBD_SESSION}" --directory="${MBD_DEBUG_TMPDIR}/${MBD_ARCH}/${MBD_DST}"
