#!/bin/sh -ef
# NAME=ODD read
# DESCRIPTION=Optical Disc Drive read test
# DESTROYS_HDD=false
# IS_INTERACTIVE=true
# POWEROFF_DURING_TEST=false
# VERSION=0.1
# TAGS=odd
# VAR=TEST_IMAGE_HASH:string:6fa7786eef2e11d36e8bc1663679f161:Default image for comparison hash
# VAR=TEST_IMAGE_BLOCKS:int:332800:This images size in blocks (2048 bytes each)
# VAR=MESH_POINTS:int:1024:Points for meshes for monitoring drive's speed
# VAR=FORCE_NON_INTERACTIVE:boolean:false:Force non-interactive mode for already prepared system

. /usr/share/inquisitor/functions-test

exit_handler()
{
	[ -f "$IMAGE" ] && rm -f $IMAGE
	[ -f "$RESULT_FILE" ] && rm -f $RESULT_FILE
}

IMAGE=`mktemp`
RESULT_FILE=`mktemp`

load_kernel_modules()
{
	modprobe ide-cd || true
	sleep 5
}

test_odd()
{
	local odd=$1

	readcd -v dev=$odd f=$IMAGE sectors=0-$TEST_IMAGE_BLOCKS \
		meshpoints=$MESH_POINTS >$RESULT_FILE || true
	[ ! -s "$IMAGE" ] && dd if=$odd of=$IMAGE bs=2048 \
		count=$TEST_IMAGE_BLOCKS >$DEBUG_TTY 2>&1 || true

	[ `md5sum -b < $IMAGE | awk '{print $1}'` = $TEST_IMAGE_HASH ] \
		&& return 0 || return 1
}

interactive()
{
	local odd=$1
	local non_passed=1

	while ( [ "$non_passed" -eq 1 ] ); do
		eject $odd || true
		require_attention
		print_green_message "Drive ${odd}:"
		print_green_message "Either press y and insert media for testing, or n if you want to finish test: "
		read choice
		dismiss_attention

		if [ "$choice" = "n" ]; then
			test_failed "User cancelled test for $odd"
			break
		fi

		if test_odd $odd; then
			print_green_message "Drive test passed"
			TESTED_QUANTITY=$(( $TESTED_QUANTITY + 1 ))
			test_progress $TESTED_QUANTITY $ODD_QUANTITY
			non_passed=0

			generate_speed_results
			eject $odd || true
		else
			print_red_message "Drive test failed"
		fi
	done
}

generate_speed_results()
{
	RESULTS=`perl -e '@l=map{split}<>;print join ",",@l' $RESULT_FILE`

	#here will be call to publish this monitoring results
}

load_kernel_modules

ODD_QUANTITY=`get_odds_list | wc -l`
if [ "$ODD_QUANTITY" -eq 0 ]; then
	test_succeeded "No ODDs found"
	exit 0
fi

TESTED_QUANTITY=0

for odd in `get_odds_list`; do
	print_green_message "Testing drive ${odd}..."

	if [ "$FORCE_NON_INTERACTIVE" == "true" ]; then
		# Forced non-interactive mode, no user actions allowed here
		if test_odd $odd; then
			TESTED_QUANTITY=$(( $TESTED_QUANTITY + 1 ))
			test_progress $TESTED_QUANTITY $ODD_QUANTITY
			generate_speed_results
			eject $odd || true
		else
			test_failed "Bad ODD $odd"
		fi
	else
		# Default mode, try to test drive if media present
		if [ `check_readable_odd $odd` == "true" ];then
			print_green_message \
				"Found inserted media. Trying to test..."
			if test_odd $odd; then
				print_green_message "Drive test passed"
				TESTED_QUANTITY=$(( $TESTED_QUANTITY + 1 ))
				test_progress $TESTED_QUANTITY $ODD_QUANTITY
				eject $odd || true
			else
				print_red_message \
				"Drive test failed, going to interactive mode"
				interactive $odd
			fi
		else
			interactive $odd
		fi
	fi
done

test_succeeded
