#!/bin/bash

set -e

DATE=$(date '+%Y-%m-%d_%H-%M-%S')
GREEN='\e[1;32m'
YELLOW='\e[0;33m'
RED='\e[1;31m'
RESET_COLOR='\033[0m'

help() {
	echo -e "\e[1;32mKaisen Linux ISO build system

\e[0;33mUSAGE
	\e[1;35mbuild\e[0;33m: \e[1;36msudo ./kaisen-build \e[1;35m-v\e[0;33m \e[1;36mVARIANT \e[1;35m-r\e[0;33m \e[1;36mRELEASENUMBER
	\e[1;35mclean\e[0;33m: \e[1;36msudo ./kaisen-build \e[1;35m-c
	\e[1;35mhelp\e[0;33m: \e[1;36msudo ./kaisen-build \e[1;35m-h

\e[0;33mEXPLAINATION
		Arguments can be used with this script:
		\e[1;35m-h\e[0;33m|\e[1;35m--help\e[0;33m: display help
		\e[1;35m-c\e[0;33m|\e[1;35m--clean\e[0;33m: Completely remove files and confs generated by kaisen-build
		\e[1;35m-r\e[0;33m|\e[1;35m--release\e[0;33m: Select your release number, example \e[1;36m2.3
		\e[1;35m-v\e[0;33m|\e[1;35m--variant\e[0;33m: \e[1;36mMATE\e[0;33m, \e[1;36mXFCE\e[0;33m, \e[1;36mKDE\e[0;33m, \e[1;36mLXQT\e[0;33m, \e[1;36mSR\e[0;33m, \e[1;36mNETINST\e[0;33m, \e[1;36mCUSTOM\e[0;33m
			   The graphical interface to use (\e[1;36mMATE\e[0;33m, \e[1;36mXFCE\e[0;33m, \e[1;36mKDE\e[0;33m or \e[1;36mLXQT\e[0;33m)
			   Or use System Rescue release (Only technicians tools, with or not GUI, \e[1;36mSR\e[0;33m variant)
			   Or use NETwork INSTaller (no live, \e[1;36mNETINST\e[0;33m variant)
			   Or use your own configurations (\e[1;36mCUSTOM\e[0;33m variant)

EXAMPLE
	To build an ISO with the short options:
	\e[1;36msudo ./kaisen-build \e[1;35m-v \e[1;36mMATE \e[1;35m-r \e[1;36m2.3\e[0;33m
	To build an ISO with the long options:
	\e[1;36msudo ./kaisen-build \e[1;35m--variant \e[1;36mMATE \e[1;35m--release \e[1;36m2.3\e[0;33m
	To build an ISO with the long options with your own configurations:
	\e[1;36msudo ./kaisen-build \e[1;35m--variant \e[1;36mCUSTOM \e[1;35m--release \e[1;36m0.1\e[0;33m

DOCUMENTATION
	\e[1;32mhttps://kaisenlinux.org/documentation/how-to-build-custom-iso.html\e[0;33m
"
exit 0
}

clean() {
[ -x auto/clean ] && ./auto/clean
}

ARGS=$(getopt -o r:v:hc -l release:,variant:,help,clean -- "$@")
eval set -- "$ARGS"
while true
do
        case "$1" in
                -h|--help) help; ;;
                -c|--clean) clean;exit 0; ;;
                -r|--release) RELEASE=$2; shift 2; ;;
                -v|--variant) VARIANT=$2; shift 2; ;;
                --) shift; break; ;;
                 *) echo -e $RED "Internal error, unhandled option: $1"; exit 1; ;;
        esac
done

if [[ "$EUID" -ne 0 ]]; then
        echo -e $RED "Run this script as root or with the sudo command."$RESET_COLOR
        exit 1
fi

if [ ! -e variant/$VARIANT ] && [ ! -d variant/$VARIANT ]; then
        echo -e $RED "The variant $VARIANT does not exist nor is a folder in the variant directory."$RESET_COLOR
        exit 1
fi

if [ -z $VARIANT ]; then
	VARIANT="KDE"
fi

if [ -z $RELEASE ]; then
        RELEASE="NULL"
fi

export VARIANT
export RELEASE

confs() {
sed -i "s/Kaisen Linux Rolling Live/Kaisen Linux Rolling $VARIANT $RELEASE Live/g" common/includes.binary/boot/grub/grub.cfg
sed -i "s/Kaisen Linux Rolling Install/Kaisen Linux Rolling $VARIANT $RELEASE Install/g" common/includes.binary/boot/grub/grub.cfg
sed -i "s/Kaisen Linux/Kaisen Linux $VARIANT $RELEASE/g" common/includes.binary/.disk/info
sed -i "0,/Kaisen Linux Rolling/ s/Kaisen Linux Rolling/Kaisen Linux Rolling $VARIANT $RELEASE/" common/includes.binary/isolinux/menu.cfg
sed -i "s/Kaisen Linux NETINST/Kaisen Linux NETINST "$RELEASE"/g" variant/NETINST/simple-cdd.conf
sed -i "s/Kaisen Linux Rolling NETINST/Kaisen Linux Rolling NETINST "$RELEASE"/g" variant/NETINST/simple-cdd.conf variant/NETINST/hook
}

case $VARIANT in
	KDE|LXQT|MATE|XFCE|SR)
		clean
		confs
		lb build
		mv live-image-amd64.hybrid.iso ../kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso
		echo -e $GREEN "ISO can be found in ../kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso"$RESET_COLOR
		echo -e $YELLOW "Generating torrent file..."$RESET_COLOR
		mktorrent -p -a "udp://torrent.kaisenlinux.org:6969/announce" -a "http://torrent.kaisenlinux.org:6969/announce" -w "https://iso.kaisenlinux.org/rolling/kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso" -c "Official Kaisen Linux $RELEASE $VARIANT ISO. Built by the Kaisen Linux team." ../kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso
		mv kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso.torrent ../
		echo -e $GREEN "Torrent generation complete. Can be found in ../kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso.torrent"$RESET_COLOR
		clean
	;;
esac

case $VARIANT in
	CUSTOM)
		clean
		confs
		lb build
		mv live-image-amd64.hybrid.iso ../kaisenlinuxCUSTOM-amd64-$RELEASE.iso
		echo -e $YELLOW "ISO can be found in ../kaisenlinuxCUSTOM-amd64-$RELEASE.iso"$RESET_COLOR
		clean
	;;
esac

case $VARIANT in
	NETINST)
		clean
		confs
		build-simple-cdd \
		--profiles kaisen \
		--auto-profiles kaisen \
		--verbose \
		--debug \
		--force-root \
		--conf variant/NETINST/simple-cdd.conf \
		--dist kaisen-rolling \
		--debian-mirror https://deb.kaisenlinux.org
		mv images/kaisen-Rolling-amd64-NETINST-1.iso ../kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso
		echo -e $GREEN "ISO can be found in ../kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso"$RESET_COLOR
		echo -e $YELLOW "Generating torrent file..."$RESET_COLOR
		mktorrent -p -a "udp://torrent.kaisenlinux.org:6969/announce" -a "http://torrent.kaisenlinux.org:6969/announce" -w "https://iso.kaisenlinux.org/rolling/kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso" -c "Official Kaisen Linux $RELEASE $VARIANT ISO. Built by the Kaisen Linux team." ../kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso
		mv kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso.torrent ../
		echo -e $GREEN "Torrent generation complete. Can be found in ../kaisenlinuxrolling$RELEASE-amd64-$VARIANT.iso.torrent"$RESET_COLOR
		clean
	;;
esac
