#!/bin/bash
set -e

# install-debian.sh - installs debian in a chroot
# ed <irc.rizon.net>, MIT-licensed, 2020-06-21, v1.0
# https://ocv.me/dev/?install-debian.sh

rls=testing
rls=stable

aname=amd64
croot=/c/deb-$aname-$rls

msg() { printf '\033[1;30m>\033[0;34m>\033[1m>\033[0m %s\n' "$1" >&2; }
inf() { printf '\n\033[1;30m>\033[0;32m>\033[1m>\033[0;32m %s\033[0m\n' "$1" >&2; }
wrn() { printf '\n\033[1;30m>\033[0;33m>\033[1m>\033[0;33m %s\033[0m\n' "$1" >&2; }
err() { printf '\n\033[1;30m>\033[0;31m>\033[1m>\033[0;31m %s\033[0m\n' "$1" >&2; }

[ $(id -u) -eq 0 ] || {
	err "need root"
	exit 1
}


inf "creating chroot $croot"
command -v apk && apk add debootstrap debian-archive-keyring
command -v pacman && pacman -S --needed debootstrap debian-archive-keyring
mkdir -p /var/cache/debootstrap
debootstrap --force-check-gpg --arch=$aname --cache-dir=/var/cache/debootstrap $rls "$croot" https://ftp.acc.umu.se/debian/



inf "writing chroot scripts"

sed -r "s@chrootdir@$croot@g" > "$croot"/activate <<'EOF'
set -e
cd "chrootdir"

# block internet traffic for user 1984
iptables -I OUTPUT 1 -m owner --uid-owner 1984 -j DROP || true

# sync dbus id
[ -e "chrootdir"/var/lib/dbus/ ] &&
	cat /var/lib/dbus/machine-id > "chrootdir"/var/lib/dbus/machine-id || true

# indirect pulseaudio
# sudo -u ed pactl load-module module-native-protocol-unix auth-anonymous=1 socket=/tmp/pulse-socket || true

# sync dns
cp /etc/resolv.conf etc

mount |
grep -qF " on chrootdir" || {
	mount --rbind /dev  dev
	mount --make-rslave dev
	mount -t proc /proc proc
	mount --rbind /sys  sys
	mount --make-rslave sys
	mount --rbind /tmp  tmp
	
	# optional: home-dirs and pulseaudio compat
	mount --rbind /run  run
	for d in /home/*; do
		mkdir -p "chrootdir/$d"
		mount --rbind "$d" "chrootdir/$d"
	done
}

rv=0
[ -z "$*" ] && { chroot "chrootdir" /bin/bash || rv=$?; }
[ -z "$*" ] || { chroot "chrootdir" "$@"      || rv=$?; }

find /proc/ -mindepth 2 -maxdepth 2 -name exe -printf '%l\n' 2>/dev/null |
grep -qE '^chrootdir' &&
	printf '\033[33mother shells using the chroot; will not deactivate\033[0m\n' >&2 ||
	"chrootdir"/opt/deactivate

[ $rv -eq 0 ] || {
	printf "\033[1;31m\n[!] left chroot with exit code $rv\033[0m\n\n" >&2
	exit $rv
}
EOF

sed -r "s@chrootdir@$croot@g" > "$croot"/opt/deactivate <<'EOF'
deactivate()
{
	printf '\033[33munmounting chroot stuff\033[0m\n'
	mount |
	grep -E "^[^ ]+ on chrootdir(/| )" |
	awk '{print $3}' |
	tee /dev/stderr |
	xargs umount 
}

deactivate ||
deactivate ||
deactivate ||
echo "giving up, just reboot"
EOF

chmod 755 "$croot"/{activate,opt/deactivate}


inf "installing packages"
TERM=xterm "$croot/activate" <<EOF
set -e
/sbin/useradd -m -p k -s /bin/bash -u 1000 ed
/sbin/useradd -m -p k -s /bin/bash -u 1984 nope
printf 'h\nh\n' | passwd ed

cat >/etc/apt/sources.list <<'EOF2'
deb     https://ftp.acc.umu.se/debian $rls main contrib non-free
deb-src https://ftp.acc.umu.se/debian $rls main contrib non-free
EOF2

echo $rls | grep -qE 'testing|unstable' || cat >>/etc/apt/sources.list <<'EOF2'
deb     http://deb.debian.org/debian-security/ $rls/updates main
deb-src http://deb.debian.org/debian-security/ $rls/updates main
deb     https://ftp.acc.umu.se/debian          $rls-updates main
deb-src https://ftp.acc.umu.se/debian          $rls-updates main
EOF2

apt update
apt -y dist-upgrade
apt -y install pixz file libncurses5

exit 0
find /var/cache/apt/archives -name '*.deb' -delete
rm /var/cache/apt/* || true
rm /var/lib/apt/lists/*debian_dists_$rls*
EOF



inf "done; run $croot/activate"
