#!@awk@ -f

function sleep (secs){
	if (!nosleep && 0 != system("sleep " secs)){
		exit 10
	}
}

function my_rnd (){
	if (rand() < threshold)
		return -1 # return unusual value for exiting with error
	else
		return rand()
}

BEGIN {
	threshold = ARGV [1] + 0.0
	fake_cmd  = ARGV [2]

	nosleep   = ARGV [1] ~ /ns/
	nopostfail = ARGV [1] ~ /nopostfail/

	ARGV [1] = "-"
	ARGV [2] = ""

	srand()

	# FreeBSD's /usr/bin/awk generates very bad first random value.
	# This is why we run rand() twice.
	rand()
}

{
	rnd = my_rnd()
	if (rnd < 0.0) {
		sleep(0.1)
		exit 1
	}

	sleep(rnd)

	print " " $0

	rnd = my_rnd()
	if (!nopostfail && rnd < 0.0) {
		sleep(0.1)
		exit 1
	}

	print "success"
	print ""
	fflush()
}
