#! /usr/bin/env bash

TOOLBOXALMA="quay.io/toolbx-images/almalinux-toolbox"
TOOLBOXALPINE="quay.io/toolbx-images/alpine-toolbox"
TOOLBOXARCH="quay.io/toolbx/arch-toolbox"
TOOLBOXCENTOS="quay.io/toolbx-images/centos-toolbox"
TOOLBOXDEBIAN="quay.io/toolbx-images/debian-toolbox"
TOOLBOXFEDORA="quay.io/fedora/fedora-toolbox"
TOOLBOXGENTOO="docker.io/gentoo/stage3"
TOOLBOXOPENSUSE="quay.io/toolbx-images/opensuse-toolbox"
TOOLBOXRHEL="registry.access.redhat.com"
TOOLBOXUBUNTU="quay.io/toolbx/ubuntu-toolbox"


function f_help {
	echo ""
	echo "Usage:"
	echo " $0 [-d] [-r] [-l] [-h] toolbox-name"
	echo ""
	echo "Tool to create toolbox for unsupported OS in toolbox project."
	echo ""
	echo "Options :"
	echo "  -d		Distro"
	echo "  -r		Release"
	echo "  -l		List distros available"
	echo "  -h		Help"
	echo "  toolbox-name	Name of your toolbox (instead of distro-toolbox-release)"
}

function f_list {
	echo "Supported distros :"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "+   \033[1m distro \033[0m   +                \033[1m release version \033[0m               +             \033[1m infos \033[0m             +"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| alma         | 8 9 or latest                                  | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| alpine       | 3.19 3.20 edge or latest                       | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| arch         | latest                                         | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| centos       | stream8 stream9 or latest                      | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| debian       | 10 11 12 testing unstable or latest            | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| fedora       | 39 40 41 rawhide or latest                     | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| gentoo       | latest systemd                                 | via docker container            |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| opensuse     | tumbleweed                                     | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| rhel         | 8.10 9.3 9.4 ... (<major>.<minor>' format)     | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
	echo -e "| ubuntu       | 22.04 24.04 ... (YY.MM format)                 | official toolbox                |"
	echo -e "+--------------+------------------------------------------------+---------------------------------+"
}


while getopts ":d:r:lh" option; do
	case $option in
		d)
			option_d=true
			val_d=$OPTARG
		;;
		r)
			option_r=true
			val_r=$OPTARG
		;;
		l)
			if [[ -n $option_d || -n $option_r ]]
			then
				echo "-l cannot be used with -d or -r"
				exit 1
			fi
			option_l=true
		;;
		h)
			f_help
			exit 0
		;;
		\?)
			echo "Invalid option: -$OPTARG" >&2
			f_help
			exit 1
		;;
		:)
			echo "-$OPTARG needs value" >&2
			f_help
			exit 1
		;;
	esac
done

shift $((OPTIND - 1))
TOOLBOXNAME="$1"

if [[ $option_d == true && $option_r == true ]]
then
	TOOLBOXDISTRO=$val_d
	TOOLBOXRELEASE=$val_r
	case $TOOLBOXDISTRO in
		alma)
			toolbox create --image "$TOOLBOXALMA:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		alpine)
			toolbox create --image "$TOOLBOXALPINE:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		arch)
			toolbox create --image "$TOOLBOXARCH:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		centos)
			toolbox create --image "$TOOLBOXCENTOS:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		debian)
			toolbox create --image "$TOOLBOXDEBIAN:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		fedora)
			toolbox create --image "$TOOLBOXFEDORA:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		gentoo)
			toolbox create --image "$TOOLBOXGENTOO:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		opensuse)
			toolbox create --image "$TOOLBOXOPENSUSE:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		rhel)
			toolbox create --image "$TOOLBOXRHEL/ubi${TOOLBOXRELEASE%%.*}/toolbox:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		ubuntu)
			toolbox create --image "$TOOLBOXUBUNTU:$TOOLBOXRELEASE" "$TOOLBOXNAME"
			;;
		*)
			echo "Unknown distro : $TOOLBOXDISTRO"
			f_list
			exit 1
			;;
	esac

elif [[ $option_d == true || $option_r == true ]]
then	
	echo "You must specify a distro (-d) AND a release version (-r)"
elif [[ $option_l == true ]]
then
	f_list
else
	# No opt
	f_help
fi
