#!/bin/bash

baseDir=/media/Accensio/live

build=$baseDir/build

config=$baseDir/config

iso=$baseDir/iso

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

_usage()
{
	cat <<"EOF"
-f Make full live image
-x Make xfce live image
-c Make cinnamon live image
-v Make xfce-vm live image
EOF
}

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

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"

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)

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

_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 bullseye \
	--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

}

_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 "fxcv" OPTION; do
	case $OPTION in
	f)
		type="full"
		printf "\e[96mMake $type\e[0m\n"
		_config
		_build
		_create_squashfs
		_delete
		;;
	x)
		type="xfce"
		printf "\e[96mMake $type\e[0m\n"
		_config
		_build
		mkdir -p $build/chroot/etc/skel/.ssh
		cp /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
		;;
	v)
		type="vmxfce"
		printf "\e[96mMake $type\e[0m\n"
		_config
		_build
		_create_iso
		_delete
		;;
	c)
		type="cinnamon"
		printf "\e[96mMake $type\e[0m\n"
		_config
		_build
		_create_iso
		_delete
		;;
	*)
		_usage
		exit 1
		;;
	esac
done

if [ -z "$type" ]
then
	_usage
fi
