#!/bin/sh -efu
### This file is covered by the GNU General Public License
### version 2 or later.
###
### Copyright (C) 2018  ALT Linux Team
### Author: Leonid Krivoshein <klark@altlinux.org>

if [ -z "${__included_rescue_launcher_http-}" ]; then
__included_rescue_launcher_http=1

# Remote WEB-server
autorun_probe_http_method() {
	local baseURI="http://${AUTORUN_SERVER-}${AUTORUN_DIRECTORY-}"
	local binary="$(mktemp -u "$TMPDIR/launch-XXXXXXXX.tmp")"
	local load="$(which curl 2>/dev/null ||:)"

	autorun_debug "Probing HTTP..."
	autorun_spawn test -n "${AUTORUN_SERVER-}" || return 1
	autorun_spawn test -n "${AUTORUN_DIRECTORY-}" || return 1

	if [ -n "$load" -a -x "$load" ]; then
		load="$load --connect-timeout 30 -o"
		# curl arguments
	else
		load="$(which wget 2>/dev/null ||:)"
		test -x "$load" || return 1
		load="$load --timeout=30 -t 3 -O"
		# wget arguments
	fi

	echo
	echo "AUTORUN: loading script from '$baseURI/'..."
	echo

	autorun_debug "Loading script from '$baseURI/'..."
	if autorun_spawn $load "$binary" "$baseURI/$AUTORUN_RUN"; then
		export AUTORUN_HTTPBASE="$baseURI"
		autorun_spawn cd "$AUTORUN_MPOINT/"
		autorun_spawn chmod 755 "$binary"
		autorun_unset_all
		exec "$binary"
		load=$?
		rm -f "$binary"
		export AUTORUN_HTTPBASE=
		unset AUTORUN_HTTPBASE
		exit $load
	fi

	return 1
}

fi #__included_rescue_launcher_http
