# ==========================================================================
#              ____        _ _     _     _____           _
#             | __ ) _   _(_) | __| |   |_   _|__   ___ | |___
#             |  _ \| | | | | |/ _` |_____| |/ _ \ / _ \| / __|
#             | |_) | |_| | | | (_| |_____| | (_) | (_) | \__ \
#             |____/ \__,_|_|_|\__,_|     |_|\___/ \___/|_|___/
#
#                           --- Build-Tools ---
#                https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# Unified Build Tool PBuilder Configuration
# Copyright (C) 2012-2026 by Thomas Dreibholz
#
# 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 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/>.
#
# Contact: thomas.dreibholz@gmail.com

# Bash options:
set -e


# ====== Initialise =========================================================
. /etc/os-release
if [ "${OS}" == "" ] ; then
   OS="${ID}"
fi
if [ "${DIST}" == "" ] ; then
   DIST="${VERSION_CODENAME}"
   if [ "${DIST}" == "" ] ; then
      DIST="${VERSION_ID}"
   fi
fi
if [ "${ARCH}" == "" ] ; then
   ARCH="$(dpkg --print-architecture)"
fi
echo "Initialising PBuilder: OS=${OS} DIST=${DIST} ARCH=${ARCH}"


# ====== Settings ===========================================================
DISTRIBUTION="${DIST}"
COMPONENTS="main universe"
if [ "${OS}" == "ubuntu" ] ; then
   COMPONENTS="main universe"
   if [ "${ARCH}" == "amd64" ] || [ "${ARCH}" == "i386" ] ; then
      MIRRORSITE="http://dk.archive.ubuntu.com/ubuntu/"
   else
      MIRRORSITE="http://ports.ubuntu.com/ubuntu-ports/"
   fi
   KEYRING="/usr/share/keyrings/ubuntu-archive-keyring.gpg"
elif [ "${OS}" == "debian" ] ; then
   if [ "${ARCH}" == "m68k" ] || [ "${ARCH}" == "riscv64" ] ; then
      # Debian Ports (special architectures)
      COMPONENTS="main"
      MIRRORSITE="http://ftp.ports.debian.org/debian-ports/"
      KEYRING="/usr/share/keyrings/debian-ports-archive-keyring.gpg"
   else
      # Debian (supported architectures)
      COMPONENTS="main"
      MIRRORSITE="http://ftp.dk.debian.org/debian/"
      KEYRING="/usr/share/keyrings/debian-archive-keyring.gpg"
   fi
else
   echo >&2 "ERROR: Unknown Linux distribution ${OS}!"
   exit 1
fi
if [ ! -e "${KEYRING}" ] ; then
   echo >&2 "ERROR: Missing keyring file ${KEYRING}!"
   exit 1
fi
DEBOOTSTRAPOPTS=("${DEBOOTSTRAPOPTS[@]}" "--keyring=${KEYRING}")
DEBOOTSTRAPOPTS=("${DEBOOTSTRAPOPTS[@]}" "--variant=buildd")
ARCHITECTURE="${ARCH}"

COMPRESSPROG=pigz
EXTRAPACKAGES=eatmydata
EATMYDATA=yes

cores="$(getconf _NPROCESSORS_ONLN)"
export CONCURRENCY_LEVEL="${cores}"
export DEB_BUILD_OPTIONS="parallel=${cores}"

# Support tmpfs build
# (see http://www.davromaniak.eu/index.php?post/2011/07/12/Pbuilder-and-tmpfs-can-be-friends)
# Add to /etc/fstab:
# tmpfs   /var/cache/pbuilder/build   tmpfs   defaults,auto   0   2
#
APTCACHEHARDLINK=no


# ====== Directories ========================================================
NAME="${OS}-${DISTRIBUTION}-${ARCHITECTURE}"
BASETGZ="/var/cache/pbuilder/${NAME}-base.tgz"
APTCACHE="/var/cache/pbuilder/aptcache/${NAME}/"
BUILDPLACE="/var/cache/pbuilder/build/${NAME}/"
BUILDRESULT="/var/cache/pbuilder/result/${NAME}/"
HOOKDIR="/var/cache/pbuilder/hook.d/"

mkdir -p "${APTCACHE}"
mkdir -p "${BUILDRESULT}"
mkdir -p "${HOOKDIR}"


# ====== Debugging hook =====================================================
cat >${HOOKDIR}/C10shell <<EOF
#!/bin/sh
# Run when the build fails:
# - Install helpful tools (bash, joe, less, etc.).
# - Run a shell.

apt-get install -y --force-yes bash joe less
cd /tmp/buildd/*/debian/..
/bin/bash </dev/tty >/dev/tty 2>/dev/tty
EOF

chmod a+x ${HOOKDIR}/C10shell
