#!/bin/sh

# Copyright (c) 2018-2024 Franco Fichtner <franco@opnsense.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. 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.
#
# THIS SOFTWARE IS PROVIDED ``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
# AUTHOR 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.

DEFAULTS="\${product_name} \${product_version} \(\${product_arch}\)"
VERSIONDIR="/usr/local/opnsense/version"
PLUGINCTL="/usr/local/sbin/pluginctl"
PKG="/usr/local/sbin/pkg-static"
TARGET="core"
OUTPUT=

DO_CHECK=
DO_ORIGIN=

while getopts AaCceHilNnOstVvwx OPT; do
	case ${OPT} in
	A)
		OUTPUT="${OUTPUT} \${product_arch}"
		;;
	a)
		OUTPUT="${OUTPUT} \${product_abi}"
		;;
	C)
		OUTPUT="${OUTPUT} \${product_conflicts}"
		;;
	c)
		DO_CHECK="-c"
		;;
	e)
		OUTPUT="${OUTPUT} \${product_email}"
		;;
	H)
		OUTPUT="${OUTPUT} \${product_hash}"
		;;
	i)
		OUTPUT="${OUTPUT} \${product_nickname}"
		;;
	l)
		OUTPUT="${OUTPUT} \${product_lock}"
		;;
	N)
		OUTPUT="${OUTPUT} \${product_name}"
		;;
	n)
		OUTPUT="${OUTPUT} \${product_id}"
		;;
	O)
		DO_ORIGIN="-O"
		;;
	s)
		OUTPUT="${OUTPUT} \${product_size}"
		;;
	t)
		OUTPUT="${OUTPUT} \${product_tier}"
		;;
	V)
		OUTPUT="${OUTPUT} \${product_series}"
		;;
	v)
		OUTPUT="${OUTPUT} \${product_version}"
		;;
	w)
		OUTPUT="${OUTPUT} \${product_website}"
		;;
	x)
		OUTPUT="${OUTPUT} \${target_abi_major}.\${target_abi_minor}"
		;;
	*)
		echo "Usage: man ${0##*/}" >&2
		exit 1
		;;
	esac
done

shift $((OPTIND - 1))

if [ -n "${1}" ]; then
	TARGET=${1}
	shift
fi

VERSION="${VERSIONDIR}/${TARGET}"

if [ -n "${*}" ]; then
	echo "Additional arguments are not supported" >&2
	exit 1
fi

if [ -n "${DO_CHECK}" ]; then
	RET=0
	if [ ! -f ${VERSION} ]; then
		RET=1
	fi
	exit ${RET}
fi

if [ ! -f ${VERSION} ]; then
	echo "Missing ${VERSION}" >&2
	exit 1
fi

if [ -n "${DO_ORIGIN}" ]; then
	cat ${VERSION}
	exit 0
fi

if grep -q '[{}]' ${VERSION}; then
	eval "$(grep -v '[{}]' ${VERSION} | sed 's/[^"]*"\([^"]*\)"[^"]*"\([^"]*\)".*/\1="\2"/')"
fi

case "${TARGET}" in
core)
	if [ -z "${OUTPUT}" ]; then
		OUTPUT="${DEFAULTS}"
	fi

	if [ -z "${OUTPUT%%*product_size*}" -a -f ${PKG} ]; then
		RESULT=$(${PKG} info -s ${product_id})
		product_size=${RESULT##* }
	fi
	if [ -z "${OUTPUT%%*target_abi*}" -a -f ${PLUGINCTL} ]; then
		target_abi_major=${product_abi%%.*}
		target_abi_minor=${product_abi#*.}

		IS_BUSINESS=$(echo ${product_id} | grep -c -- '-business')
		WANT_BUSINESS=$(${PLUGINCTL} -g system.firmware.subscription | grep -c '.')

		if [ "${IS_BUSINESS}${WANT_BUSINESS}" = "01" -o ]; then
			case ${target_abi_minor} in
			1[0-2])
				target_abi_major=$((target_abi_major + 1))
				target_abi_minor=$((target_abi_minor - 9))
				;;
			*)
				target_abi_minor=$((target_abi_minor + 3))
				;;
			esac
		elif [ "${IS_BUSINESS}${WANT_BUSINESS}" = "10" ]; then
			case ${target_abi_minor} in
			[1-3])
				target_abi_major=$((target_abi_major - 1))
				target_abi_minor=$((target_abi_minor + 9))
				;;
			*)
				target_abi_minor=$((target_abi_minor - 3))
				;;
			esac
		fi
	fi
	;;
*)
	if [ -z "${OUTPUT}" ]; then
		if [ -n "${product_name}" ]; then
			OUTPUT=${DEFAULTS}
		else
			OUTPUT="\${product_version}"
		fi
	fi

	if [ -z "${product_version}" -a -z "${OUTPUT%%*product_version*}" -a -f ${VERSION} ]; then
		if [ -z "${product_version}" ]; then
			product_version=$(cat ${VERSION})
		fi
	fi
	if [ -z "${OUTPUT%%*product_size*}" ]; then
		if [ -n "${product_id}" ]; then
			RESULT=$(${PKG} info -s ${product_id})
			product_size=${RESULT##* }
		elif [ -f ${VERSION}.size ]; then
			product_size=$(cat ${VERSION}.size)
		fi
	fi
	if [ -z "${OUTPUT%%*product_arch*}" ]; then
		if [ -z "${product_arch}" -a -f ${VERSION}.arch ]; then
			product_arch=$(cat ${VERSION}.arch)
		fi
	fi
	if [ -z "${OUTPUT%%*product_hash*}" ]; then
		if [ -z "${product_hash}" -a -f ${VERSION}.hash ]; then
			product_hash=$(cat ${VERSION}.hash)
		fi
	fi
	if [ -z "${OUTPUT%%*product_id*}" ]; then
		if [ -z "${product_id}" ]; then
			# mock ID target, there is no .name file
			product_id=${TARGET}
		fi
	fi
	if [ -z "${OUTPUT%%*product_lock*}" ]; then
		if [ -f ${VERSION}.lock ]; then
			# for the time being only .lock files are supported
			product_lock=1
		else
			product_lock=0
		fi
	fi
	;;
esac

OUTPUT=$(eval echo ${OUTPUT})
if [ -n "${OUTPUT}" ]; then
	echo "${OUTPUT}"
fi
