#!/bin/sh
#Copyright (c) 2014-2023 Pierre Pronchery <khorben@EdgeBSD.org>
#
#This code is derived from software contributed to the EdgeBSD Project
#by Pierre Pronchery <khorben@EdgeBSD.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 EDGEBSD PROJECT 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 FOUNDATION 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.



#environment
umask 022
#variables
PROGNAME="edgebsd-update"
#executables
CAT="/bin/cat"
CMP="/usr/bin/cmp"
CP="/bin/cp -af"
DEBUG="_debug"
DUMPFS="/usr/sbin/dumpfs"
FTP="/usr/bin/ftp"
GREP="grep"
GZIP="/usr/bin/gzip -9"
INSTALL="/usr/bin/install"
INSTALLBOOT="/usr/sbin/installboot"
MKDIR="/bin/mkdir -p"
MKTEMP="/usr/bin/mktemp"
MOUNT="/sbin/mount"
MV="/bin/mv -f"
NETPGP="/usr/bin/netpgp"
NETPGPKEYS="/usr/bin/netpgpkeys"
PAXCTL="/usr/sbin/paxctl"
PKGIN="/usr/pkg/bin/pkgin"
POSTINSTALL="/usr/sbin/postinstall"
PRINTF="printf"
PROGRESS="/usr/bin/progress"
REBOOT="/sbin/reboot"
RM="/bin/rm -f"
RMDIR="/bin/rmdir"
ROOTDEV=
SYSCTL="/sbin/sysctl"
TAR="/rescue/tar"
TR="tr"
UMOUNT="/sbin/umount"
UNAME="/usr/bin/uname"
ZCAT="/usr/bin/zcat"

#settings
BOOTLOADER=0
BOOTXX="/usr/mdec/"
DESTDIR=
DEVNULL="/dev/null"
EDGEBSD_ARCH=$($UNAME -m)
EDGEBSD_KERNEL=
EDGEBSD_MIRROR="https://ftp.EdgeBSD.org"
EDGEBSD_PATH=
EDGEBSD_VENDOR="EdgeBSD"
ETCMTREE="/etc/mtree"
INIT=0
KERNEL=0
[ -n "$TMPDIR" ] || TMPDIR="/tmp"
MKTEMP_TEMPLATE="$TMPDIR/$PROGNAME-XXXXXX"
MODULES=0
NOOP=0
PACKAGES=0
RELEASE=
SETEXT=
SETS=
SYSCONFDIR="/usr/pkg/etc"
UPDATE_ETC=0
USRMDECBOOT="/usr/mdec/boot"
USRMDECBOOTXX="/usr/mdec/bootxx"
USRMDECPREKERN="/usr/mdec/prekern"
VERBOSE=1
VERIFY=1
VERIFY_USERID="root@EdgeBSD.org"

#load local settings
[ -f "$SYSCONFDIR/$EDGEBSD_VENDOR/$PROGNAME.conf" ] &&
	. "$SYSCONFDIR/$EDGEBSD_VENDOR/$PROGNAME.conf"
[ -f "$HOME/.config/$EDGEBSD_VENDOR/$PROGNAME.conf" ] &&
	. "$HOME/.config/$EDGEBSD_VENDOR/$PROGNAME.conf"


#functions
#edgebsd_update
_edgebsd_update()
{
	#detect the release
	_detect_release "$RELEASE"
	case "$EDGEBSD_RELEASE/$EDGEBSD_ARCH" in
		EdgeBSD-9/alpha|EdgeBSD-9/amd64|EdgeBSD-9/sparc64|\
			NetBSD-9.*/alpha|NetBSD-9.*/amd64|NetBSD-9.*/sparc64|\
			EdgeBSD-10/alpha|EdgeBSD-10/amd64|EdgeBSD-10/sparc64|\
			NetBSD-10.*/alpha|NetBSD-10.*/amd64|NetBSD-10.*/sparc64)
			[ -n "$SETEXT" ] || SETEXT=".tar.xz"
			;;
		EdgeBSD-6/*|EdgeBSD-7/*|EdgeBSD-8/*|EdgeBSD-9/*|EdgeBSD-10/*|\
			NetBSD-2.*|NetBSD-3.*|NetBSD-4.*|NetBSD-5.*|\
			NetBSD-6.*/*|NetBSD-7.*/*|NetBSD-8.*/*|NetBSD-9.*/*)
			[ -n "$SETEXT" ] || SETEXT=".tgz"
			;;
		*)
			_error "$RELEASE: Unsupported $EDGEBSD_VENDOR release"
			return $?
			;;
	esac

	if [ "$EDGEBSD_VENDOR" = "EdgeBSD" ]; then
		if [ $VERIFY -ne 0 ]; then
			#import the GPG key
			_info "Importing the GPG key..."
			#XXX does not work
			_import_gpg_key				|| return 2
		fi
	fi

	#detect the sets to update
	[ -n "$SETS" ] || SETS=$(_detect_sets)

	if [ $MODULES -ne 0 ]; then
		has_stand=0
		for i in $SETS; do
			[ "$i" = "stand" ] && has_stand=1
		done
		[ $has_stand -eq 0 ] && SETS="$SETS stand"
	fi

	if [ -z "$EDGEBSD_PATH" ]; then
		EDGEBSD_PATH="$EDGEBSD_MIRROR/pub/$EDGEBSD_VENDOR/$EDGEBSD_RELEASE/$EDGEBSD_ARCH/binary/sets"
	else
		EDGEBSD_MIRROR="/nonexistent"
	fi

	#detect and download the kernel
	[ -n "$EDGEBSD_KERNEL" ] || EDGEBSD_KERNEL=$(_detect_kernel)
	_download_kernel
	if [ $? -ne 0 ]; then
		_error "$EDGEBSD_KERNEL: Could not download the kernel"
		return $?
	fi
	_download_kernel_signature
	if [ $? -ne 0 ]; then
		_error "$EDGEBSD_KERNEL: Could not download the kernel signature"
		return $?
	fi

	_download_sets
	if [ $? -ne 0 ]; then
		_error "Could not download the sets"
		return $?
	fi

	#update the kernel
	clean_kernel=0
	_update_kernel
	ret=$?
	if [ $ret -eq 0 ]; then
		[ $INIT -ne 0 -o -n "${EDGEBSD_MIRROR%%/*}" ] && clean_kernel=1
	else
		_error "Could not update the kernel"
	fi

	#update the sets
	clean_sets=0
	clean_sets_keep="etc"
	if [ $ret -eq 0 ]; then
		_update_sets
		ret=$?
		if [ $ret -eq 0 ]; then
			[ $INIT -ne 0 -o -n "${EDGEBSD_MIRROR%%/*}" ] && clean_sets=1
		else
			_error "Could not update the sets"
		fi
	fi

	#update the configuration
	if [ $ret -eq 0 ]; then
		_update_configuration
		ret=$?
		if [ $ret -eq 0 ]; then
			clean_sets_keep=
		else
			_error "Could not update the configuration"
		fi
	fi

	#update the packages
	if [ $ret -eq 0 ]; then
		_update_packages
		ret=$?
		[ $ret -eq 0 ] || _error "Could not update the packages"
	fi

	#update the bootloader
	if [ $ret -eq 0 ]; then
		_update_bootloader
		ret=$?
		[ $ret -eq 0 ] || _error "Could not update the bootloader"
	fi

	#cleanup
	[ $clean_kernel -eq 1 ] && _clean_kernel
	[ $clean_sets -eq 1 ] && _clean_sets "$clean_sets_keep"

	if [ $ret -eq 0 ]; then
		_info "Update successful."
	else
		_info "Some errors occurred."
	fi

	#reboot after update
	if [ $ret -eq 0 ]; then
		_update_reboot
		ret=$?
		if [ $ret -ne 0 ]; then
			_error "Could not reboot after update"
			return $?
		fi
	fi

	return $ret
}


#architecture-specific
_arch_amd64_update_bootloader()
{
	_arch_i386_update_bootloader
}

_arch_amd64_update_kernel()
{
	_arch_i386_update_kernel
}

_arch_evbarm_update_bootloader()
{
	#XXX assumes the Raspberry PI and DESTDIR=/
	#TODO replace the firmware content if available
	return 0
}

_arch_evbarm_update_kernel()
{
	ret=0

	#XXX assumes the Raspberry PI and DESTDIR=/
	if ! $DEBUG $MOUNT "$DESTDIR/boot"; then
		_error "$EDGEBSD_KERNEL: Could not mount the boot partition"
		return $?
	fi
	file="kern-${EDGEBSD_KERNEL}${SETEXT}"
	[ -z "${EDGEBSD_MIRROR##/*}" ] && file="$EDGEBSD_PATH/$file"
	if [ $VERIFY -ne 0 ]; then
		_info "Verifying kernel ${EDGEBSD_KERNEL}..."
		_verify "$file"
		if [ $? -ne 0 ]; then
			_error "$EDGEBSD_KERNEL: Could not verify the kernel"
			return $?
		fi
	fi
	#backup the current kernel
	if [ -f "$DESTDIR/boot/kernel7.img" ]; then
		$DEBUG $CP -- "$DESTDIR/boot/kernel7.img" \
			"$DESTDIR/boot/okernel7.img"
		if [ $? -ne 0 ]; then
			_error "$EDGEBSD_KERNEL: Could not backup the kernel"
			return $?
		fi
	fi
	_info "Installing kernel ${EDGEBSD_KERNEL}..."
	_progress -f "$file" -p "$($PRINTF "%-7s " "$EDGEBSD_KERNEL")" \
		$TAR -xzpf - -C "$DESTDIR/boot"
	if [ $? -ne 0 ]; then
		_error "$EDGEBSD_KERNEL: Could not install the kernel"
		return $?
	fi
	_info "Renaming kernel ${EDGEBSD_KERNEL}..."
	$DEBUG $MV -f -- "$DESTDIR/boot/netbsd.bin" "$DESTDIR/boot/kernel7.img"
	if [ $? -ne 0 ]; then
		_error "$EDGEBSD_KERNEL: Could not rename the kernel"
		return $?
	fi
	ret=$?
	$DEBUG $UMOUNT "$DESTDIR/boot"
	[ $? -ne 0 ] && _error "$EDGEBSD_KERNEL: Could not unmount the boot partition"
	return $ret
}

_arch_generic_update_bootloader()
{
	if [ -f "$DESTDIR$USRMDECBOOT" ]; then
		if [ -f "$DESTDIR/altroot/boot" ]; then
			_update_file 0444 "$DESTDIR$USRMDECBOOT" \
				"$DESTDIR/altroot/boot"		|| return 2
		fi
		if [ -f "$DESTDIR/boot" ]; then
			_update_file 0444 "$DESTDIR$USRMDECBOOT" \
				"$DESTDIR/boot"			|| return 2
		fi
	fi
	if [ -f "$DESTDIR$USRMDECPREKERN" ]; then
		if [ -f "$DESTDIR/altroot/prekern" ]; then
			_update_file 0444 "$DESTDIR$USRMDECPREKERN" \
				"$DESTDIR/altroot/prekern"	|| return 2
		fi
		if [ -f "$DESTDIR/prekern" ]; then
			_update_file 0444 "$DESTDIR$USRMDECPREKERN" \
				"$DESTDIR/prekern"		|| return 2
		fi
	fi
	return 0
}

_arch_generic_update_kernel()
{
	file="kern-${EDGEBSD_KERNEL}${SETEXT}"
	[ -z "${EDGEBSD_MIRROR##/*}" ] && file="$EDGEBSD_PATH/$file"
	if [ ! -f "$file" ]; then
		if [ $INIT -ne 0 ]; then
			_info "$EDGEBSD_KERNEL: Skipping kernel update (update file missing)"
			return $?
		fi
		_error "$EDGEBSD_KERNEL: Update file missing"
		return $?
	fi
	if [ $VERIFY -ne 0 ]; then
		_info "Verifying kernel ${EDGEBSD_KERNEL}..."
		_verify "$file"
		if [ $? -ne 0 ]; then
			_error "$EDGEBSD_KERNEL: Could not verify the kernel"
			return $?
		fi
	fi
	#detect the kernel file booted
	bootedfile=$($SYSCTL -n "machdep.booted_kernel" 2> "$DEVNULL")
	[ $? -eq 0 -a -n "$bootedfile" ] || bootedfile="/netbsd.gz"
	bootedfile=${bootedfile#/}
	#detect if a backup was booted
	if [ "${bootedfile#o}" != "$bootedfile" ]; then
		bootedfile="${bootedfile#o}"
	elif [ -f "$DESTDIR/altroot/$bootedfile" ]; then
		#backup the current kernel
		$DEBUG $CP -- "$DESTDIR/altroot/$bootedfile" \
			"$DESTDIR/altroot/o$bootedfile"
		if [ $? -ne 0 ]; then
			_error "$EDGEBSD_KERNEL: Could not backup the kernel"
			return $?
		fi
	elif [ -f "$DESTDIR/$bootedfile" ]; then
		#backup the current kernel
		$DEBUG $CP -- "$DESTDIR/$bootedfile" "$DESTDIR/o$bootedfile"
		if [ $? -ne 0 ]; then
			_error "$EDGEBSD_KERNEL: Could not backup the kernel"
			return $?
		fi
	fi
	#install the new kernel
	tmpdir=$($MKTEMP -d "$MKTEMP_TEMPLATE")
	[ $? -eq 0 ]						|| return 2
	_info "Installing kernel ${EDGEBSD_KERNEL}..."
	_progress -f "$file" -p "$($PRINTF "%-7s " "$EDGEBSD_KERNEL")" \
		$TAR -xzpf - -C "$tmpdir"
	if [ $? -ne 0 ]; then
		_error "$EDGEBSD_KERNEL: Could not install the kernel"
		$DEBUG $RM -- "$tmpdir/netbsd"
		NOOP=0 $DEBUG $RMDIR -- "$tmpdir"
		return $?
	fi
	if [ "${bootedfile%.gz}" != "$bootedfile" ]; then
		#compress the kernel
		_info "Compressing kernel ${EDGEBSD_KERNEL}..."
		$DEBUG $GZIP -f -- "$tmpdir/netbsd" &&
			$DEBUG $MV -- "$tmpdir/netbsd.gz" "$tmpdir/netbsd"
		if [ $? -ne 0 ]; then
			_error "$EDGEBSD_KERNEL: Could not compress the kernel"
			$DEBUG $RM -- "$tmpdir/netbsd" "$tmpdir/netbsd.gz"
			NOOP=0 $DEBUG $RMDIR -- "$tmpdir"
			return $?
		fi
	fi
	if [ -f "$DESTDIR/altroot/$bootedfile" ]; then
		$DEBUG $MV -- "$tmpdir/netbsd" "$DESTDIR/altroot/$bootedfile"
	else
		$DEBUG $MV -- "$tmpdir/netbsd" "$DESTDIR/$bootedfile"
	fi
	if [ $? -ne 0 ]; then
		_error "$EDGEBSD_KERNEL: Could not update the kernel"
		$DEBUG $RM -- "$tmpdir/netbsd"
		NOOP=0 $DEBUG $RMDIR -- "$tmpdir"
		return $?
	fi
	NOOP=0 $DEBUG $RMDIR -- "$tmpdir"
	if [ $INIT -ne 0 ]; then
		#remove the kernel set
		_clean_kernel
		#reboot after kernel update
		_info "Rebooting after kernel update..."
		$DEBUG $REBOOT
		if [ $? -ne 0 ]; then
			_error "Could not reboot after kernel update"
			return $?
		fi
	fi
	return 0
}

_arch_i386_update_bootloader()
{
	_arch_generic_update_bootloader				|| return 2
	#detect the boot method
	bootmethod=$($SYSCTL -n "machdep.bootmethod" 2> "$DEVNULL")
	if [ $? -ne 0 -o -z "$bootmethod" ]; then
		_error "Could not detect the boot method (assuming BIOS)"
		bootmethod="BIOS"
	fi
	case "$bootmethod" in
		UEFI)
			#TODO update the EFI boot file
			return 0
			;;
		BIOS)
			_arch_i386_update_bootloader_bios
			return $?
			;;
		*)
			_error "$bootmethod: Unsupported boot method"
			return $?
			;;
	esac
}

_arch_i386_update_bootloader_bios()
{
	#XXX require the root device to be set
	if [ -z "$ROOTDEV" ]; then
		_error "Could not update the bootloader (root device not set)"
		return 0
	fi
	#detect the underlying filesystem
	filesystem=$($DUMPFS "$ROOTDEV" | $GREP '^format	')
	if [ $? -ne 0 -o -z "$filesystem" ]; then
		_error "$DESTDIR/: Could not detect the filesystem"
		return $?
	fi
	filesystem=${filesystem#*	}
	case "$filesystem" in
		FFSv1|FFSv2)
			filesystem=$(echo "$filesystem" | _tolower)
			;;
	esac
	if [ ! -f "${USRMDECBOOTXX}_$filesystem" ]; then
		_error "$filesystem: Unsupported filesystem for boot partition"
		return $?
	fi
	$DEBUG $INSTALLBOOT "$ROOTDEV" "${USRMDECBOOTXX}_$filesystem"
	if [ $? -eq 0 ]; then
		_error "$ROOTDEV: Could not install the bootloader"
		return $?
	fi
	return 0
}

_arch_i386_update_kernel()
{
	_arch_generic_update_kernel
}

_arch_sparc_update_bootloader()
{
	_arch_generic_update_bootloader
}

_arch_sparc_update_kernel()
{
	_arch_generic_update_kernel
}

_arch_sparc64_update_bootloader()
{
	_arch_sparc_update_bootloader
}

_arch_sparc64_update_kernel()
{
	_arch_sparc_update_kernel
}


#clean_kernel
_clean_kernel()
{
	if [ $KERNEL -ne 0 ]; then
		file="kern-${EDGEBSD_KERNEL}${SETEXT}"
		[ -z "${EDGEBSD_MIRROR##/*}" ] && file="$EDGEBSD_PATH/$file"
		#XXX ignore errors
		if [ -f "$file" ]; then
			_info "Removing downloaded kernel ${EDGEBSD_KERNEL}..."
			$DEBUG $RM -- "$file"
		fi
		if [ $VERIFY -ne 0 -a -f "$file.sig" ]; then
			_info "Removing downloaded signature for kernel ${EDGEBSD_KERNEL}..."
			$DEBUG $RM -- "$file.sig"
		fi
	fi
}


#clean_sets
_clean_sets()
{
	keep="$1"

	for set in $SETS; do
		[ -n "$keep" -a "$keep" = "$set" ]		&& continue
		file="$set${SETEXT}"
		[ -z "${EDGEBSD_MIRROR##/*}" ] && file="$EDGEBSD_PATH/$file"
		#XXX ignore errors
		if [ -f "$file" ]; then
			_info "Removing downloaded set ${set}..."
			$DEBUG $RM -- "$file"
		fi
		if [ $VERIFY -ne 0 -a -f "$file.sig" ]; then
			_info "Removing downloaded signature for set ${set}..."
			$DEBUG $RM -- "$file.sig"
		fi
	done
}


#debug
_debug()
{
	[ $VERBOSE -ge 3 ] && echo "$@" 1>&2
	[ $NOOP -ne 0 ] || "$@"
}


#detect_kernel
_detect_kernel()
{
	kernel=$($UNAME -v)
	kernel="${kernel##*(}"
	echo "${kernel%)*}"
}


#detect_release
_detect_release()
{
	if [ -n "$1" ]; then
		EDGEBSD_RELEASE="$EDGEBSD_VENDOR-$1"
	elif [ -z "$EDGEBSD_RELEASE" ]; then
		EDGEBSD_RELEASE=$($UNAME -r)
		case "$EDGEBSD_VENDOR" in
			NetBSD)
				EDGEBSD_RELEASE="${EDGEBSD_RELEASE%_*}"
				;;
			*)
				EDGEBSD_RELEASE="${EDGEBSD_RELEASE%.*}"
				;;
		esac
		EDGEBSD_RELEASE="$EDGEBSD_VENDOR-$EDGEBSD_RELEASE"
	fi
}


#detect_sets
_detect_sets()
{
	for i in "$DESTDIR$ETCMTREE/set."*; do
		[ -f "$i" ]					|| continue
		set="${i#$DESTDIR$ETCMTREE/set.}"
		[ -z "$set" ] || echo "$set"
	done
}


#download
_download()
{
	file="$1"

	NOOP=0 $DEBUG $FTP "$EDGEBSD_PATH/$file"
	res=$?
	[ $res -eq 0 ] || $DEBUG $RM -- "$file"
	return $res
}


#download_kernel
_download_kernel()
{
	#download the kernel
	if [ $KERNEL -ne 0 -a -n "${EDGEBSD_MIRROR##/*}" ]; then
		if [ ! -f "kern-${EDGEBSD_KERNEL}${SETEXT}" ]; then
			_info "Downloading kernel ${EDGEBSD_KERNEL}..."
			_download "kern-${EDGEBSD_KERNEL}${SETEXT}"
			return $?
		fi
	fi
	return 0
}


#download_kernel_signature
_download_kernel_signature()
{
	#download the kernel signature
	if [ $KERNEL -ne 0 -a -n "${EDGEBSD_MIRROR##/*}" ]; then
		if [ $VERIFY -ne 0 -a ! -f "kern-${EDGEBSD_KERNEL}${SETEXT}.sig" ]; then
			_info "Downloading signature for kernel ${EDGEBSD_KERNEL}..."
			_download "kern-${EDGEBSD_KERNEL}${SETEXT}.sig"
			return $?
		fi
	fi
	return 0
}


#download_sets
_download_sets()
{
	res=0

	#download the relevant sets
	for set in $SETS; do
		[ -z "${EDGEBSD_MIRROR##/*}" ] && continue
		[ -f "${set}${SETEXT}" ] && continue
		_info "Downloading set ${set}..."
		_download "${set}${SETEXT}"
		res=$?
		[ $res -eq 0 ] || break
		[ $VERIFY -eq 0 ] && continue
		_info "Downloading signature for set ${set}..."
		_download "${set}${SETEXT}.sig"
		res=$?
		[ $res -eq 0 ] || break
	done
	if [ $res -ne 0 -a -n "${EDGEBSD_MIRROR##/*}" ]; then
		for set in $SETS; do
			$DEBUG $RM -- "${set}${SETEXT}"
			[ $VERIFY -eq 0 ] && continue
			$DEBUG $RM -- "${set}${SETEXT}.sig"
		done
	fi
	return $res
}


#error
_error()
{
	echo "$PROGNAME: $@" 1>&2
	return 2
}


#import_gpg_key
_import_gpg_key()
{
	ret=0

	tmpfile=$($MKTEMP "$MKTEMP_TEMPLATE")
	[ $? -eq 0 ]						|| return 2
	_import_gpg_key_echo > "$tmpfile" &&
	$DEBUG $NETPGPKEYS --import-key "$tmpfile" > "$DEVNULL"	|| ret=2
	$RM -- "$tmpfile"
	return $ret
}


#import_gpg_key_echo
_import_gpg_key_echo()
{
	echo "-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1

mQINBFN+vzoBEACqHaeLm1s87JMrg6YwnZL2UnFesCG9DlTlEKooe0B8SsDrDIGO
yVuhVYoXmvG5bZCqRkUskRnILLFi1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YAQIADwUCU36/
OgIbDAUJEswDAAAKCRDSbVi43IbdaduwD/9MvwosDb9IqmFZ7HnyL4cXnqtfykqC
LMYYlKCrihvabZO824aDhvnPlUYxnb1LHi9D+kOD3w7Ky7gp530hEpKUU8YPdM2d
PcfYaAJ8EAFV3nio0dPH64Et9YAPatqyJvhLDFFU6ikUjsvu5M0Fak4rrG7z5MIQ
Pq8K4GtYfYKMxFFoGYrtzy6soYQSgCgtUCW2JDO+f9VhPyhvyWX5kGxBJQumJNlc
dB5Act5PpcfZD5B37cLLSvSsFvA7v/ussQTfVoExxFZGCKBviIj72WXE/TID+8tX
TJc5Dt2Pu2uv9UN0C6Kz40V6r0WgDoY0N1QRjZaDquZdYfUUOJwCIDZS+89G1l1p
CACUTcNF6rdvqMmXErkvIkzF4rIYfk/tcj/BDOnIkLZTo+//zGBI2xPjZZrrBYFb
T1omTHX42msZhN/2N7mqvqPNQQJu3e5VVfBwF2LmXBxsaz/9bUJRbLtnL1Eh4Mj6
9UgV9dM51Zcw+ygOWU7bCyO36GiKYZehuOHn30ZJ4jDKQnFMs3+Brw9UEgmTN+R4
LUpTDZLArqVheAUsA1WZP6xIYRGiu8LVTu4vPm7X5je36P/2GbYdgTYE4nFE0PKc
uY1IVSc/BQkoP4NTt/GDBAHEGRYJEf4rHOvlQpCnFCqVdsmtmT3uJFXfv8XAuCSL
mEH+2rR/ZC0mmQ==
=dmei
-----END PGP PUBLIC KEY BLOCK-----"
}


#info
_info()
{
	[ $VERBOSE -lt 2 ] || echo "$@"
}


#progress
_progress()
{(
	cat="$CAT"
	options=
	filename=
	prefix=

	while getopts "ef:p:z" name; do
		case "$name" in
			e)
				options="-e $options"
				;;
			f)
				#XXX must be last
				options="$options -f"
				filename="$OPTARG"
				;;
			p)
				prefix="$OPTARG"
				;;
			z)
				options="-z $options"
				cat="$ZCAT"
				;;
			*)
				_progress_usage
				return $?
				;;
		esac
	done
	shift $((OPTIND - 1))
	if [ $# -lt 1 ]; then
		return 1
	fi
	#only use progress(1) if in interactive and verbose mode
	if [ -n "$PS1" -a $VERBOSE -ge 1 ]; then
		$DEBUG $PROGRESS -p "$prefix" $options "$filename" -- "$@"
	else
		$DEBUG $cat -- "$filename" | "$@"
	fi
)}

_progress_usage()
{
	echo "Usage: progress [-ez][-f filename][-p prefix] cmd [args...]" 1>&2
	return 1
}


#tolower
_tolower()
{
	$TR A-Z a-z
}


#update_bootloader
_update_bootloader()
{
	#update the bootloader
	if [ $BOOTLOADER -ne 0 ]; then
		_info "Updating the bootloader..."
		case "$EDGEBSD_ARCH" in
			amd64|evbarm|i386|sparc|sparc64)
				"_arch_${EDGEBSD_ARCH}_update_bootloader"
				return $?
				;;
			*)
				return 2
				;;
		esac
	fi
	return 0
}


#update_configuration
_update_configuration()
{
	res=0

	#update the configuration
	if [ $UPDATE_ETC -eq 1 ]; then
		set="etc"
		file="${set}${SETEXT}"
		[ -z "${EDGEBSD_MIRROR##/*}" ] && file="$EDGEBSD_PATH/$file"
		if [ $VERIFY -ne 0 ]; then
			_info "Verifying set ${set}..."
			_verify "$file"
			res=$?
			[ $res -eq 0 ] || return $res
		fi
		_info "Updating the configuration..."
		$DEBUG $POSTINSTALL -s "$file" -d "$DESTDIR/" fix > "$DEVNULL"
		res=$?
	fi
	return $res
}


#update_file
_update_file()
{
	mode="$1"
	src="$2"
	dst="$3"

	$DEBUG $CMP "$src" "$dst" > "$DEVNULL"
	[ $? -eq 0 ] || $DEBUG $INSTALL -m "$mode" "$src" "$dst"
}


#update_kernel
_update_kernel()
{
	#update the kernel
	if [ $KERNEL -ne 0 ]; then
		_info "Updating the kernel..."
		case "$EDGEBSD_ARCH" in
			amd64|evbarm|i386|sparc|sparc64)
				"_arch_${EDGEBSD_ARCH}_update_kernel"
				return $?
				;;
		esac
		return 2
	fi
	return 0
}


#update_packages
_update_packages()
{
	#update the packages
	if [ $PACKAGES -ne 0 ]; then
		_info "Updating the packages..."
		if [ -n "$DESTDIR" ]; then
			$DEBUG $PKGIN -y -c "$DESTDIR" full-upgrade
		else
			$DEBUG $PKGIN -y full-upgrade
		fi
		return $?
	fi
	return 0
}


#update_reboot
_update_reboot()
{
	if [ $INIT -ne 0 ]; then
		if [ $UPDATE_ETC -eq 1 -a -z "$clean_sets_keep" ]; then
			_info "Rebooting after set update..."
			$DEBUG $REBOOT
			return $?
		else
			_error "Partial update (not rebooting)"
		fi
	fi
	return 0
}


#update_sets
_update_sets()
{
	#update the relevant sets
	_info "Updating the sets..."
	$MKDIR -- "$DESTDIR/"
	for set in $SETS; do
		file="${set}${SETEXT}"
		[ -z "${EDGEBSD_MIRROR##/*}" ] && file="$EDGEBSD_PATH/$file"
		if [ ! -f "$file" ]; then
			if [ $INIT -ne 0 ]; then
				_info "$set: Skipping set update (update file missing)"
				continue
			fi
			_error "$set: Set file missing"
			return $?
		fi
		if [ $VERIFY -ne 0 ]; then
			_info "Verifying set ${set}..."
			_verify "$file"
			res=$?
			[ $res -eq 0 ]				|| return $res
		fi
		if [ "$set" = "etc" ]; then
			UPDATE_ETC=1
			#skip the "etc" set if already installed
			[ -f "$DESTDIR$ETCMTREE/set.$set" ]	&& continue
		fi
		_progress -f "$file" -p "$($PRINTF "%-7s " "${set}")" \
			$TAR -xzpf - -C "$DESTDIR/"
		res=$?
		[ $res -eq 0 ]					|| return $res
		#also extract the modules to /altroot if relevant
		if [ "$set" = "modules" -a -d "$DESTDIR/altroot/stand" ]; then
			_progress -f "$file" -p "$($PRINTF "%-7s " "${set}")" \
				$TAR -xzpf - -C "$DESTDIR/altroot"
			res=$?
			[ $res -eq 0 ]				|| return $res
		fi
	done
	return 0
}


#usage
_usage()
{
	echo "Usage: $PROGNAME [-BbIKkmnpqSsv][-D destdir][-M mirror][-P path][-r release]" 1>&2
	echo "  -B	Do not update the bootloader" 1>&2
	echo "  -b	Update the bootloader" 1>&2
	echo "  -D	Specify a different directory where to update" 1>&2
	echo "  -I	Perform a boot-time update" 1>&2
	echo "  -K	Do not update the kernel" 1>&2
	echo "  -k	Update the kernel" 1>&2
	echo "  -M	Specify a different mirror when downloading" 1>&2
	echo "  -m	Update the kernel modules" 1>&2
	echo "  -n	Download the sets but do not actually update" 1>&2
	echo "  -P	Full path to the sets" 1>&2
	echo "  -p	Update the packages" 1>&2
	echo "  -q	Enable quiet mode" 1>&2
	echo "  -r	Set the release" 1>&2
	echo "  -S	Do not verify signatures" 1>&2
	echo "  -s	Verify signatures (default)" 1>&2
	echo "  -v	Be more verbose" 1>&2
	return 1
}


#verify
_verify()
{
	file="$1"

	$NETPGP --verify --userid "$VERIFY_USERID" "${file}.sig"
}


#main
#parse arguments
while getopts "D:O:M:P:BbIKkmnpqr:Ssv" name; do
	case "$name" in
		D)
			DESTDIR="$OPTARG"
			;;
		O)
			export "${OPTARG%%=*}"="${OPTARG#*=}"
			;;
		P)
			EDGEBSD_PATH="$OPTARG"
			;;
		B)
			BOOTLOADER=0
			;;
		b)
			BOOTLOADER=1
			;;
		I)
			BOOTLOADER=1
			KERNEL=1
			INIT=1
			;;
		K)
			KERNEL=0
			;;
		k)
			KERNEL=1
			;;
		M)
			EDGEBSD_MIRROR="$OPTARG"
			;;
		m)
			MODULES=1
			;;
		n)
			NOOP=1
			;;
		p)
			PACKAGES=1
			;;
		q)
			FTP="/usr/bin/ftp -V"
			VERBOSE=0
			;;
		r)
			RELEASE="$OPTARG"
			;;
		S)
			VERIFY=0
			;;
		s)
			VERIFY=1
			;;
		v)
			VERBOSE=$((VERBOSE + 1))
			[ $VERBOSE -ge 1 ] && FTP="/usr/bin/ftp"
			[ $VERBOSE -ge 2 ] && FTP="/usr/bin/ftp -v"
			;;
		*)
			_usage
			exit $?
			;;
	esac
done
shift $((OPTIND - 1))
if [ $# -ne 0 ]; then
	_usage
	exit $?
fi

_edgebsd_update
