#! /bin/sh
set -e
readonly PROGRAM="${0##*/}"
readonly VERSION="0.1.0"

print_usage () {
	cat <<-END
	Upgrade selectively schroots deployed on the machine.  If no schroot is
	specified, then a list of available schroots for upgrade is printed
	out.

	Usage: $PROGRAM [-adhlv] [schroots_names ...]

	Options:
	  -a, --all      upgrade all available schroots at once, instead of
	                 proceeding to upgrades on a selection of schroots.
	  -c, --check    check only that schroots listed in argument are part
	                 of the available ones.
	  -d, --debug    verbosely print out each command read and launched by
	                 the shell script, useful for debug.
	  -h, --help     print program usage information.
	  -l, --list     list all available schroots for upgrade.
	  -v, --version  print program version information.
	END
}

print_version () {
	echo "$PROGRAM $VERSION"
}

readonly AVAILABLE_SCHROOTS="$(
	sed -n 's/^\[\([^ \t]\+\)\]$/\1/p' \
		/etc/schroot/chroot.d/*    \
		/etc/schroot/schroot.conf
)"

# print out a nicely wrapped columned list of available schroots, but make sure
# the output remains decently usable for a pipe to other commands.
list_schroots () {
	if [ -t 1 ]
	then
		echo "$AVAILABLE_SCHROOTS" \
		| column
	else
		echo "$AVAILABLE_SCHROOTS"
	fi
}

# validate the list of schroots passed in argument are part of the list of
# available schroots.
validate_list () {
	local schroots="$@"
	local schroot
	local errcode=0
	for schroot in $schroots
	do
		set +e
		echo "$AVAILABLE_SCHROOTS" \
		| grep --quiet -- "^$schroot\$"
		if [ $? != 0 ]
		then
			printf -- 'error: schroot not found: %s\n' \
				"$schroot" >&2
			local errcode=1
		fi
		set -e
	done
	return "$errcode"
}

# proceed to schroot upgrade as such, note that the schroots are expected to
# already exist.
upgrade_schroots () {
	local schroots="$@"
	local schroot
	for schroot in $schroots
	do
		sudo schroot -d / -c "source:$schroot" -- sh -c '
			printf -- "\n>>> upgrading %s\\n" '"$schroot"' \
			&& apt-get update                           \
			&& apt-get upgrade -y                    \
			&& apt-get dist-upgrade -y            \
			&& apt-get autoremove --purge -y   \
			&& apt-get clean
		'
	done
}

main () {
	case "$1" in
	'-a' | '--all' )
		upgrade_schroots "$AVAILABLE_SCHROOTS"
	;;
	'-c' | '--check' )
		shift
		validate_list "$@"
	;;
	'-d' | '--debug' )
		shift
		set -vx
		main "$@"
	;;
	'-h' | '--help' )
		print_usage
	;;
	'-l' | '--list' | '' )
		list_schroots
	;;
	'-v' | '--version' )
		print_version
	;;
	* )
		validate_list "$@"
		upgrade_schroots "$@"
	;;
	esac
}

main "$@"

# Copyright (C) 2020-2021, Étienne Mollier <etienne.mollier@mailoo.org>
# 
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.  You may
# obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.  See the License for the specific language governing
# permissions and limitations under the License.
