#!/bin/sh
#-
# Copyright (c) 2011 Nathan Whitehorn
# Copyright (c) 2026 Devin Teske
# All rights reserved.
#
# 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.
#

test -f "$BSDINSTALL_DISTDIR/MANIFEST" || exit 0

BSDCFG_SHARE=/usr/share/bsdconfig
. $BSDCFG_SHARE/common.subr || exit 1
f_include $BSDCFG_SHARE/strings.subr

dist_to_statusvar()
{
	local __name="$1" __var_to_set="$2"

	# Underscore first, so later encodings stay injective
	case "$__name" in *_*) f_replaceall "$__name" _ __ __name ;; esac

	# We expect to see these in names of DISTRIBUTIONS
	case "$__name" in *.*) f_replaceall "$__name" . _dot_ __name ;; esac
	case "$__name" in *-*) f_replaceall "$__name" - _dash_ __name ;; esac

	# Anything else becomes underscore
	case "$__name" in
	*[!$VALID_VARNAME_CHARS]*) f_str2varname "$__name" __name ;;
	esac

	setvar "$__var_to_set" "status_$__name"
}

NDISTRIBUTIONS=$( set -- $DISTRIBUTIONS; echo $#  )

n=0
for dist in $DISTRIBUTIONS; do
	dist_to_statusvar "$dist" statusvar
	setvar $statusvar -8 # inprogress

	items=""
	for i in $DISTRIBUTIONS; do
		dist_to_statusvar $i itemvar
		f_getvar $itemvar:--11 itemstatus # current status or pending
		items="$items $i $itemstatus"
	done
	percentage=$(( $n * 100 / $NDISTRIBUTIONS ))
	bsddialog --backtitle "$OSNAME Installer" --title "Checksum Verification" \
	    --mixedgauge "\nVerifying checksums of selected distributions.\n" \
	    0 0 $percentage  -- $items

	CK=$( sha256 -q "$BSDINSTALL_DISTDIR/$dist" )
	awk -v checksum="$CK" -v dist="$dist" '
		function str(x) { return x "" }
		$1 == dist {
			found = 1
			# NB: force non-numerical comparison
			exit(str(checksum) == $2 ? 0 : 2)
		}
		END { if (!found) exit(1) }
	' "$BSDINSTALL_DISTDIR/MANIFEST"
	CK_VALID=$?

	if [ $CK_VALID -eq 0 ]; then
		# Checksum matched
		setvar $statusvar -3 # passed
	elif [ $CK_VALID -eq 1 ]; then
		# dist not found in MANIFEST
		setvar $statusvar -7 # skipped
	else
		# Checksum mismatch or error reading MANIFEST
		setvar $statusvar -2 # failed
		case $(/bin/freebsd-version -u) in
		*-ALPHA*|*-CURRENT|*-STABLE|*-PRERELEASE)
			bsddialog --backtitle "$OSNAME Installer" --title "Error" \
			    --msgbox "The checksum for $dist does not match. It may have become corrupted, or it may be from a newer version of $OSNAME. Please check for a newer snapshot." 0 0
			;;
		*)
			bsddialog --backtitle "$OSNAME Installer" --title "Error" \
			    --msgbox "The checksum for $dist does not match. It may have become corrupted, and should be redownloaded." 0 0
		esac
		exit 1
	fi

	n=$(( $n + 1 ))
done

exit 0
