#!/bin/sh

# SPDX-FileCopyrightText: (C) 2025 Enno Tensing <tenno+containerctl@suij.in>
#
# SPDX-License-Identifier: GPL-2.0-only

BASEDIR="/var/lib/containerctl"
CONTAINERDIR="${BASEDIR}/containers"
CONFIGDIR="${BASEDIR}/configs"
LOGDIR="/var/log/containerctl"
TODAY="$(date '+%F')"
LOG="${LOGDIR}/${TODAY}"

MINIMUM_PYTHON3_VERSION='11'
MAXIMUM_PYTHON3_VERSION='13'

get_python_path()
{
	py="python3"
	pyver="$(/usr/bin/env "${py}" -c 'import sys; print(sys.version_info.minor)')"
	if [ "${pyver}" -lt "${MINIMUM_PYTHON3_VERSION}" ]
	then
		pyver="${MAXIMUM_PYTHON3_VERSION}"
		py="python3.${pyver}"
	else
		printf '%b' "${py}"
		return
	fi

	while [ "${pyver}" -ge "${MINIMUM_PYTHON3_VERSION}" ]
	do
		if /usr/bin/env "${py}" -c "print('${py}')" 2> /dev/null
		then
			return
		fi
		pyver=$((pyver - 1))
		py="python3.${pyver}"
	done

	log_error "containerctl needs at least Python 3.${MINIMUM_PYTHON3_VERSION} to run!"
	exit 1
}

log_error()
{
	printf '[%b] (EE) %b\n' "${TODAY}" "${@}" | tee -a "${LOG}" 2> /dev/null
}

list_containers()
{
	find "${CONTAINERDIR}" -mindepth 1 -type d
}

list_configs()
{
	find "${CONFIGDIR}" -mindepth 1 -type f
}

exec_script()
{
	container="${1}"
	script="${2}"

	if [ -z "${container}" ]
	then
		log_error "No container passed"
		exit 1
	fi

	if [ -z "${script}" ]
	then
		log_error "No script passed"
		exit 1
	fi

	if ! [ -e "${CONTAINERDIR}/${container}" ]
	then
		log_error "${container} does not exist, did you forget to generate it?"
		exit 1
	fi

	if ! [ -d "${CONTAINERDIR}/${container}" ]
	then
		log_error "${container} does not exist"
		exit 1
	fi

	if ! [ -e "${CONTAINERDIR}/${container}/${script}" ]
	then
		log_error "${script} is invalid, maybe try regenerating the container?"
		exit 1
	fi

	if ! [ -f "${CONTAINERDIR}/${container}/${script}" ]
	then
		log_error "${script} is invalid, please recreate the container"
		exit 1
	fi

	/bin/sh "${CONTAINERDIR}/${container}/${script}" || log_error "${script} failed!"
}


generate_container()
{
	config="${1}"

	if [ -z "${config}" ]
	then
		log_error "No config was passed."
		exit 1
	fi

	if ! [ -e "${CONFIGDIR}" ]
	then
		log_error "${CONFIGDIR} does not exist"
		exit 1
	fi

	if ! [ -d "${CONFIGDIR}" ]
	then
		log_error "${CONFIGDIR} is not a directory"
		exit 1
	fi

	if ! [ -e "${CONFIGDIR}/${config}" ]
	then
		log_error "${config} does not exist!" \
			"Please check the name and or location  of the desired config."
		exit 1
	fi

	if ! [ -f "${CONFIGDIR}/${config}" ]
	then
		log_error "${config} is not a file."
		exit 1
	fi

	mypython="$(get_python_path)"
	/usr/bin/env "${mypython}" "${BASEDIR}/generate/generate.py" \
		"${CONFIGDIR}/${config}" "${LOG}" "${CONTAINERDIR}"

	if [ "${?}" = 1 ]
	then
		log_error "Container generation failed."
		exit 1
	fi
}

generate_all()
{
	list_configs | while read -r config
	do
		generate_container "$(printf '%b' "${config}" | sed -e "s|${CONFIGDIR}||g")"
	done
}

usage()
{
	printf '%b list-containers|list-configs|generate CONFIG-FILE|generate-all|CONTAINER-NAME ACTION\n' "${0}"
}

case "${1}" in
	"list-containers") list_containers ;;
	"list-configs") list_configs ;;
	"generate-all") generate_all ;;
	"generate") shift; generate_container "${@}" ;;
	"help") usage "${0}" ;;
	"usage") usage "${0}" ;;
	*) exec_script "${@}" ;;
esac
