#!/bin/sh
#
# Copyright (c) 2022-2023, Jesús Daniel Colmenares Oviedo <DtxdF@disroot.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

lib_load "${LIBDIR}/check_func"
lib_load "${LIBDIR}/colors"
lib_load "${LIBDIR}/jail"
lib_load "${LIBDIR}/jail_types"
lib_load "${LIBDIR}/random"

update_desc="Update a thick jail or a release."

update_main()
{
	local command="$1"; shift
	if lib_check_empty "${command}"; then
		update_usage
		exit ${EX_USAGE}
	fi

	case "${command}" in
		jail|release) ;;
		*) update_usage; exit ${EX_USAGE} ;;
	esac

	update_${command} "$@"
}

update_jail()
{
	local _o
	local opt_build=0
	local opt_force=0
	local opt_cond_kernel=1
	local opt_kernel=0
	local jobs="${JOBS}"

	while getopts ":bfKkj:" _o; do
		case "${_o}" in
			j)
				if lib_check_empty "${OPTARG}"; then
					update_usage
					exit ${EX_USAGE}
				fi
				;;
		esac

		case "${_o}" in
			b)
				opt_build=1
				;;
			f)
				opt_force=1
				;;
			K)
				opt_cond_kernel=0
				;;
			k)
				opt_kernel=1
				;;
			j)
				jobs="${OPTARG}"
				;;
			*)
				update_usage
				exit ${EX_USAGE}
				;;
		esac
	done
	shift $((OPTIND-1))

	local jail_name="$1"; shift
	if lib_check_empty "${jail_name}"; then
		update_usage
		exit ${EX_USAGE}
	fi

	if ! lib_check_jailname "${jail_name}"; then
		lib_err ${EX_DATAERR} "Invalid jail name \"${jail_name}\""
	fi

	lib_set_logprefix " [`random_color`${jail_name}${COLOR_DEFAULT}]"

	local jail_path="${JAILDIR}/${jail_name}"
	local basedir="${jail_path}/jail"
	if [ ! -d "${basedir}" ]; then
		lib_err ${EX_NOINPUT} "Cannot find the jail \`${jail_name}\`."
	fi

	local jail_type
	jail_type=`lib_ajconf get -Vnt "${jail_path}/conf/config.conf" jail_type` || return $?

	if [ "${jail_type}" != "${JAIL_TYPE_THICK}" ]; then
		lib_err ${EX_NOPERM} "${jail_name} is not a thickjail."
	fi

	local osarch
	osarch=`lib_ajconf get -Vnt "${jail_path}/conf/config.conf" osarch` || return $?

	if lib_check_empty "${osarch}"; then
		lib_err ${EX_CONFIG} -- "osarch is empty or not defined in the configuration file!"
	fi

	local osversion
	osversion=`lib_ajconf get -Vnt "${jail_path}/conf/config.conf" osversion` || return $?

	if lib_check_empty "${osversion}"; then
		lib_err ${EX_CONFIG} -- "osversion is empty or not defined in the configuration file!"
	fi

	local release_name
	release_name=`lib_ajconf get -Vnt "${jail_path}/conf/config.conf" release_name` || return $?

	if lib_check_empty "${release_name}"; then
		lib_err ${EX_CONFIG} -- "release_name is empty or not defined in the configuration file!"
	fi

	local releasedir="${RELEASEDIR}/${osarch}/${osversion}/${release_name}"

	if [ -f "${releasedir}/.empty" ]; then
		lib_err ${EX_NOPERM} "This jail uses an empty release, so you cannot update it."
	elif [ -f "${releasedir}/.from_src" ]; then
		if [ ${opt_cond_kernel} -eq 1 -a -f "${releasedir}/.done_installkernel" ]; then
			opt_kernel=1
		fi

		local logname
		logname=`sh -c "${BUILDLOG_NAME}"`

		errlevel=$?
		if [ ${errlevel} -ne 0 ]; then
			lib_err ${errlevel} "{BUILDLOG_NAME} exits with a non-zero exit status."
		fi

		if lib_check_ispath "${logname}"; then
			lib_err ${EX_DATAERR} -- "${logname}: invalid log name."
		fi

		lib_zfs_mklogdir "jails" "${jail_name}" "build"

		local build_name="jails/${jail_name}/build/${logname}"
		local buildlog="${LOGDIR}/${build_name}"

		lib_info "Build log will be ${build_name}"

		if [ ! -f "${releasedir}/.target" ]; then
			lib_err ${EX_NOINPUT} -- "${releasedir}/.target: File does not exist."
		fi

		if [ ! -f "${releasedir}/.kernconf" ]; then
			lib_err ${EX_NOINPUT} -- "${releasedir}/.kernconf: File does not exist."
		fi

		if [ ! -f "${releasedir}/.srcdir" ]; then
			lib_err ${EX_NOINPUT} -- "${releasedir}/.srcdir: File does not exist."
		fi

		local target target_arch kernel source_tree
		local _target_arch_arg=

		target=`head -1 -- "${releasedir}/.target"` || exit $?
		if [ -f "${releasedir}/.target_arch" ]; then
			target_arch=`head -1 -- "${releasedir}/.target_arch"` || exit $?
			_target_arch_arg="TARGET_ARCH=${target_arch}"
		fi
		kernel=`head -1 -- "${releasedir}/.kernconf"` || exit $?
		source_tree=`head -1 -- "${releasedir}/.srcdir"` || exit $?

		if [ ${opt_build} -eq 1 ]; then
			lib_info "Starting buildworld with ${jobs} jobs ..."

			make -C "${source_tree}" -j${jobs} buildworld \
				TARGET="${target}" ${_target_arch_arg} ${MAKEARGS} "$@" >> "${buildlog}" 2>&1

			errlevel=$?

			if [ ${errlevel} -eq 0 ]; then
				lib_info "buildworld finished!"
			else
				lib_err ${errlevel} "buildworld failed. Run \`appjail logs read ${build_name}\` for details."
			fi

			if [ ${opt_kernel} -eq 1 ]; then
				lib_info "Starting buildkernel with ${jobs} jobs ..."

				make -C "${source_tree}" -j${jobs} buildkernel \
					TARGET="${target}" ${_target_arch_arg} \
					KERNCONF="${kernel}" ${MAKEARGS} "$@" >> "${buildlog}" 2>&1

				errlevel=$?

				if [ ${errlevel} -eq 0 ]; then
					lib_info "buildkernel finished!"
				else
					lib_err ${errlevel} "buildkernel failed. Run \`appjail logs read ${build_name}\` for details."
				fi
			fi
		fi

		lib_info "Starting installworld with ${jobs} jobs ..."

		make -C "${source_tree}" -j${jobs} installworld \
			DESTDIR="${basedir}" DB_FROM_SRC=1 \
			TARGET="${target}" ${_target_arch_arg} \
			${MAKEARGS} "$@" >> "${buildlog}" 2>&1

		errlevel=$?

		if [ ${errlevel} -eq 0 ]; then
			lib_info "installworld finished!"
		else
			lib_err ${errlevel} "installworld failed. Run \`appjail logs read ${build_name}\` for details."
		fi

		if [ ${opt_kernel} -eq 1 ]; then
			lib_info "Starting installkernel with ${jobs} jobs ..."

			make -C "${source_tree}" -j${jobs} installkernel \
				DESTDIR="${basedir}" KERNCONF="${kernel}" \
				TARGET="${target}" ${_target_arch_arg} \
				${MAKEARGS} "$@" >> "${buildlog}" 2>&1
			
			errlevel=$?

			if [ ${errlevel} -eq 0 ]; then
				lib_info "installkernel finished!"
			else
				lib_err ${errlevel} "installkernel failed. Run \`appjail logs read ${build_name}\` for details."
			fi
		fi

		lib_info "Done."
	else
		local freebsd_update_args
		if [ ${opt_force} -eq 1 ]; then
			freebsd_update_args="-F"
		fi

		local currently_running
		currently_running=`chroot "${basedir}" freebsd-version`

		freebsd-update \
			${freebsd_update_args} \
			-f "${FREEBSD_UPDATE_CONF}" \
			--not-running-from-cron \
			-b "${basedir}" \
			--currently-running "${currently_running}" \
			fetch install
	fi
}

update_release()
{
	local _o
	local opt_force=0
	local freebsd_arch="${FREEBSD_ARCH}"
	local release_name="${DEFAULT_RELEASE}"
	local freebsd_version="${FREEBSD_VERSION}"

	while getopts ":fa:v:" _o; do
		case "${_o}" in
			a|v)
				if lib_check_empty "${OPTARG}"; then
					update_usage
					exit ${EX_USAGE}
				fi
				;;
		esac

		case "${_o}" in
			f)
				opt_force=1
				;;
			a)
				freebsd_arch="${OPTARG}"
				;;
			v)
				freebsd_version="${OPTARG}"
				;;
			*)
				update_usage
				exit ${EX_USAGE}
				;;
		esac
	done
	shift $((OPTIND-1))

	if [ $# -gt 0 ]; then
		release_name="$1"

		if lib_check_empty "${release_name}"; then
			update_usage
			exit ${EX_USAGE}
		fi
	fi

	lib_set_logprefix " [`random_color`${release_name}${COLOR_DEFAULT}]"

	local releasedir="${RELEASEDIR}/${freebsd_arch}/${freebsd_version}/${release_name}"

	if [ -f "${releasedir}/.empty" ]; then
		lib_err ${EX_NOPERM} "This is an empty release, so you will have to update it manually."
	elif [ -f "${releasedir}/.from_src" ]; then
		lib_err ${EX_NOPERM} "This is a release installed from a source tree, so you will have to run \`appjail fetch src\` by yourself."
	fi

	local basedir="${releasedir}/release"

	if [ ! -d "${basedir}" ]; then
		lib_err ${EX_NOINPUT} "Cannot find the release directory (${basedir})."
	fi

	local freebsd_update_args
	if [ ${opt_force} -eq 1 ]; then
		freebsd_update_args="-F"
	fi

	local currently_running
	currently_running=`chroot "${basedir}" freebsd-version`

	freebsd-update \
		${freebsd_update_args} \
		-f "${FREEBSD_UPDATE_CONF}" \
		--not-running-from-cron \
		-b "${basedir}" \
		--currently-running "${currently_running}" \
		fetch install
}

update_help()
{
	man 1 appjail-update
}

update_usage()
{
	cat << EOF
usage: update jail [-bfKk] [-j <jobs>] <jail>
       update release [-f] [-a <arch>] [-v <version>] [<release>]
EOF
}
