#!/bin/bash -eu
# SPDX-License-Identifier: GPL-3.0-or-later

# See also: https://www.altlinux.org/Rescue/Launcher#HTTP
export AUTORUN_HTTPBASE="http://ftp.altlinux.org/pub/people/klark/deploy/example"

# For debugging only
export SYSREST_DEBUG=1

# From USB-stick or from NFS-server
if [ -z "${AUTORUN_HTTPBASE-}" ]; then
	rpm -q system-restore &>/dev/null ||
		apt-get install -y -- ./bash-builtin-lockf-*.rpm ./system-restore-*.rpm
	exec /usr/bin/system-restore --no-pci --deploy --poweroff
fi

# Download and install the dependencies
if ! rpm -q system-restore &>/dev/null; then
	url="$AUTORUN_HTTPBASE/PACKAGES"
	pkgs="$(curl --no-buffer --connect-timeout 5 --max-redirs 2 -- "$url")"

	for url in $pkgs; do
		apt-get install -y -- "$AUTORUN_HTTPBASE/$url"
		sleep 2
	done

	unset url pkgs
fi

# From the HTTP-server
exec /usr/bin/system-restore --backup="$AUTORUN_HTTPBASE" --no-pci --deploy --poweroff

