#!/bin/bash
# A quick and dirty flashcard script, for learning Toki Pona vocab
# open to all for anything, [email protected]
#
# create a text file with one entry per line, word:definition
# the script will randomly choose a word and give you multiple
# options. Choose the correct one; the script lets you know if
# you got it right or not, and shows you the correct answer.
#
# color is optionally disabled with -c at the end

if [ "$#" -lt "1" ]; then
       echo "Usage: flashcard.sh <filename.txt> [-c]"
       exit 1
fi
if [ ! -f "$1" ]; then
       echo "The file \"$1\" does not exist."
       exit 1
fi
if [ "$2" != "-c" ]; then
       color_yellow='\033[1;33m'
       color_green='\033[0;32m'
       color_cyan='\033[0;36m'
       color_red='\033[0;31m'
       color_nc='\033[0m'
fi

readarray flashcards < $1
lastcard=$((${#flashcards[@]}-1))

while [ "$guess" != "q" ]; do
       multichoice=($(shuf -i 0-119 -n 4))
       choiceorder=($(shuf -i 0-3 -n 4))

       IFS=":" read -ra testcard <<< "${flashcards[${multichoice[0]}]}"

       echo -e "\n [${color_yellow}${testcard[0]}${color_nc}] \n"

       c=0
       for i in "${choiceorder[@]}"; do
               IFS=":" read -ra mixcard <<< "${flashcards[${multichoice[$i]}]}"
               echo "$c) ${mixcard[1]}"
               if [ "${testcard[1]}" == "${mixcard[1]}" ]; then
                       answer="$c"
               fi
               ((c++))
       done

       echo -n "Answer:"
       read -n1 guess

       if [ "$answer" == "$guess" ]; then
               echo -e "\n${color_green}Correct.${color_nc}"
       else
               echo -e "\n${color_red}Incorrect.${color_nc}"
       fi
       echo -e "${color_cyan}${testcard[0]} : ${testcard[1]}${color_nc}"
done