#!/bin/sh

# Copyright (c) 2026 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 BY THE AUTHOR 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 AUTHOR 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.

set -e

DO_ABI=
DO_CURL=
DO_INSECURE=
DO_RELEASE=
DO_VERBOSE=

while getopts A:cir:Vz OPT; do
	case ${OPT} in
	A)
		DO_ABI="-A ${OPTARG}"
		;;
	c)
		DO_CURL="-c"
		;;
	i)
		DO_INSECURE="-i"
		;;
	r)
		DO_RELEASE="-r ${OPTARG}"
		;;
	V)
		DO_VERBOSE="-V"
		;;
	z)
		DO_ABI="-A snapshots"
		;;
	*)
		echo "Usage: man ${0##*/}" >&2
		exit 1
		;;
	esac
done

shift $((OPTIND - 1))

if [ -n "${DO_VERBOSE}" ]; then
	set -x
fi

if [ -z "${DO_RELEASE}" ]; then
	echo "Please select a release." >&2
	exit 1
fi

if [ -z "${*}" ]; then
	echo "Please select at least one set." >&2
	exit 1
fi

MIRROR="$(opnsense-update -M)"
ARCH=$(uname -p)

if [ -n "${DO_ABI}" ]; then
	MIRROR="${MIRROR%/*}/${DO_ABI#-A }"
fi

for SET in ${*}; do
	SETFILE="${SET}-${DO_RELEASE#-r }-${ARCH}"
	if [ "${SET}" = "packages" ]; then
		SETFILE="${SETFILE}.tar"
	else
		SETFILE="${SETFILE}.txz"
	fi

	FILES=${SETFILE}
	if [ -z "${DO_INSECURE}" ]; then
		FILES="${SETFILE}.sig ${FILES}"
	fi

	rm -f ${FILES}

	for FILE in ${FILES}; do
		if [ -n "${DO_CURL}" ]; then
			REFSIZE=$(curl -sI "${MIRROR}/sets/${FILE}" | grep -i Content-Length | awk '{sub("\r", ""); print $2}')
		else
			REFSIZE=$(fetch -s "${MIRROR}/sets/${FILE}" || true 2> /dev/null)
		fi

		if [ -z "${REFSIZE}" ]; then
			REFSIZE="Unknown"
		fi

		echo "Downloading ${FILE} with remote size ${REFSIZE}:"

		if [ -n "${DO_CURL}" ]; then
			curl -f -o "${FILE}" "${MIRROR}/sets/${FILE}"
		else
			fetch -o "${FILE}" "${MIRROR}/sets/${FILE}"
		fi
	done

	SIZE=$(stat -f %z "${SETFILE}")
	if [ "${REFSIZE}" != "${SIZE}" ]; then
		echo "Remote size ${REFSIZE} does not match downloaded file size ${SIZE}"
	fi

	if [ -z "${DO_INSECURE}" ]; then
		if ! opnsense-verify "${SETFILE}"; then
			rm -f ${FILES}
		fi
	fi
done
