#!/bin/bash

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

omit_pid() {
	: > "/.initrd/killall/$1"
}

devlinks_check() {
	local l
	for l in ${DEVLINKS-}; do
		[ "$1" != "$l" ] ||
			return 0
	done
	return 1
}

get_majmin() {
	local v
	v="$(stat -L -c '%02t:%02T' "$1" 2>/dev/null)" &&
		printf '%d:%d\n' "0x${v%:*}" "0x${v#*:}" ||
		return 1
}

get_dev() {
	local name retval value

	if [ "$#" = 2 ]; then
		retval="$1"; shift
	fi
	name="$1"; shift

	case "$name" in
		'')
			return 1
			;;
		UUID=*)
			[ "${ID_FS_UUID-}" = "${name#UUID=}" ] ||
				return 1
			;;
		LABEL=*)
			[ "${ID_FS_LABEL-}" = "${name#LABEL=}" ] ||
				return 1
			;;
		PARTUUID=*)
			[ "${ID_PART_ENTRY_UUID-}" = "${name#PARTUUID=}" ] ||
				return 1
			;;
		PARTLABEL=*)
			[ "${ID_PART_ENTRY_NAME-}" = "${name#PARTLABEL=}" ] ||
				return 1
			;;
		SERIAL=*)
			[ "${ID_SERIAL-}" = "${name#SERIAL=}" ] ||
				return 1
			;;
		/dev/disk/by-uuid/*)
			[ "/dev/disk/by-uuid/${ID_FS_UUID_ENC-}" = "$name" ] ||
				return 1
			;;
		/dev/disk/by-label/*)
			[ "/dev/disk/by-label/${ID_FS_LABEL_ENC-}" = "$name" ] ||
				return 1
			;;
		/dev/disk/by-partuuid/*)
			[ "/dev/disk/by-partuuid/${ID_PART_ENTRY_UUID-}" = "$name" ] ||
				return 1
			;;
		/dev/disk/by-partlabel/*)
			[ "/dev/disk/by-label/${ID_PART_ENTRY_NAME-}" = "$name" ] ||
				return 1
			;;
		/dev/block/[0-9]*:[0-9]*)
			[ "/dev/block/${MAJOR-}:${MINOR-}" = "$name" ] ||
				return 1
			;;
		/*)
			[ "${DEVNAME-}" = "$name" ] || devlinks_check "$name" ||
				return 1
			;;
		*:*)
			[ "${MAJOR-}" = "${name%:*}" ] && [ "${MINOR-}" = "${name#*:}" ] ||
				return 1
			;;
		*)
			( _=$(( 0x$name )) ) 2>/dev/null ||
				return 1
			value=$(( 0x$name ))
			[ "${MAJOR-}" = "$(( $value / 256 ))" ] && [ "${MINOR-}" = "$(( $value % 256 ))" ] ||
				return 1
			;;
	esac
	[ -n "${DEVNAME-}" ] ||
		return 1
	[ -z "$retval" ] ||
		eval "$retval=\"\$DEVNAME\""
}

readline()
{
        local __v=
        # shellcheck disable=SC2162
        read __v < "$2" ||:
        eval "$1=\"\$__v\""
}

_rootdelay_pause=/.initrd/rootdelay/pause
_rootdelay_timestamp=/.initrd/rootdelay/deadline

rootdelay_pause()
{
	mkdir -p "$_rootdelay_pause"
}

rootdelay_unpause()
{
	rm -rf "$_rootdelay_pause"
}

rootdelay_paused()
{
	[ -d "$_rootdelay_pause" ]
}

rootdelay_reset_timer()
{
	local now
	now="$(timestamp boottime raw)"
	now="${now%.*}"
	echo $(( $now + $ROOTDELAY )) > "$_rootdelay_timestamp"
}

initrd_features=/.initrd/features

has_feature()
{
	while [ "$#" -gt 0 ]; do
		[ -d "$initrd_features/$1" ] ||
			return 1
		shift
	done
}

_resume_checked=/.initrd/resume/checked

set_resume_checked()
{
	mkdir -p -- "$_resume_checked"
}

resume_checked()
{
	[ -n "${NORESUME-}" ] || [ -z "${RESUME-}" ] || [ -e "$_resume_checked" ] ||
		return 1
}

fi
