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

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

# default html viewer?
if [ -n "${HTML_VIEWER+xxx}" ]; then
    if type "${HTML_VIEWER}" > /dev/null 2>&1; then
        echo "user specified \$HTML_VIEWER: ${HTML_VIEWER}"
    else
        echo "user specified \$HTML_VIEWER: ${HTML_VIEWER} is not executable"
        HTML_VIEWER=""	# this allows questions w/ figs to be supressed
    fi
else
    HTML_VIEWER="xdg-open"	# find default browser
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" | "6" | "7" | "7R" | "8" | "9" ) ;;

	"T" | "GROL" | "MROP" | "GMDSS" | "GMDSSM" | "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.8";
	    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 "    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 "    T  Radiotelegraph: elements 1, 6.";
	    echo "    GROL General Radiotelephone Operator License + Radar Endorsement: 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 "    RGMDSS Restricted GMDSS Radio Operator: elements 1, 7R.";
	    echo "    GMDSSM GMDSS Radio Maintainer: elements 1, 3, 9.";
	    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 "    https://www.fcc.gov/wireless/bureau-divisions/mobility-division/commercial-radio-operator-license-program/examinations";
	    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/* .;;
	"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/* .;;
	"T" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 6";
	    ln -s "${INSTALLDIR}"/E6/* .;;
	"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/* .;;
	"RGMDSS" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 7R";
	    ln -s "${INSTALLDIR}"/E7R/* .;;
	"GMDSSM" )
	    echo "reloading element 1";
	    ln -s "${INSTALLDIR}"/E1/* .;
	    echo "reloading element 3";
	    ln -s "${INSTALLDIR}"/E3/* .;
	    echo "reloading element 9";
	    ln -s "${INSTALLDIR}"/E9/* .;;
	"" | * ) 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 questions are present, display figures
for i in 3 8; do
    ls ${USERDIR}/${i}* >> /dev/null 2>&1
    if [ $? -eq 0 ]; then
	# no HTML_VIEWER available?
	if [ "${HTML_VIEWER}" = "" ]; then
	    echo "no HTML_VIEWER to display element ${i} figures"
	else
	    # bring up diagrams for this element
	    ${HTML_VIEWER} ${INSTALLDIR}/E${i}.html 2>/dev/null &
	    if [ $? -ne 0 ]; then
		echo "${HTML_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 "${HTML_VIEWER}" ]; then
            # question references figure?
            egrep '[Ff]igure ' "${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 "${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
