is_quiet() {
    return ${QUIET}
}

log_msg() {
    if [ ! -f "${INIT_LOG}" ]
    then
        touch "${INIT_LOG}" 2>/dev/null || return
    fi
    local msg=${1}
    LANG=C echo "] ${msg}" | sed \
        -e "s,[^[:print:]],,g" \
        -e 's,\(\\033\)\?\[[0-9;]\+m,,g' \
        | ts '[ %Y-%m-%d %H:%M:%.S' >> "${INIT_LOG}"
}

is_true() {
    case "${1}" in
        1)
            return 0
        ;;
        [Tt][Rr][Uu][Ee])
            return 0
        ;;
        [Tt])
            return 0
        ;;
        [Yy][Ee][Ss])
            return 0
        ;;
        [Yy])
            return 0
        ;;
    esac
    return 1
}

good_msg() {
    local msg_string=${1}
    msg_string="${msg_string:-...}"
    log_msg "[OK] ${msg_string}"
    is_true "${2-${QUIET}}" || printf "%b\n" "${GOOD}>>${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
}

good_msg_n() {
    local msg_string=${1}
    msg_string="${msg_string:-...}"
    log_msg "[OK] ${msg_string}"
    is_true "${2-${QUIET}}" || printf "%b" "${GOOD}>>${NORMAL}${BOLD} ${msg_string}"
}

bad_msg() {
    local msg_string=${1}
    msg_string="${msg_string:-...}"
    log_msg "[!!] ${msg_string}"
    printf "%b\n" "${BAD}!!${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
}

warn_msg() {
    local msg_string=${1}
    msg_string="${msg_string:-...}"
    log_msg "[**] ${msg_string}"
    is_true "${2-${QUIET}}" || printf "%b\n" "${WARN}**${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
}

warn_msg_n() {
    local msg_string=${1}
    msg_string="${msg_string:-...}"
    log_msg "[**] ${msg_string}"
    is_true "${2-${QUIET}}" || printf "%b" "${WARN}**${NORMAL}${BOLD} ${msg_string}"
}

str_starts() {
    if [ "${1#"${2}"*}" != "${1}" ]
    then
        return 0
    fi
    return 1
}

run() {
    local retval
    if "$@"; then
        retval=$?
        log_msg "Executed: '$*'"
    else
        retval=$?
        log_msg "Failed (${retval}): '$*'"
    fi
    return ${retval}
}

devicelist() {
    # Locate the cdrom device with our media on it.
    # USB Keychain/Storage
    DEVICES="${DEVICES} /dev/sd*"
    # IDE devices
    DEVICES="${DEVICES} /dev/hd*"
    # USB using the USB Block Driver
    DEVICES="${DEVICES} /dev/ubd* /dev/ubd/*"
    # iSeries devices
    DEVICES="${DEVICES} /dev/iseries/vcd*"
    # builtin mmc/sd card reader devices
    DEVICES="${DEVICES} /dev/mmcblk* /dev/mmcblk*/*"
    # fallback scanning, this might scan something twice, but it's better than
    # failing to boot.
    [ -e /proc/partitions ] && DEVICES="${DEVICES} $(awk '/([0-9]+[[:space:]]+)/{print "/dev/" $4}' /proc/partitions)"
    echo ${DEVICES}
}

determine_fs() {
    local _dev="${1}"
    local _orig="${2:-auto}"
    local _fs line
    _fs=$(udevadm info --query=env --name="$_dev" 2>/dev/null | \
        while read line || [ -n "${line}" ]
        do
            if str_starts ${line} "ID_FS_TYPE="
            then
                echo ${line#ID_FS_TYPE=}
                break
            fi
        done
    )
    _fs=${_fs:-auto}
    if [ "${_fs}" = "auto" ]
    then
        _fs="${_orig}"
    fi
    echo "${_fs}"
}

findmediamount() {
    # $1 = mount dir name / media name
    # $2 = recognition file
    # $3 = variable to have the device path
    # $4 = actual mount dir path (full path)
    # args remaining are possible devices

    local media=$1 recon=$2 vrbl=$3 mntdir=$4
    shift 4
    good_msg "Looking for the ${media}"
    if [ "$#" -gt "0" ]
    then
        [ ! -d "${mntdir}" ] && mkdir -p ${mntdir} >/dev/null 2>&1
        mntcddir="${mntdir}"
        for x in $*
        do
            # Check for a block device to mount
            if [ -b "${x}" ]
            then
                good_msg "Attempting to mount media: ${x}"
                CDROOT_TYPE=$(determine_fs "${x}" "${CDROOT_TYPE}")
                run mount -t ${CDROOT_TYPE} -o defaults ${x} ${mntcddir} >/dev/null 2>&1
                if [ $? -eq 0 ]
                then
                    # Check for the media
                    if [ -f "${mntdir}/${recon}" ]
                    then
                        #set REAL_ROOT, CRYPT_ROOT_KEYDEV or whatever ${vrbl} is
                        eval ${vrbl}'='"${x}"
                        good_msg "Run FS check on ${x}..."
                        run umount ${mntcddir}
                        run e2fsck -p ${x}
                        run mount -t ${CDROOT_TYPE} -o defaults ${x} ${mntcddir} >/dev/null 2>&1
                        good_msg "Media found on ${x}"
                        break
                    else
                        run umount ${mntcddir}
                    fi
                fi
            fi
        done
    fi
    eval local result='$'${vrbl}
    [ -n "${result}" ] || bad_msg "Media not found"
}

run_shell() {
	[ -x /bin/sh ] && SH=/bin/sh || SH=/bin/ash

	export PS1='rescueshell \w \# '

	echo
	GOOD=${BLUE} good_msg "${NORMAL}Welcome to ${BOLD}rescue shell${NORMAL}!" 0
	GOOD=${BLUE} good_msg "${NORMAL}...running Linux kernel ${BOLD}${KV}${NORMAL}" 0
	echo

	# Avoid /dev/{console,tty0} due to "can't access tty; job control turned off" problem;
	# cttyhack will handle this for us...
	if [ -n "${CONSOLE}" ] \
		&& [ "${CONSOLE}" != "/dev/console" ] \
		&& [ "${CONSOLE}" != "/dev/tty0" ] \
		&& [ -c "${CONSOLE}" ]
	then
		log_msg "Opening rescue shell on ${CONSOLE} ..."
		setsid ${SH} -c "exec sh --login 0<>${CONSOLE} 1<>${CONSOLE} 2<>${CONSOLE}"
	elif command -v cttyhack 1>/dev/null 2>&1
	then
		log_msg "Opening rescue shell using cttyhack ..."
		setsid cttyhack ${SH} --login
	elif [ -c '/dev/tty1' ]
	then
		log_msg "Opening rescue shell on /dev/tty1 fallback ..."
		setsid ${SH} -c "exec sh --login 0<>/dev/tty1 1<>/dev/tty1 2<>/dev/tty1"
	else
		log_msg "Opening rescue shell (last resort) ..."
		${SH} --login
	fi

	# Leave function early when /dev/null does not exist anymore,
	# i.e. after failed switch_root call
	[ ! -e /dev/null ] && return

	echo

	# We maybe have called exec and dettached from main script; We
	# must restore control...
	exec 0<>${CONSOLE} 1<>${CONSOLE} 2<>${CONSOLE}
}

run_emergency_shell() {
	run_shell
}

find_mount() {
    # $1 = mount point
    local mnt_p=$1
    local res
    res=$(cat /proc/self/mountinfo | cut -d' ' -f5 | grep "^${mnt_p}/")
    echo ${res}
}
