#!/bin/bash -efu

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

. /.initrd/initenv
. uevent-sh-functions

handlerdir="/lib/pipeline"
mntdir="/dev/pipeline"

check_parameter()
{
	local v
	eval "v=\"\${$1-}\""
	[ -n "$v" ] ||
		fatal "Parameter '$1' required"
}

get_parameter()
{
	eval "printf '%s' \"\${${1}$callnum-}\""
}

resolve_target()
{
	local target="$1"
	case "${target%%/*}" in
		'')
			;;
		pipe[0-9]|pipe[0-9][0-9]|pipe[0-9][0-9][0-9])
			target="$mntdir/dst/$target"
			;;
		*)
			if [ -z "${prevdir-}" ]; then
				message "no previous stop to use"
				return
			fi
			target="$prevdir/${target#/}"
			;;
	esac
	printf '%s' "$target"
}

run()
{
	[ -z "${DEBUG-}" ] || message "[$callnum] RUN: $*"
	"$@"
}

pipe_name()
{

	local -a arr

	IFS="," read -r -a arr <<<"$PIPELINE"

	[ "$1" -lt "${#arr[@]}" ] ||
		return 0

	local n="$1"

	set -- "${arr[@]}"
	shift $n

	eval "name=\"\$1\""
}

pipe_callnum()
{
	local -a arr

	IFS="," read -r -a arr <<<"$PIPELINE"

	[ "$1" -lt "${#arr[@]}" ] ||
		return 0

	local n="$1"

	set -- "${arr[@]}"
	shift $n

	local i num=0
	for (( i=0; $i < $n; i+=1 )); do
		[ "$1" != "${arr[$i]}" ] || num=$(( $num + 1 ))
	done

	eval "callnum=\"\$num\""
}

pipe_init()
{
	local event
	event="$(make_event pipeline)"
	release_event "pipeline.init" "$event"
}

pipe_event()
{
	local event

	message "create event: $1${2:+ move #$2} -> step #$3"

	event="$(make_event pipeline)"
	printf > "$event" '%s\n' \
		"prevnum=$2" \
		"pipenum=$3"

	release_event "$1" "$event"
}

pipe_failed()
{
	[ -n "${PIPE_RETRY:-}" ] ||
		return 0

	local failed fn

	failed=0
	fn="/.initrd/pipeline/step-failed/$callnum"

	[ ! -f "$fn" ] || read -r failed < "$fn"
	failed="$(( $failed + 1 ))"

	echo "$failed" > "$fn"

	[ "$failed" -le "${PIPE_RETRY:-}" ]
}

fi # __pipeline_sh_functions
