# Start with a series of variables.  Not all may be needed in each 
# troubleshooting command, but all are set up in case needed:
#
# presumes the following variables from calling script:
#  cmd  = $(basename $0)
#  type = $cmd stripped of leading ts

TSVER=0.1
TSREL=-1
TSDATA=${TSHINTS:-$TSLIB/data}
TSHINTS=${TSHINTS:-$TSLIB/hints}
TSLESSONS=${TSLESSONS:-$TSLIB/lessons}
TSPROBLEMS=${TSPROBLEMS:-$TSLIB/problems}
TSDOC=${TSDOCUMENTATION:-/usr/share/doc/ts.$TSVER$TSREL}

case $1 in 
	"hint")
		hint=$2
		
		usage() {
			echo -e "$cmd: ERROR\\n" >&2
			echo -e "usage: $cmd hint <hintnum>\\n" >&2
			exit 1
		}
		
		# Two usage tests are performed:
		#	1.  Argument count is 2.
		#	2.  Second argument is single digit numeric.
		#
		[ "$#" = 2 ] || usage
		case "$hint" in
		    [1-9])	true  ;;
		    '')		usage ;;
		esac
		
		# Read in the hints:
		#
		source $TSHINTS/$type
		current="$cmd problem"

		# Give an error if the hint does not exist for the problem:
		#
		[ "$(set | grep hint-$type.$hint)" ] || {
			echo -e "\\nTroubleshooting Hint Error" >&2
			echo -e "Hint number $hint has not been prepared for $current\\n" >&2
			exit 2
		}
		
		# Present the hint:
		#
		echo -e "\\nTroubleshooting Hints"
		echo -e "Hint $hint for $current\\n"
		hint-$type.$hint
	;;
	
	"lesson")
		
		usage() {
			echo -e "$cmd: ERROR\\n" >&2
			echo -e "usage: $cmd lesson\\n" >&2
			exit 1
		}
		
		# 
		source $TSLESSONS/$type
		current="$cmd problem"
		
		[ "$(set | grep lesson-$type)" ] || {
			echo "No lesson has been prepared for $current"
			exit 2
		}
		
		(echo -e "Troubleshooting Lessons\\nLesson for $current\\n\\n";
		 lesson-$type) | less
	;;
	
	"break")
		# Don't run if we're not root
		if [ $EUID -ne 0 ]
		then
			echo -e '\nScript must be run as root!\n'
			exit 1
		fi
	
		# Some functions needed below:
		#
		usage() {
			echo -e "\\n$cmd:\\tERROR\\n\\n\\tusage:\\t$cmd break\\n" >&2
			exit 1
		}
		
		instructions() {
			echo -e "\\n\\tInstructions for problem:" | tee /etc/ts
			(echo -e "\\n\\t\\t$1" | fmt -65) |tee -a /etc/ts
			echo "" |tee -a /etc/ts
		}
		
		# Read in the problems:
		#
		if [ -f "$TSPROBLEMS/$type" ]
		then
			source $TSPROBLEMS/$type
		else
			echo -e "$cmd: ERROR: $TSPROBLEMS/$type does not exist\\n" >&2
			exit 2
		fi
		
		# Check if chosen problem is already started:
		#
		#     Database format:  <cmd>:<date_started>

		current="$cmd problem"
		ts_history_file=/root/.rhce-ts-history

		if grep "^$cmd:" $ts_history_file >& /dev/null
		then
			until [[ "$restart" = y ]]
			do
				echo
				echo "Warning: you have already started $current. Would you"
				echo -n "like to restart $current anyway? (y/N) "
				read response
				echo
				restart=$(echo ${response:-n} | tr 'A-Z' 'a-z')

				case "$restart" in
				    y)	break ;;
				    n)	exit 1 ;;
				    *)	echo "Error: invalid choice - please type 'y' or 'n'." ;;
				esac
			done
		else
			echo "$cmd:$(date)" >> $ts_history_file
		fi

		echo -e "\\nSetting up problem . . . "
		
		# Actually invoke the problem: 
		#
		problem-$type
		sleep 1
		
		echo "done."
		echo -e "\\nFix the problem."
	;;
	
	
	*)
		usage() {
			echo -e "USAGE:\\n\\t$cmd break\\n" >&2
			echo -e "OR\\t$cmd hint <hintnum>\\n" >&2
			echo -e "OR\\t$cmd lesson\\n" >&2
			exit 1
		}
		usage
		;;		
esac 

