#!/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.

. "${AJ_CONFIG}"
. "${LIBDIR}/load"

lib_load "${LIBDIR}/log"
lib_load "${LIBDIR}/check_func"
lib_load "${LIBDIR}/jail"
lib_load "${LIBDIR}/replace"

main()
{
	local args="$1"
	if lib_check_empty "${args}"; then
		lib_err - "usage: PKG [[--chroot|--jexec [--jail]|--local]] <package> ..."
		lib_err - "       PKG [[--chroot|--jexec [--jail]|--local]] --remove <package> ..."
		lib_err - "       PKG [[--chroot|--jexec [--jail]|--local]] --autoremove"
		lib_err - "       PKG [[--chroot|--jexec [--jail]|--local]] --clean"
		lib_err - "       PKG [[--chroot|--jexec [--jail]|--local]] --update"
		lib_err - "       PKG [[--chroot|--jexec [--jail]|--local]] --upgrade"
		exit ${EX_USAGE}
	fi

	# Options
	local use_autoremove=0
	local use_pkgchroot=0
	local use_clean=0
	local use_pkgjail=0
	local use_pkgjexec=1
	local use_pkglocal=0
	local use_remove=0
	local use_update=0
	local use_upgrade=0

	# Arguments
	local args_list
	local total_items
	local current_index=0

	args_list=`lib_split_jailparams "${args}"` || exit $?
	total_items=`printf "%s\n" "${args_list}" | wc -l`
	
	while [ ${current_index} -lt ${total_items} ]; do 
		current_index=$((current_index+1))
		local arg=`printf "%s\n" "${args_list}" | head -${current_index} | tail -n 1`

		if lib_check_empty "${arg}"; then
			continue
		fi

		case "${arg}" in
			--autoremove)
				use_autoremove=1
				;;
			--chroot)
				use_pkgchroot=1
				;;
			--clean)
				use_clean=1
				;;
			--jail)
				use_pkgjail=1
				;;
			--jexec)
				use_pkgjexec=1
				;;
			--local)
				use_pkglocal=1
				;;
			--remove)
				use_remove=1
				;;
			--update)
				use_update=1
				;;
			--upgrade)
				use_upgrade=1
				;;
			--)
				break
				;;
			--*)
				main # usage
				;;
			*)
				break
				;;
		esac
	done
	current_index=$((current_index-1))

	local mutually_exclusive=$((use_clean+use_remove+use_autoremove+use_update+use_upgrade))
	if [ ${mutually_exclusive} -ge 2 ]; then
		main # usage
	fi

	local pkg_args=
	local escape_chars='`"\'

	if [ ${use_clean} -eq 1 ]; then
		pkg_cmd="clean -y"
	elif [ ${use_remove} -eq 1 ]; then
		pkg_cmd="remove -y --"
	elif [ ${use_autoremove} -eq 1 ]; then
		pkg_cmd="autoremove -y"
	elif [ ${use_update} -eq 1 ]; then
		pkg_cmd="update"
	elif [ ${use_upgrade} -eq 1 ]; then
		pkg_cmd="upgrade -y"
	else
		pkg_cmd="install -y --"
	fi

	local pkg_prefix
	if [ ${use_pkgchroot} -eq 1 ]; then
		pkg_prefix="\"\${APPJAIL_SCRIPT}\" pkg chroot \"\${APPJAIL_JAILNAME}\" ${pkg_cmd}"
	elif [ ${use_pkglocal} -eq 1 ]; then
		pkg_prefix="pkg ${pkg_cmd}"
	elif [ ${use_pkgjexec} -eq 1 ]; then
		if [ ${use_pkgjail} -eq 1 ]; then
			pkg_args="-j"
		fi

		pkg_prefix="\"\${APPJAIL_SCRIPT}\" pkg jail \"\${APPJAIL_JAILNAME}\" ${pkg_args} -- ${pkg_cmd}"
	fi

	# command
	printf "%s" "${pkg_prefix}"

	# autoremove, clean, update and upgrade does not require packages.
	if [ ${use_autoremove} -eq 0 -a ${use_clean} -eq 0 -a ${use_update} -eq 0 -a ${use_upgrade} -eq 0 ]; then
		while [ ${current_index} -lt ${total_items} ]; do 
			current_index=$((current_index+1))
			local pkg=`printf "%s\n" "${args_list}" | head -${current_index} | tail -n 1`
			if lib_check_empty "${pkg}"; then
				continue
			fi

			pkg=`lib_escape_string "${pkg}" "" "${escape_chars}"`

			printf " %s" "\"${pkg}\""
		done
	fi
	echo
}

main "$@"
