#!/bin/bash

mkdir -p out

scripts=""
options=""
abort_if_fail=true
while [ $# -gt 0 ]; do
	opt=$1; shift
	case $opt in
	--continue)
		abort_if_fail=false;;
	--reader)
		options="$options $opt $1"
		shift;;
	-*)	options="$options $opt";;
	*)	scripts="$scripts $opt";;
	esac
done

if [ -z "$scripts" ]; then
	scripts=`ls init* crypt* pin*`
fi

for script in $scripts; do
	echo -n "${script}... "
	mkdir -p test-data
	if ./$script $options >out/$script 2>&1; then
		echo "success"
	else
		mkdir -p failed
		failed="failed/$script"
		mv test-data $failed
		cp out/$script $failed/test.log
		echo "fail (test data moved to $failed)"
		if $abort_if_fail; then
			echo Aborting.
			exit 1
		fi
		echo -n "Wiping card... "
		if ./erase $options >out/erase 2>&1; then
			echo done
		else
			echo failed.
			exit 1
		fi
	fi
done

exit 0
