#!/bin/sh
# init


function pause(){
   read -p "$*"
}

function asktest(){
   echo "Specify test to do [DIST,P11,all]" 
   while [ "$n"="" ]
   do
       read n
       case $n in
            "DIST") TESTSUITE=$n;
		return;;
            "P11") TESTSUITE=$n;
		return;;
            "all") TESTSUITE=$n;
		return;;
            *) echo "Invalid option $n, please select one of this: [fnmt, fnmtdnie, dnie, tif, all]";
       esac
   done

}

function dotest(){
	case $TESTSUITE in
		"all") 
			echo "Test P11 on $1, Fedora";
			test_code "P11" $1;
			echo "Test DIST on $1, Fedora";
	       	 	test_code "DIST" $1;;
		"P11")
			echo "Test P11 on $1, Fedora"
			test_code "P11" $1;;
		"DIST") 
			echo "Test DIST on $1, Fedora"
			test_code "DIST" $1;;
	esac
}

function askdist(){
   echo "Specify which test to do [fnmt, fnmtdnie, dnie, tif, all]" 
   while [ "$n"="" ]
   do
       read n
       case $n in
            "fnmt") CFGDIST=$n;
		return ;;
            "fnmtdnie") CFGDIST=$n;
		return;;
            "dnie") CFGDIST=$n;
		return;;
            "tif") CFGDIST=$n;
		return;;
            "all") CFGDIST=$n;
		return;;
            *) echo "Invalid option $n, please select one of this: [fnmt, fnmtdnie, dnie, tif, all]";;
        esac
    done
 
}

function test_code(){

SHORTNAMETEST=libpkcs11-$2
NAMETEST=$SHORTNAMETEST-$CFGMAJOR.$CFGMINOR.$CFGFIX
   pause "Going to do $1 test, please insert $2 card"
   if [ "$1" = "DIST" ]; then
       $BUILDDIR/$NAMETEST/test_DIST/$SHORTNAMETEST-test-dist -a -v /usr/lib/$SHORTNAMETEST.so > log_$NAMETEST.DIST.log
       cat log_$NAMETEST.DIST.log
   elif [ "$1" = "P11" ]; then
       read -s -p "Please enter password: " password
       $BUILDDIR/$NAMETEST/test_P11/$SHORTNAMETEST-test-p11 -sl 0 -p $password -a -o -s /usr/lib/$SHORTNAMETEST.so > log_$NAMETEST.P11.log
       cat log_$NAMETEST.P11.log
   else
       echo "Invalid type of test." 
   fi 
}

if [[ $# -ne 4 ]]; then
  echo Bad parameters [MAJOR] [MINOR] [FIX] [BUILDDIR]
  echo BUILDIR where the test are.
  exit 1
fi

CFGMAJOR=$1
CFGMINOR=$2
CFGFIX=$3
BUILDDIR=$4
TESTSUITE="all"
askdist
SHORTNAMETEST=libpkcs11-$CFGDIST
NAMETEST=$SHORTNAMETEST-$CFGMAJOR.$CFGMINOR.$CFGFIX
case $CFGDIST in
	"all")
		echo "Test Suite to All Platforms please change card when required";
		asktest;
		dotest "fnmt";
		dotest "fnmtdnie";
		dotest "dnie";
		dotest "tif";;
	*)
		asktest
		dotest $CFGDIST;;
esac
