#!/bin/bash

baseDir=$(pwd)

build=$baseDir/build

config=$baseDir/config

iso=$baseDir/iso

if [ "$EUID" -ne 0 ]
then 
	echo "Please run as root"
	exit 1
fi

debian_version="bullseye"

type_list=(full xfce cinnamon vmxfce vmfull)

pack_full=(live cli disk net firmware xfce4 cinnamon dm-common gui gui_opt)

pack_xfce=(live cli disk net firmware xfce4 dm-common gui)

pack_cinnamon=(live cli disk net firmware cinnamon dm-common gui)

pack_vmxfce=(live cli disk net firmware xfce4 dm-common gui vm)

pack_vmfull=(live cli disk net firmware xfce4 cinnamon dm-common gui vm)

_usage()
{
	cat <<EOF
-t Specifie type in list: $(printf "\e[96m${type_list[*]}\e[0m\n")
-v Specifie Debian codename
EOF
}

if [ -z "$1" ]
then
	_usage
	exit 1
fi

#export http_proxy="http://192.168.110.1:3142"

KERNEL_PARAM="boot=live components lang=fr_FR.UTF-8 locales=fr_FR.UTF-8 keyboard-layouts=fr keyboard-model=pc105 timezone=Europe/Paris utc=yes"

_config()
{

	mkdir -p $build/config

	cd $build

	ln -sr $config/includes.chroot $build/config
	ln -sr $config/packages.chroot $build/config

	mkdir -p $build/config/hooks
	ln -sr $config/hooks/live $build/config/hooks

	lb clean
	lb clean --cache

	#--apt-http-proxy "http://192.168.110.1:3142" \
	lb config noauto \
	--architectures amd64 \
	--distribution $debian_version \
	--binary-images iso-hybrid \
	--archive-areas "main contrib non-free" \
	--debian-installer none \
	--apt-recommends false \
	--updates true \
	--security true \
	--source false \
	--backports false \
	--memtest none \
	--bootappend-live "$KERNEL_PARAM"

	rm $build/config/package-lists/*

	eval pack=\${pack_${type}[@]}

	for file in ${pack[@]}; do
		ln -sr $config/package-lists/$file.list.chroot $build/config/package-lists/$file.list.chroot
	done

	if [ ! -f "$iso" ];then
		printf "\e[96mCreate ISO directory\e[0m\n"
		install -d -m 0755 -o $SUDO_UID -g $SUDO_GID $iso
	fi

	out="$iso/$type"

	install -d -m 0755 -o $SUDO_UID -g $SUDO_GID $out

}

_build()
{
	
	printf "\e[96mBootstrap new root filesystem\e[0m\n"
	lb bootstrap

	printf "\e[96mBuild live OS file system\e[0m\n"
	lb chroot

	# Write commit hash and build date
	echo "$(git -C $baseDir log -1 --pretty='%cd %h %s' --date=format:"%F")" >> $build/chroot/etc/debian_version

}

_create_squashfs()
{

	printf "\e[96mPreparing root filesystem\e[0m\n"
	lb binary_chroot

	# Chroot preparation component lists
	# We deliberately exclude 'debianchroot'
	CHROOT_PREP_MOUNTS="devpts proc selinuxfs sysfs"
	CHROOT_PREP_OTHER="dpkg tmpfs sysv-rc hosts resolv hostname apt"

	# Configuring chroot
	lb chroot_prep install "${CHROOT_PREP_MOUNTS}"
	lb chroot_prep install "${CHROOT_PREP_OTHER}" mode-apt-install-binary mode-archives-chroot
	lb chroot_archives chroot install

	printf "\e[96mBuilding root filesystem\e[0m\n"
	lb binary_rootfs

	printf "\e[96mPrepare images\e[0m\n"
	lb binary_linux-image
	lb binary_syslinux

	# Deconfiguring chroot
	lb chroot_prep remove "${CHROOT_PREP_MOUNTS}"
	lb chroot_prep remove "${CHROOT_PREP_OTHER}"

	printf "\e[96mCopy filesystem\e[0m\n"

	cp $build/binary/live/{initrd.img,vmlinuz,filesystem.squashfs} $out/

	chown $SUDO_UID:$SUDO_GID $out/{initrd.img,vmlinuz,filesystem.squashfs}

}

_create_iso()
{

	printf "\e[96mBuild binary (live disc) images\e[0m\n"
	lb binary

	printf "\e[96mCopy ISO\e[0m\n"

	cp $build/live-image-amd64.hybrid.iso $out/live-${type}-image.iso

	chown $SUDO_UID:$SUDO_GID $out/live-${type}-image.iso

}
_delete()
{
	printf "\e[96mDelete $build\e[0m\n"
	rm -r $build
}

while getopts "t:v:" OPTION; do
	case $OPTION in
	t)
		type=${OPTARG}
		;;
	v)
		debian_version=${OPTARG}
		;;
	*)
		_usage
		exit 1
		;;
	esac
done

if ! [[  "${type_list[*]}" =~ "$type" ]]
then
	printf "Cannot proceed \e[96m$type\e[0m\n\n"
	_usage
	exit
fi

printf "\e[96mMake $type\e[0m\n"
printf "\e[96mDebian version of live system: $debian_version\e[0m\n"

read -p "Continue ? " -n 1 -r
echo
if ! [[ $REPLY =~ ^[YyOo]$ ]]
then
	echo "Cancel"
	exit
fi

case $type in
"full")
	_config
	_build
	_create_squashfs
	_delete
	;;
"xfce")
	_config
	_build
	mkdir -p $build/chroot/etc/skel/.ssh
	cp -v /home/lionel/.ssh/{config,key_rsa_majalis,key_rsa_majalis.pub} $build/chroot/etc/skel/.ssh
	sudo -u $SUDO_USER ssh-keygen -H -F netdldata.net > $build/chroot/etc/skel/.ssh/known_hosts
	_create_squashfs
	_delete
	;;
"vmxfce")
	_config
	_build
	_create_iso
	_delete
	;;
"vmfull")
	_config
	_build
	_create_iso
	_delete
	;;
"cinnamon")
	_config
	_build
	_create_iso
	_delete
	;;
*)
	_usage
	exit 1
	;;
esac

