#!/usr/bun/env bash

sudo apt install -y schroot mmdebstrap
sudo apt install -y --no-install-recommends sbuild autopkgtest
sudo apt install -y devscripts dput-ng

#	        corresponds to Debian 7 Wheezy	        20170617T034212Z
# Devuan 1.0 Jessie corresponds to Debian 8 Jessie	20180623T023722Z
# Devuan 2.0 ASCII corresponds to Debian 9 Stretch	20200717T204551Z
# Devuan 3.0 Beowulf corresponds to Debian 10.4 Buster	20230611T103552Z
# Devuan 4.0 Chimaera corresponds to Debian 11.1 Bullseye20230611T103552Z
# Devuan 5.0 Daedalus corresponds to Debian 12 Bookworm	20231007T150030Z
# Devuan 6.0 Excalibur corresponds to Debian 13 Trixie	testing
# Devuan 7.0 Freia corresponds to Debian 14 Forky
# Devuan     Ceres corresponds to Debian Sid	        unstable

UNSTABLE="ceres"
# TESTING="excalibur"
STABLE="daedalus"
OLD_STABLE="chimaera"

# distro="devuan"
# location="/srv/chroot"
# tarball="${release}-sbuild.tgz"
R="/space/code_repositories/decode-os/os-build-system/arm-sdk"
strapdir="$R/tmp/devuan-arm64-build/bootstrap"
arch="arm64"
release="daedalus"
mirror="http://deb.devuan.org/merged/" # apt url for debootsrap and dpks.
apt_proxy="http://192.168.1.49:3142"   # apt-cacher-ng proxy
devname="Cyteen"
devemail="cyteen@ring-zero.co.uk"
uploader="${devname} <${devemail}>"
schroot_config="arm-sdk"
sources_dir="$R/tmp/kernels/rockpro64" # where the sources outside the chroot are palced.
sources_dest="/scratch"                # where to mount the sources you wish to build in the chroot.


conf_print_debdevrc() {
  fn conf_print_debdevrc
  req=(${devname} ${devemail)
  ckreq || return 1

  cat <<-EOF
  export DEBFULLNAME="${devname}"
  export DEBEMAIL="${devemail}"
  export DEB_BUILD_OPTIONS="parallel=threads>"
  EOF
}
conf_print_debdevrc | sudo tee ${DEST}/.zshrc.d/001_debdev.zsh

# to use debsign on .changes
conf_print_devscripts() {
  fn conf_print_devscripts
  req=(${gpgkey})
  ckreq || return 1

  cat <<-EOF
    DEBSIGN_KEYID=${gpgkey}
    USCAN_SYMLINK=rename
  EOF
}
conf_print_devscripts |sudo tee ${DEST}/.devscripts

# to add experimental sources.list
conf_print_sbuildexperimental() {
  fn conf_print_sbuildexperimental 
  req=(${gpgkey})
  ckreq || return 1

  cat <<-EOF
    #!/bin/sh
    sbuild \
      --dist=experimental \
      --extra-repository='deb http://deb.debian.org/debian experimental main contrib non-free' \
      --build-dep-resolver=aspcud \
      $@
  EOF
}
conf_print_sbuildexperimental |sudo tee ${DEST}/.local/bin/sbuild-experimental
chmod +x ${DEST}/.local/bin/sbuild-experimental

# Create a symlink to an existing tarball
# # ln -s -r ${DEST}/.cache/sbuild/unstable-amd64.tar.xz ${DEST}/.cache/sbuild/experimental-amd64.tar.xz

conf_print_schroot() {
  fn conf_print_schroot
  req=(OLD_STABLE STABLE TESTING UNSTABLE)
  ckreq || return 1

  cat <<-EOF
		# schroot chroot definitions.
		# See schroot.conf(5) for complete documentation of the file format.
		#
		# Please take note that you should not add untrusted users to
		# root-groups, because they will essentially have full root access
		# to your system.  They will only have root access inside the chroot,
		# but that's enough to cause malicious damage.
		#
		# Per chroot configuration should be put in /etc/schroot/chroot.d/<name>
		# and this file reserved for default values for all chroots.
		#
		# This is mostly for sbuild created chroots who's creation time
		# settings are limited.
		#
		[*]
		type=directory
		union-type=overlay
		command-prefix=eatmydata,ccache
		users=default
		source-users=default
		script-config=/etc/schroot/default/config
		preserve-environment=true
	EOF
}
# conf_print_schroot | sudo tee /etc/schroot/schroot.conf >/dev/null

#-------------------------------------------------------------------------------
## sbuild configuration
# See: https://wiki.ubuntu.com/SimpleSbuild
# cp /usr/share/doc/sbuild/examples/example.sbuildrc ~/.sbuildrc
conf_print_sbuildrc() {
  fn conf_print_sbuildrc
  req=(release)
  ckreq || return 1

  # Backlup any existing configuration
  if [[ -f ~/.sbuildrc ]]; then
    mv ~/.sbuildrc ~/.sbuildrc-bak
  fi

  if [[ -f ~/.config/sbuild/config.pl ]]; then
    mv ~/.config/sbuild/config.pl ~/.config/sbuild/config.pl-bak
  fi

  cat <<-EOF
		# sbuild configuration file
		#
		# See sbuild.conf(5) for complete documentation of the file format.
    
		# Name to use as override in .changes files for the Maintainer: field
		# (mandatory, no default!).
		\$maintainer_name=${uploader};

    # backend for using mmdebstrap chroots
    \$chroot_mode = 'unshare';

    # build in tmpfs
    # For packages that take a lot of build space you might want to comment this
    # to prevent a OOM build failure.
    \$unshare_tmpdir_template = '/dev/shm/tmp.sbuild.XXXXXXXX';

    # upgrade before starting build
    \$apt_update = 1;
    \$apt_upgrade = 1;

		# Default distribution to build.
		# \$distribution = "${release}";

    # build everything including source for source-only uploads
    \$build_arch_all = 1;
    \$build_arch_any = 1;
    \$build_source = 1;
    \$source_only_changes = 1;

    # go to shell on failure instead of exiting
    \$external_commands = { "build-failed-commands" => [ [ '%SBUILD_SHELL' ] ] };

    # Switch these comments if you want to preserve even successful builds, and
    # then use "schroot -e --all-sessions" to clean them up manually.
    # always clean build dir, even on failure
    \$purge_build_directory = "always";
		# \$purge_build_directory = 'successful';
		# \$purge_session = 'successful';
		# \$purge_build_deps = 'successful';
		# \$purge_build_directory = 'never';
		# \$purge_session = 'never';
		# \$purge_build_deps = 'never';

    # run lintian
    \$run_lintian = 1;
    \$lintian_opts = [ '-i', '-I', '-E', '--pedantic' ];

    # do not run piuparts
    \$run_piuparts = 0;

    # run autopkgtest
    \$run_autopkgtest = 1;
    \$autopkgtest_root_args = '';
    \$autopkgtest_opts = [ '--apt-upgrade', '--', 'unshare', '--release', '%r', '--arch', '%a', '--prefix=/dev/shm/tmp.autopkgtest.' ];

    # set uploader for correct signing
    \$uploader_name = "${uploader}";

		# Directory for writing build logs to
		\$log_dir=\$ENV{HOME}."/devuan/logs";

    # Not required after perl 5.38
    1;
  EOF
}
conf_print_sbuildrc | sudo tee ~/.config/sbuild/config.pl >/dev/null

# Create ~/.mk-sbuild.rc:
conf_print_mk_sbuild_rc() {
  fn conf_print_mk_sbuild_rc
  req=(apt_proxy)
  ckreq || return 1

  cat <<-EOF
		SCHROOT_CONF_SUFFIX="source-root-users=root,sbuild,admin
		source-root-groups=root,sbuild,admin
		preserve-environment=true"
		# you will want to undo the below for stable releases, read 'man mk-sbuild' for details
		# during the development cycle, these pockets are not used, but will contain important
		# updates after each release of Ubuntu
		SKIP_UPDATES="1"
		SKIP_PROPOSED="1"
		# if you have e.g. apt-cacher-ng around
		# DEBOOTSTRAP_PROXY=${apt_proxy}
	EOF
}
# conf_print_mk-sbuild_rc | sudo tee ~/.mk-sbuild.rc >/dev/null

#-------------------------------------------------------------------------------
# Create the custom schroot config directory and contents.
mkdir -p /etc/schroot/${schroot_config}

conf_print_chroot_config() {
  fn conf_print_chroot_config
  req=(schoot_config sources_dir sources_dest)
  ckreq || return 1

  cat <<-EOF | sudo tee /etc/schroot/${schroot_config}/copyfiles
		# Files to copy into the chroot from the host system.
		#
		# <source-and-destination>[ <destination>]
		/etc/resolv.conf
		/etc/hosts
		/etc/sudoers
		~/.Xauthority
	EOF

  cat <<-EOF | sudo tee /etc/schroot/${schroot_config}/fstab
		# fstab: static file system information for chroots.
		# Note that the mount point will be prefixed by the chroot path
		# (CHROOT_PATH)
		#
		# <file system> <mount point>   <type>  <options>       <dump>  <pass>
		/proc           /proc           none    rw,bind         0       0
		/sys            /sys            none    rw,bind         0       0
		/dev            /dev            none    rw,bind         0       0
		/dev/pts        /dev/pts        none    rw,bind         0       0
		${sources_dir}  ${sources_dest} none    rw,bind         0       0
		/tmp            /tmp            none    rw,bind         0       0

		# It may be desirable to have access to /run, especially if you wish
		# to run additional services in the chroot.  However, note that this
		# may potentially cause undesirable behaviour on upgrades, such as
		# killing services on the host.
		#/run           /run            none    rw,bind         0       0
		#/run/lock      /run/lock       none    rw,bind         0       0
		#/dev/shm       /dev/shm        none    rw,bind         0       0
		#/run/shm       /run/shm        none    rw,bind         0       0
	EOF

  cat <<-EOF | sudo tee /etc/schroot/${schroot_config}/nssdatabases
		# System databases to copy into the chroot from the host system.
		#
		# <database name>
		passwd
		shadow
		group
		gshadow
		services
		protocols
		networks
		hosts
	EOF

  cat <<-EOF | sudo tee /etc/schroot/${schroot_config}/config
		# Filesystems to mount inside the chroot.
		FSTAB="/etc/schroot/custom/fstab"

		# Files to copy from the host system into the chroot.
		COPYFILES="/etc/schroot/custom/copyfiles"

		# System NSS databases to copy into the chroot.
		NSSDATABASES="/etc/schroot/custom/nssdatabases"
	EOF
}
#-------------------------------------------------------------------------------

# sudo sbuild-createchroot \
# 	--arch=${arch} \
# 	--make-sbuild-tarball=${location}/${tarball} \
# 	${release} \
# 	${location}/${release} \
# 	${mirror}

# this is the file created automatically by the create line above.
# cat <<-EOF | sudo tee /etc/schroot/chroot.d/daedalus-arm64-sbuild-lNGdIk
#	[daedalus-arm64-sbuild]
#	description=Debian daedalus/arm64 autobuilder
#	groups=root,sbuild
#	root-groups=root,sbuild
#	profile=sbuild
#	type=file
#	file=/srv/chroot/daedalus-sbuild.tgz
# EOF
#
# cat <<-EOF | sudo tee -a /etc/schroot/schroot.d/${release}_${arch}
#	[${release}-file]
#	description=${distro} ${release} (custom)
#	file=/srv/chroot/${release}.tar.gz
#	location=/${location}
#	groups=sbuild
# EOF

conf_print_chroot_d() {
  fn conf_print_chroot_d
  req=(strapdir arch release schroot_config)
  ckreq || return 1

  cat <<-EOF | sudo tee -a /etc/schroot/schroot.d/${release}_${arch}
		[${release}_${arch}]
		description=${release} (${arch}) for devuan arm-sdk
		type=directory
		directory=${strapdir}
		union-type=overlay  # 'aufs', 'overlay'  and 'unionfs'
		command-prefix=eatmydata,ccache
		users=default
		source-users=default
		script-config=${schroot_config}/config
		preserve-environment=true
	EOF
}
# conf_print_chroot_d | sudo tee /etc/schroot/schroot.d/${release}_${arch} >/dev/null

# zfs snapshot examples
zfs_snapshot="rpool/CHROOT/sid"
sudo zfs snapshot ${zfs_snapshot}@initial
sudo zfs create ${zfs_snapshot}

sudo sbuild-createchroot \
  --arch=${arch} \
  --include=debhelper,eatmydata,ccache \
  --chroot-mode=schroot \
  --components=main contrib non-free \
  ${release} \
  ${zfs_snapshot} \
  ${mirror}

# cat <<-EOF | sudo tee /etc/schroot/chroot.d/sid-zfs-snapshot
#	[sid-zfs-snapshot]
#	description=Devuan sid ZFS snapshot
#	type=zfs-snapshot
#	zfs-dataset=rpool/CHROOT/sid
#	zfs-snapshot=initial
#	groups=root,sbuild
#	root-groups=root,sbuild
# EOF
# schroot -c sid-zfs-snapshot

# [${UNSTABLE}-znap]
# type=zfs-snapshot
# description=Devuan ${UNSTABLE} ZFS snapshot
# groups=sbuild,root
# root-users=rleigh
# root-groups=root,sbuild
# source-root-users=rleigh
# zfs-dataset=rpool/CHROOT/${UNSTABLE}
# mount-options=-o atime,sync,user_xattr
# zfs-snapshot-options=-o checksum=off
