#!/bin/bash

## Copyright (C) 2017 - 2023 ENCRYPTED SUPPORT LP <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## This script fixes the interaction between multiple tools.
##
## - `cowbuilder` hardcodes setting '--buildplace'.
##
## - `pbuilder` sets by default '--force-check-gpg'.
##   (This could also be sorted in `dist_build_pbuilder_config_file`.)
## - `pbuilder` `/usr/lib/pbuilder/pbuilder-createbuildenv` hardcodes calling `${DEBOOTSTRAP}`.
##
## - `mmdebstrap` does not support '--force-check-gpg'.
## - `mmdebstrap` requires the parameters in a certain order.

set -x
set -e

true "${bold}INFO: Currently running: $0 (derivative-maker help-steps/pbuilder-debootstrap-command-filter)${reset}"

args_original="$@"

## Example $@ when called by cowbuilder:
## --arch=amd64 --include=apt --variant=buildd --force-check-gpg bookworm /var/cache/pbuilder/base.cow_amd64 http://HTTPS///deb.debian.org/debian

## Debugging.
true "BUILDPLACE: $BUILDPLACE"
true "dist_grml_mount_point: $dist_grml_mount_point"
true "http_proxy: $http_proxy"
true "https_proxy: $https_proxy"
true "ALL_PROXY: $ALL_PROXY"
true "REPO_PROXY: ${REPO_PROXY}"
true "APTGETOPT: $APTGETOPT"
true "dist_build_target_arch: $dist_build_target_arch"
true "dist_build_multiarch_package_item: $dist_build_multiarch_package_item"
true "dist_build_apt_stable_release: $dist_build_apt_stable_release"
true "dist_build_apt_sources_mirror: $dist_build_apt_sources_mirror"
true "dist_aptgetopt_file: $dist_aptgetopt_file"
cat "$dist_aptgetopt_file"
true "dist_build_sources_list_primary: $dist_build_sources_list_primary"
cat "$dist_build_sources_list_primary"

## args_filtered is currently not required. Just keeping it for debugging
## purposes in applications such as grml-debootstrap, cowbuilder or pbuilder in
## the future set additional arguments.

args_filtered="$args_original"

true "INFO: filter out default mirror by cowbuilder or pbuilder (if we were not using --mirror)"
args_filtered="${args_filtered//"http://ftp.us.debian.org/debian"}"
args_filtered="${args_filtered//"https://ftp.us.debian.org/debian"}"

true "INFO: filter out default mirror by grml-debootstrap"
args_filtered="${args_filtered//"http://httpredir.debian.org/debian"}"
args_filtered="${args_filtered//"https://httpredir.debian.org/debian"}"
args_filtered="${args_filtered//"http://deb.debian.org/debian"}"
args_filtered="${args_filtered//"https://deb.debian.org/debian"}"

true "INFO: filter out --mirror"
args_filtered="${args_filtered//"$dist_build_apt_sources_mirror"}"

true "INFO: filter out variable BUILDPLACE (set by pbuilder) because we need to adjust the order of arguments"
args_filtered="${args_filtered//"$BUILDPLACE"}"

true "INFO: filter out --force-check-gpg (set by cowbuilder or pbuilder) since mmdebstrap does this by default and therefore does not support that command"
args_filtered="${args_filtered//"--force-check-gpg"}"

true "INFO: filter out --verbose (set by grml-debootstrap) since we add it by ourselves."
args_filtered="${args_filtered//"--verbose"}"

true "INFO: filter out --include=apt (set by cowbuilder or pbuilder) since we will use our own --include switch."
args_filtered="${args_filtered//"--include=apt"}"

true "INFO: Default to default --variant. Decide which packages are to be installed using --include."
args_filtered="${args_filtered//"--variant=buildd"}"

true "INFO: filter out '--arch amd64' since we will use our own."
args_filtered="${args_filtered//"--arch $dist_build_target_arch"}"
true "INFO: filter out '--arch=amd64' since we will use our own."
args_filtered="${args_filtered//"--arch=$dist_build_target_arch"}"

true "INFO: filter out suite since we will use our own."
args_filtered="${args_filtered//"$dist_build_apt_stable_release"}"

true "INFO: filter out --include (that grml-debootstrap sets) since we will use our own."
args_filtered="${args_filtered//"--include"}"

true "INFO: filter out sources list file (that grml-debootstrap sets) since we will use our own."
args_filtered="${args_filtered//"$dist_build_sources_list_primary"}"

true "INFO: filter out dist_build_apt_sources_mirror (that pbuilder sets) since we will use our own."
args_filtered="${args_filtered//"$dist_build_apt_sources_mirror"}"

## TODO: broken
#true "INFO: filter out extraneous white spaces."
#args_filtered="${args_filtered//" "}"

true "FINAL args_filtered: $args_filtered"
## example args_filtered:
## /mnt/debootstrap.15557

## mmdebstrap needs 3 positional arguments.
## suite buildplace path-to-sources-list
## For example when using grml-debootstrap:
## bookworm /mnt/debootstrap.15557 /home/user/derivative-maker/build_sources/debian_stable_current_clearnet.list
## For example when using cowbuilder:
## bookworm /var/cache/pbuilder/base.cow_amd64 /home/user/derivative-maker/build_sources/debian_stable_current_clearnet.list

if [ "$BUILDPLACE" = "" ]; then
   ## For grml-debootstrap.

   true "$0: Variable BUILDPLACE is unset."
   true "$0: This means this script was probably called by grml-debootstrap."
   true "$0: Setting BUILDPLACE to: $dist_grml_mount_point"
   BUILDPLACE="$dist_grml_mount_point"

   ## - zstd: for faster speed
   ##   https://github.com/grml/grml-debootstrap/issues/211
   ##
   ## - libpam-tmpdir on the host operating system breaks grml-debootstrap
   ##   https://github.com/grml/grml-debootstrap/issues/232
   ##
   ## - libpam-systemd
   ##   dracut requires libpam-systemd
   ##   https://github.com/grml/grml-debootstrap/issues/233
   ##
   ## - dracut-live
   ##   'dracut --add dmsquash-live' requires dracut-live
   ##
   ## - grub2-common
   ##   grml-debootstrap relies on packages(-arm64) file to install this package.
   ##   https://github.com/grml/grml-debootstrap/issues/258#issuecomment-1848595257
   include_opt="--include=eatmydata,apt-transport-tor,gpg,gpg-agent,fasttrack-archive-keyring,zstd,libpam-tmpdir,libpam-systemd,dracut-live,grub2-common"

   ## example:
   ## /home/user/derivative_dot/derivative-maker/help-steps/mmdebstrap --verbose --debug --format directory --variant=required --architectures=amd64 --aptopt=/home/user/derivative-binary/30_derivative-maker.conf --include=eatmydata,apt-transport-tor,gpg,gpg-agent,fasttrack-archive-keyring bookworm /mnt/debootstrap.33132 /home/user/derivative-maker/build_sources/debian_stable_current_clearnet.list
else
   ## For cowbuilder.

   true "$0: Variable BUILDPLACE is set."
   true "$0: This means this script was probably called by called by pbuilder which was called by cowbuilder."

   ## build dependencies
   ## - libpam-tmpdir: Not strictly required. Only keeping for consistency with above.
   include_opt="--include=apt,sudo,devscripts,debhelper,strip-nondeterminism,fakeroot,apt-transport-tor,eatmydata,aptitude,cowdancer,fasttrack-archive-keyring,libpam-tmpdir"

   ## example:
   ## /home/user/derivative-maker/help-steps/mmdebstrap --verbose --debug --format directory --variant=required --architectures=amd64 --aptopt=/home/user/derivative-binary/30_derivative-maker.conf --include=apt,sudo,devscripts,debhelper,strip-nondeterminism,fakeroot,apt-transport-tor,eatmydata,aptitude,cowdancer,fasttrack-archive-keyring bookworm /var/cache/pbuilder/base.cow_amd64 /home/user/derivative-maker/build_sources/debian_stable_current_clearnet.list
fi

## Sanity tests.
[ -n "$dist_build_apt_stable_release" ] || error "dist_build_apt_stable_release is unset!"
[ -n "$BUILDPLACE" ] || error "BUILDPLACE is unset!"
[ -n "$dist_build_sources_list_primary" ] || error "dist_build_sources_list_primary is unset!"

## '--variant=required' is only supported by 'mmdebstrap'. It might not be supported by 'debootstrap'.

#"$dist_source_help_steps_folder/mmdebstrap"

## Deletion of /etc/apt/apt.conf.d/99mmdebstrap is required for compatibility with live-boot (non-customized).
## This is because otherwise 99mmdebstrap results in not installing 'Recommends:' packages, which
## are actually a dependency for a fully functional ISO with functional login.

mmdebstrap \
   --verbose \
   --debug \
   --format directory \
   --variant=required \
   --architectures="$dist_build_multiarch_package_item" \
   --aptopt="$dist_aptgetopt_file" \
   --customize-hook='echo "mmdebstrap hooks START"' \
   --customize-hook='echo "----------"' \
   --customize-hook='ls -la "$1" || true' \
   --customize-hook='realpath "$1" || true' \
   --customize-hook='echo "----------"' \
   --customize-hook='cat "$1/etc/apt/sources.list.d/0000debian_stable_current_clearnet.list" || true' \
   --customize-hook='cat "$1/etc/apt/apt.conf.d/99mmdebstrap" || true' \
   --customize-hook='echo "----------"' \
   --customize-hook='rm --force --verbose "$1/etc/apt/sources.list.d/0000debian_stable_current_clearnet.list"' \
   --customize-hook='rm --force --verbose "$1/etc/apt/apt.conf.d/99mmdebstrap"' \
   --customize-hook='echo "----------"' \
   --customize-hook='echo "mmdebstrap hooks END"' \
   $include_opt \
   "$dist_build_apt_stable_release" \
   "$BUILDPLACE" \
   "$dist_build_sources_list_primary"

true "${bold}INFO: End of script: $0 (derivative-maker help-steps/pbuilder-debootstrap-command-filter)${reset}"
