#!/bin/sh

## live-build(7) - System Build Scripts
## Copyright (C) 2016-2020 The Debian Live team
## Copyright (C) 2006-2015 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e

# Including common functions
[ -e "${LIVE_BUILD}/scripts/build.sh" ] && . "${LIVE_BUILD}/scripts/build.sh" || . /usr/lib/live/build.sh

# Setting static variables
DESCRIPTION="Bootstrap a Debian system with debootstrap(8)"
USAGE="${PROGRAM} [--force]"

# Processing arguments and configuration files
Init_config_data "${@}"

if [ "${LB_BOOTSTRAP}" != "debootstrap" ]
then
	exit 0
fi

if ! command -v debootstrap >/dev/null
then
	Echo_error "debootstrap - command not found"
	exit 1
fi

Echo_message "Begin bootstrapping system..."

Check_package host /usr/sbin/debootstrap debootstrap
Check_installed host /usr/bin/eatmydata eatmydata
if [ "${INSTALL_STATUS}" -eq 0 ]
then
	Echo_message "eatmydata found. It will be used do disable the sync command in the second stage of debootstrap"
	_EATMYDATA="eatmydata"
else
	# eatmydata is optional
	_EATMYDATA=""
fi

# Checking stage file
Check_stagefile "bootstrap"
# Note, this must match that used in `bootstrap_cache`
Check_stagefile "bootstrap_cache.restore"

# Acquire lock file
Acquire_lockfile

Print_breakage ()
{
	Echo_message "If the following stage fails, the most likely cause of the problem is with your mirror configuration or a caching proxy."
}

# Creating chroot directory
mkdir -p chroot

# Setting debootstrap options
if [ -n "${LB_ARCHITECTURE}" ]
then
	DEBOOTSTRAP_OPTIONS="${DEBOOTSTRAP_OPTIONS} --arch=${LB_ARCHITECTURE}"
fi

if [ "${LB_PARENT_ARCHIVE_AREAS}" != "main" ]
then
	# Modify archive areas to remove leading/trailing whitespaces and replace other whitespace with commas
	DEBOOTSTRAP_OPTIONS="${DEBOOTSTRAP_OPTIONS} --components=$(echo ${LB_PARENT_ARCHIVE_AREAS} | sed -e 's| |,|g')"
	FOREIGN_DEBOOTSTRAP_OPTIONS="--components=$(echo ${LB_PARENT_ARCHIVE_AREAS} | sed -e 's| |,|g')"
fi

if [ "${_VERBOSE}" = "true" ]
then
	DEBOOTSTRAP_OPTIONS="${DEBOOTSTRAP_OPTIONS} --verbose"
fi

# If LB_APT_SECURE is false, do not check signatures of the Release file
if [ "${LB_APT_SECURE}" = "false" ]
then
	DEBOOTSTRAP_OPTIONS="${DEBOOTSTRAP_OPTIONS} --no-check-gpg"
else
	DEBOOTSTRAP_OPTIONS="${DEBOOTSTRAP_OPTIONS} --force-check-gpg"
fi

if [ "${LB_INITRAMFS}" = "live-boot" -a "${LB_INITSYSTEM}" = "sysvinit" ]
then
	DEBOOTSTRAP_OPTIONS="${DEBOOTSTRAP_OPTIONS} --include=live-config-sysvinit"
fi

if [ "${LB_CACHE_PACKAGES}" = "true" ]
then
	if ls cache/packages.bootstrap/*.deb > /dev/null 2>&1
	then
		mkdir -p chroot/var/cache/apt/archives
		cp cache/packages.bootstrap/*.deb chroot/var/cache/apt/archives
	fi

	Print_breakage
	Echo_message "Running debootstrap (download-only)..."
	debootstrap ${DEBOOTSTRAP_OPTIONS} --download-only "${LB_PARENT_DISTRIBUTION_CHROOT}" chroot "${LB_PARENT_MIRROR_BOOTSTRAP}" ${DEBOOTSTRAP_SCRIPT}

	# Removing old cache
	rm -f cache/packages.bootstrap/*.deb

	# Saving new cache
	mkdir -p cache/packages.bootstrap
	cp chroot/var/cache/apt/archives/*.deb cache/packages.bootstrap
fi

Print_breakage
Echo_message "Running debootstrap..."

# Run appropriate bootstrap, i.e. foreign or regular bootstrap
if [ -n "${LB_BOOTSTRAP_QEMU_ARCHITECTURE}" ]; then

	if [ -n "${LB_BOOTSTRAP_QEMU_EXCLUDE}" ]
	then
		DEBOOTSTRAP_OPTIONS="${DEBOOTSTRAP_OPTIONS} --exclude=$(echo ${LB_BOOTSTRAP_QEMU_EXCLUDE} | sed 's|  *|,|g')"
	fi

	Echo_message "Bootstrap will be foreign"
	debootstrap ${DEBOOTSTRAP_OPTIONS} --foreign "${LB_PARENT_DISTRIBUTION_CHROOT}" chroot "${LB_PARENT_MIRROR_BOOTSTRAP}" ${DEBOOTSTRAP_SCRIPT}

	Echo_message "Running debootstrap second stage under QEMU"
	Chroot chroot /bin/sh /debootstrap/debootstrap --second-stage ${FOREIGN_DEBOOTSTRAP_OPTIONS}
else
	${_EATMYDATA} debootstrap ${DEBOOTSTRAP_OPTIONS} "${LB_PARENT_DISTRIBUTION_CHROOT}" chroot "${LB_PARENT_MIRROR_BOOTSTRAP}" ${DEBOOTSTRAP_SCRIPT}
fi

# If there's an os-release file, copy it to /etc/ and add some extra fields that identify the live image
if [ "${LB_SYSTEM}" = "live" ] && [ -e chroot/usr/lib/os-release ]; then
	# Ensure a potential update doesn't clobber our changes
	if ! Chroot chroot dpkg-divert --list /etc/os-release | grep -q /etc/os-release; then
		Chroot chroot dpkg-divert --quiet --local --add --no-rename --divert /etc/os-release.debootstrap /etc/os-release
	fi
	rm -f chroot/etc/os-release
	cp chroot/usr/lib/os-release chroot/etc/os-release
	# Ensure the metadata is sensible, even for unstable images
	if [ "${LB_PARENT_DISTRIBUTION_CHROOT}" = "sid" ] || [ "${LB_PARENT_DISTRIBUTION_CHROOT}" = "unstable" ]; then
		sed -i "s/VERSION_CODENAME=.*/VERSION_CODENAME=sid/g" chroot/etc/os-release
		echo "RELEASE_TYPE=development" >> chroot/etc/os-release
	fi
	cat <<EOF >> chroot/etc/os-release
IMAGE_ID=live
BUILD_ID=$(date --utc -d @${SOURCE_DATE_EPOCH} +%Y%m%dT%H%M%SZ)
EOF
fi

# Deconfiguring debootstrap configurations
rm -f chroot/etc/hosts

# Removing bootstrap cache
rm -f chroot/var/cache/apt/archives/*.deb

# Creating stage file
Create_stagefile "bootstrap"
