#!/bin/sh

# Copyright (c) 2018-2019 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.

VERSIONDIR="/usr/local/opnsense/version"
OPENSSL="/usr/local/bin/openssl"
TARGET="core"
OUTPUT=

while getopts AaefHNnsvw OPT; do
	case ${OPT} in
	A)
		OUTPUT="${OUTPUT} \${product_arch}"
		;;
	a)
		OUTPUT="${OUTPUT} \${product_abi}"
		;;
	e)
		OUTPUT="${OUTPUT} \${product_email}"
		;;
	f)
		OUTPUT="${OUTPUT} \${product_flavour}"
		;;
	H)
		OUTPUT="${OUTPUT} \${product_hash}"
		;;
	N)
		OUTPUT="${OUTPUT} \${product_name}"
		;;
	n)
		OUTPUT="${OUTPUT} \${product_id}"
		;;
	s)
		OUTPUT="${OUTPUT} \${product_size}"
		;;
	v)
		OUTPUT="${OUTPUT} \${product_version}"
		;;
	w)
		OUTPUT="${OUTPUT} \${product_website}"
		;;
	*)
		echo "Usage: man ${0##*/}" >&2
		exit 1
		;;
	esac
done

shift $((${OPTIND} - 1))

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

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

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

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

case "${TARGET}" in
core)
	eval "$(grep -v '[{}]' ${VERSION} | sed 's/[^"]*"\([^"]*\)"[^"]*"\([^"]*\)".*/\1="\2"/')"

	if [ -z "${OUTPUT}" ]; then
		OUTPUT="\${product_name} \${product_version} \(\${product_arch}/\${product_flavour}\)"
	fi

	if [ -z "${OUTPUT%%*product_flavour*}" -a -f ${OPENSSL} ]; then
		RESULT=$(${OPENSSL} version)
		product_flavour=${RESULT%% *}
	fi
	;;
*)
	if [ -z "${OUTPUT}" ]; then
		OUTPUT="\${product_version}"
	fi

	# mock ID target, there is no .name file
	product_id=${TARGET}

	if [ -z "${OUTPUT%%*product_version*}" -a -f ${VERSION} ]; then
		product_version=$(cat ${VERSION})
	fi
	if [ -z "${OUTPUT%%*product_size*}" -a -f ${VERSION}.size ]; then
		product_size=$(cat ${VERSION}.size)
	fi
	if [ -z "${OUTPUT%%*product_arch*}" -a -f ${VERSION}.arch ]; then
		product_arch=$(cat ${VERSION}.arch)
	fi
	;;
esac

eval echo ${OUTPUT}
