#!/bin/sh
# study FCC commercial exam question pools
# Copyright (C) 2012-2013 John Nogatch <jnogatch@gmail>

# 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=~/.fccexam

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

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

# if no questions remain...
ls ${USERDIR}/* >> /dev/null 2>&1
if [ $? -ne 0 ]; then
    # check argument for various license names, default to showing usage, version, brief help
    POOL=`echo $1 | tr [:lower:] [:upper:]`
    case "${POOL}" in
	"1" | "3" | "5" | "6" | "7" | "7R" | "8" | "9" ) ;;

	"T1" | "T2" | "T3" | "GROL" | "GROL+" | "MROP" | "GMDSS" | "GMDSS+" | "RGMDSS" ) ;;

	"Q" | *) echo "usage: fccexam {1|3|5|6|7|7R|8|9|T1|T2|T3|GROL|GROL+|MROP|GMDSS|GMDSS+|RGMDSS}";
	    echo "fccexam version 1.0.5";
	    echo "fccexam is an interactive study guide for USA FCC commercial radio examinations.";
	    echo "question pool choices:";
	    echo "    1  Basic radio law and operating practice"
	    echo "    3  Electronics fundamentals and techniques of radio transmitters and receivers"
	    echo "    5  Radiotelegraph Operating Procedure"
	    echo "    6  Advanced Radiotelegraph"
	    echo "    7  GMDSS Radio Operating Practices"
	    echo "    7R Restricted GMDSS Radio Operating Practices"
	    echo "    8  Ship Radar Techniques"
	    echo "    9  GMDSS Radio Maintenance Practices and Procedures"
	    echo "    T1 First Class Radiotelegraph: elements 1, 5, 6.";
	    echo "    T2 Second Class Radiotelegraph: elements 1, 5, 6.";
	    echo "    T3 Third Class Radiotelegraph: elements 1, 5.";
	    echo "    GROL General Radiotelephone Operator License: elements 1, 3.";
	    echo "    GROL+ General Radiotelephone Operator License + Radar: elements 1, 3, 8.";
	    echo "    MROP Marine Radio Operator Permit: element 1.";
	    echo "    GMDSS Global Maritime Distress Safety System Radio Operator: elements 1, 7.";
	    echo "    GMDSS+ GMDSS Radio Maintainer + Radar: elements 1, 7, 8, 9.";
	    echo "    RGMDSS Restricted GMDSS Radio Operator: elements 1, 7R.";
	    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 COLEM Examiners."
	    echo "For more information about FCC commercial radio licensing:"
	    echo "    http://wireless.fcc.gov/commoperators/index.htm?job=home";
	    if [ "${POOL}" != "Q" ]; then
		sleep 3
		exit 1;
	    fi;
	    POOL="Q"
	    while [ $POOL = "Q" ]:; do
		# read -p "Which pool? ..." POOL;   (Debian: read with option other than -r)
		echo -n "Which pool? {1,3,5,6,7,7R,8,9,T1,T2,T3,GROL,GROL+,MROP,GMDSS,GMDSS+,RGMDSS} " ;
		read  POOL;
		POOL=`echo ${POOL} | tr [:lower:] [:upper:]`
		case "$POOL" in
		    "1" | "3" | "5" | "6" | "7" | "7R" | "8" | "9" ) ;;
		    "T1" | "T2" | "T3" | "GROL" | "GROL+" | "MROP" | "GMDSS" | "GMDSS+" | "RGMDSS" ) ;;
		    * ) POOL="Q";;
		esac;
	    done
    esac

    # load the questions
    case ${POOL} in
	"1" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;;
	"3" )
	    echo "reloading element 3";
	    ln -s "${INSTALLDIR}"/E3/* .;;
	"5" )
	    echo "reloading element 5";
	    ln -s "${INSTALLDIR}"/E5/* .;;
	"6" )
	    echo "reloading element 6";
	    ln -s "${INSTALLDIR}"/E6/* .;;
	"7" )
	    echo "reloading element 7";
	    ln -s "${INSTALLDIR}"/E7/* .;;
	"7R" )
	    echo "reloading element 7R";
	    ln -s "${INSTALLDIR}"/E7R/* .;;
	"8" )
	    echo "reloading element 8";
	    ln -s "${INSTALLDIR}"/E8/* .;;
	"9" )
	    echo "reloading element 9";
	    ln -s "${INSTALLDIR}"/E9/* .;;
	"T1" | "T2" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 5";
	    ln -s "${INSTALLDIR}"/E5/* .;
	    echo "reloading element 6";
	    ln -s "${INSTALLDIR}"/E6/* .;;
	"T3" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 5";
	    ln -s "${INSTALLDIR}"/E5/* .;;
	"GROL" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 3";
	    ln -s "${INSTALLDIR}"/E3/* .;;
	"GROL+" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 3";
	    ln -s "${INSTALLDIR}"/E3/* .;
	    echo "reloading element 8";
	    ln -s "${INSTALLDIR}"/E8/* .;;
	"MROP" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;;
	"GMDSS" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 7";
	    ln -s "${INSTALLDIR}"/E7/* .;;
	"GMDSS+" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 7";
	    ln -s "${INSTALLDIR}"/E7/* .;
	    echo "reloading element 8";
	    ln -s "${INSTALLDIR}"/E8/* .;
	    echo "reloading element 9";
	    ln -s "${INSTALLDIR}"/E9/* .;;
	"RGMDSS" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 7R";
	    ln -s "${INSTALLDIR}"/E7R/* .;;
	"" | * ) echo "POOL variable was not set correctly";
	    sleep 3;
	    exit 1;;
    esac
else
    echo "resuming remaining questions ( to discard previous session, rm ~/.fccexam/* )"
fi

# if element 3, 8, 9 questions are present, display figures
for i in 3 8 9; do
    ls ${USERDIR}/${i}* >> /dev/null 2>&1
    if [ $? -eq 0 ]; then
	# no PNG_VIEWER available?
	if [ "${PNG_VIEWER}" = "" ]; then
	    echo "no PNG_VIEWER to display element ${i} figures"
	else
	    # bring up diagrams for this element
	    ${PNG_VIEWER} ${INSTALLDIR}/E${i}png/*.png 2>/dev/null &
	    if [ $? -ne 0 ]; then
		echo "${PNG_VIEWER} failed to display element ${i} figures"
	    fi
	fi
    fi
done

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

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

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

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

	# ...ask the question
	awk -f "${INSTALLDIR}"/ask.awk "${Q}"
    else
	echo "question ${Q} is 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 fccexam
