#!/bin/bash

n=0
while read v1 v2; do
  if [ -n $v1 -a -n $v2 ]; then
    n=$(($n+1))
    keys[$n]=$v1
    vals[$n]=$v2
    ok[$n]=0
  fi
done < "$1"

left=$n
until [ $left -eq 0 ]; do
    this=$((($RANDOM%$n)+1))
    if [ ${ok[$this]} -eq 1 ]; then continue; fi
    read -p "${keys[$this]}: " ans

    it=`echo ${vals[$this]} | tr [:upper:] [:lower:]`
    ans=`echo $ans | tr [:upper:] [:lower:]`
    if [ "$ans" == "qq" ]; then exit
    elif [ "$ans" == "$it" ]; then
      echo "Correct."
      ok[$this]=1
      left=$(($left-1))
    else
      echo "Incorrect: ${vals[$this]}"
    fi
done
echo "Congratulations."
