#!/bin/sh
# study ham exam question pools
# Copyright (C) 2011-2019 John Nogatch AC6SL <jnogatch@gmail.com>

# dir where question pools reside?
INSTALLDIR="$(dirname "$(readlink -f $0)")"

# user specified png viewer?
if [ -n "${PNG_VIEWER+xxx}" ]; then
    if type "${PNG_VIEWER}" > /dev/null 2>&1; then
	echo "user specified PNG_VIEWER: ${PNG_VIEWER}"
    else
	echo "user specified PNG_VIEWER: ${PNG_VIEWER} is not executable"
	PNG_VIEWER=""
    fi
# try eog
elif type "eog" > /dev/null 2>&1; then
    PNG_VIEWER="eog"
# try gwenview
elif type "gwenview" > /dev/null 2>&1; then
    PNG_VIEWER="gwenview"
# try gpicview
elif type "gpicview" > /dev/null 2>&1; then
    PNG_VIEWER="gpicview"
# try ristretto
elif type "ristretto" > /dev/null 2>&1; then
    PNG_VIEWER="ristretto"
else
    echo "PNG_VIEWER was not specifed nor found"
    PNG_VIEWER=""
fi

# dir to keep user's unanswered questions
USERDIR=~/.hamexam

# create user dir, if needed
if [ ! -d "${USERDIR}" ]; then
    echo "creating ${USERDIR}"
    mkdir "${USERDIR}"
    if [ $? -ne 0 ]; then
	echo "unable to create ~/.hamexam directory"
	sleep 3
	exit 1
    fi
fi

# check argument for T, G, or E, default to showing usage, version, brief help
# set POOL within set {T,G,E}
case "$1" in

    "2" | [tT]* ) POOL="T";;
    "3" | [gG]* ) POOL="G";;
    "4" | [eE]* ) POOL="E";;

    "q" | *) echo "usage: hamexam {t|g|e|q}";
	echo "hamexam version 1.7.0";
	echo "Technician, Element 2 effective July-2018 until July-2022";
	echo "General, Element 3 effective July-2019 until July-2023";
	echo "Extra, Element 4 effective July-2016 until July-2020";
	echo "hamexam is an interactive study guide for USA FCC amateur radio (ham radio) examinations.";
	echo "The 3 question pools are:";
	echo "    t element 2, Technician Class (entry level),";
	echo "    g element 3, General Class (also requires element 2),";
	echo "    e element 4, Extra Class (also requires elements 2 and 3).";
	echo "Questions are chosen randomly from the selected pool.";
	echo "Incorrect answers cause the question to be asked again later.";
	echo "Licenses are issued by the FCC, but exams are conducted by Volunteer Examiners."
	echo "For more information about USA amateur radio licensing: http://www.arrl.org/licensing-preparation-exams";
	if [ "$1" != "q" ]; then
	    sleep 3
	    exit 1;
	fi;
	POOL="Q"
	while [ $POOL = "Q" ]:; do
	    # read -p "Which pool? {t,g,e} " POOL;  (Debian: read with option other than -r)
	    echo -n "Which pool? {t,g,e}: ";
	    read POOL;
	    case "$POOL" in
		"2" | [tT]* ) POOL="T";;
		"3" | [gG]* ) POOL="G";;
		"4" | [eE]* ) POOL="E";;
		* ) POOL="Q";;
	    esac;
	done
esac

# go to user dir so that question names are accessible
cd ${USERDIR}
if [ $? -ne 0 ]; then
    echo "unable to access ~/.hamexam directory"
    sleep 3
    exit 1
fi

# if no questions remain, reload all the questions
ls ${POOL}* >> /dev/null 2>&1
if [ $? -ne 0 ]; then
    case ${POOL} in
	"T" ) echo "reloading Technicican Class element 2";;
	"G" ) echo "reloading General Class element 3";;
	"E" ) echo "reloading Extra Class element 4";;
	"" | * ) echo "POOL variable was not set correctly";
	    sleep 3;
	    exit 1;;
    esac
    ls "${INSTALLDIR}"/${POOL} | xargs touch
else
    echo "resuming remaining questions"
fi

# no PNG_VIEWER available?
if [ "${PNG_VIEWER}" = "" ]; then
    echo "no PNG_VIEWER to display diagrams"
else
    # bring up diagrams for this element
    ${PNG_VIEWER} ${INSTALLDIR}/${POOL}pngs/${POOL}1.png 2>/dev/null &
    if [ $? -ne 0 ]; then
	echo "${PNG_VIEWER} failed to display diagrams"
    fi
fi

# report how many questions remain
ls ${POOL}* | wc | awk '{print $1 " questions remain in this pool\n"}'

while :; do
    # random choice from remaining questions
    Q=`shuf -n 1 -e ${POOL}*`

    # exit if no more questions
    if [ "${Q}" = "${POOL}*" ]; then
	echo "question pool ${POOL} completed"
	sleep 3
	break
    fi

    # if question still exists (might be stale name from old pool)...
    if [ -e "${INSTALLDIR}"/${POOL}/"${Q}" ]; then
	# no png viewer?
	if [ -z "${PNG_VIEWER}" ]; then
	    # question references figure?
	    egrep '[Ff]igure ' "${INSTALLDIR}"/${POOL}/"${Q}" >> /dev/null
	    if [ $? = 0 ]; then
		echo "question ${Q} refers to a figure"
		rm "${Q}"
		continue
	    fi
	fi

	# ...ask the question
	awk -f "${INSTALLDIR}"/ask.awk "${INSTALLDIR}"/${POOL}/"${Q}"
    else
	echo "question ${Q} not found"
    fi

    case $? in
	# if correct, remove the question
	0) rm "${Q}";;

	# check for quit or EOF
	2) break;;
    esac
done

exit 0

# end of hamexam
