#!/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_cdrom-}" ]; then
__included_rescue_launcher_cdrom=1

# Additional CD-check
autorun_check_cdrom() {
	local rootdir="$1" dev= tag=

	autorun_spawn test "$rootdir" != "/" || return 1
	autorun_spawn test "$rootdir" != "$AUTORUN_MPOINT" || return 1
	autorun_spawn test -x "$rootdir/$AUTORUN_RUN" || return 1

	if [ -n "$AUTORUN_UUID" -o -n "$AUTORUN_LABEL" ]; then
		dev="$(grep ' /image iso9660 ro,' /proc/mounts | cut -f1 -d ' ')"
		autorun_spawn test -n "$dev" -a -b "$dev" || return 1
	fi
	if [ -n "$AUTORUN_UUID" ]; then
		tag="$(blkid -c /dev/null -o value -s UUID $dev 2>/dev/null ||:)"
		autorun_spawn test "$AUTORUN_UUID" = "$tag" || return 1
	fi
	if [ -n "$AUTORUN_LABEL" ]; then
		tag="$(blkid -c /dev/null -o value -s LABEL $dev 2>/dev/null ||:)"
		autorun_spawn test "$AUTORUN_LABEL" = "$tag" || return 1
	fi

	autorun_spawn mount --bind "$rootdir" "$AUTORUN_MPOINT" || return 1

	return 0
}

# Bootable CD-ROM Drive
autorun_probe_cdrom_method() {
	local directory=

	autorun_debug "Probing CDROM..."
	autorun_spawn grep ' /image iso9660 ro,' /proc/mounts || return 1
	directory="$(mktemp -q -u /image/test-XXXXXXXX.tmp)"

	if autorun_spawn mkdir -m700 "$directory" 2>/dev/null; then
		autorun_spawn rmdir "$directory" ||:
	else
		directory="${AUTORUN_DIRECTORY:-/recovery}"
		if autorun_check_cdrom "/image$directory" ||
			autorun_check_cdrom "$directory"
		then
			return 0
		fi
	fi

	return 1
}

fi #__included_rescue_launcher_cdrom
