#!/bin/bash

t150s_hookdirs="/lib/dracut/hooks"
t150s_prefix="${initdir}"

function t150s_strstr() {
    [ "${1##*"$2"*}" != "$1" ]
}

function t150s_str_replace() {
    local in="$1"; local s="$2"; local r="$3"
    local out=''

    while t150s_strstr "${in}" "$s"; do
        chop="${in%%"$s"*}"
        out="${out}${chop}$r"
        in="${in#*"$s"}"
    done
    echo "${out}${in}"
}

function t150s_dev_unit_name()
{
    local dev="$1"

    if [ "$dev" = "/" -o -z "$dev" ]; then
        printf -- "-"
        exit 0
    fi

    dev="${1%%/}"
    dev="${dev##/}"
    dev="$(t150s_str_replace "$dev" '\' '\x5c')"
    dev="$(t150s_str_replace "$dev" '-' '\x2d')"
    dev=${dev/#\./\\x2e}
    dev="$(t150s_str_replace "$dev" '/' '-')"

    printf -- "%s" "$dev"
}

function t150s_set_systemd_timeout_for_dev()
{
    local _name
    local _timeout

    _timeout=${t150s_JobTimeoutSec}

    _name=$(t150s_dev_unit_name "$1")
    if ! [ -L ${t150s_prefix}/etc/systemd/system/initrd.target.wants/${_name}.device ]; then
        [ -d ${t150s_prefix}/etc/systemd/system/initrd.target.wants ] || mkdir -p ${t150s_prefix}/etc/systemd/system/initrd.target.wants
        ln -s ../${_name}.device ${t150s_prefix}/etc/systemd/system/initrd.target.wants/${_name}.device
    fi

    if ! [ -f ${t150s_prefix}/etc/systemd/system/${_name}.device.d/timeout.conf ]; then
        mkdir -p ${t150s_prefix}/etc/systemd/system/${_name}.device.d
        {
            echo "[Unit]"
            echo "JobTimeoutSec=$_timeout"
        } > ${t150s_prefix}/etc/systemd/system/${_name}.device.d/timeout.conf
    fi
}

function t150s_wait_for_dev()
{
    local _name

    _name="$(t150s_str_replace "$1" '/' '\x2f')"

    [ -e "${t150s_prefix}${t150s_hookdirs}/initqueue/finished/devexists-${_name}.sh" ] && return 0

    printf '[ -e "%s" ]\n' $1 \
        >> "${t150s_prefix}${t150s_hookdirs}/initqueue/finished/devexists-${_name}.sh"
    {
        printf '[ -e "%s" ] || ' $1
        printf 'warn "\"%s\" does not exist"\n' $1
    } >> "${t150s_prefix}${t150s_hookdirs}/emergency/80-${_name}.sh"

    t150s_set_systemd_timeout_for_dev $1
}

function t150s_cancel_wait_for_dev()
{
    local _name
    _name="$(t150s_str_replace "$1" '/' '\x2f')"
    
    if [ -f ${t150s_prefix}/etc/systemd/system/${_name}.device.d/timeout.conf ]; then
        t150s_JobTimeoutSec=`cat ${t150s_prefix}/etc/systemd/system/${_name}.device.d/timeout.conf | grep JobTimeoutSec | awk -F "=" '{print $2}'`
    fi
    
    rm -f -- "${t150s_prefix}${t150s_hookdirs}/initqueue/finished/devexists-${_name}.sh"
    rm -f -- "${t150s_prefix}${t150s_hookdirs}/emergency/80-${_name}.sh"
    _name=$(t150s_dev_unit_name "$1")
    rm -rf ${t150s_prefix}/etc/systemd/system/initrd.target.wants/${_name}.device
    rm -rf ${t150s_prefix}/etc/systemd/system/${_name}.device.d/timeout.conf
	rm -rf ${t150s_prefix}/etc/systemd/system/${_name}.device.d
}

function t150s_convert_bypath_to_byuuid()
{
	for path in `ls ${t150s_prefix}${t150s_hookdirs}/initqueue/finished/ | grep -E "pci.*fc"`
	do
		_path=${path}
		_path=${_path//\\x2f//}
		_path=${_path#*-}
		_path=${_path%.sh*}
		_dev=$(readlink -f ${_path})
		_uuid=$(blkid ${_dev})
		_uuid=${_uuid#*UUID=\"}
		_uuid=${_uuid%%\"*}
		abs_path="/dev/disk/by-uuid/${_uuid}"
		path=${path//\\x2f//}
		path=${path#devexists-}
		path=${path%.sh}
		t150s_cancel_wait_for_dev ${path}
		t150s_wait_for_dev ${abs_path}
	done
}

if [ `echo ${outfile} | grep -E -c "(/nxup|/ultrapath)"` -ne 0 ]
then
    t150s_convert_bypath_to_byuuid
fi
