#!/bin/sh
# unit -- lecture unit extraction group
#
# SYNOPSIS:  this group of operations understands the Outline-files.txt file
#	found in each lecture unit directory.
# It includes the following operations
# (all but dump and print send output to standard output):
#	unit dump - send the files in unit to the printer
#	unit files - file names that make up the unit
#	unit examples - examples from all files of a unit
#	unit handouts - handouts (outline, questions, examples) from all files
#	unit outline - the outline only from the files in a unit
#	unit print - send the files in unit, in std outline fmt, to the printer
#	unit slides - examples (with page breaks) from all files of a unit
#
# BUGS: the print operation calls the print command dynamically
#       the shell PATH; this allows you to have your own way to print files.
#
# AUTHOR:	Gary T. Leavens

[ -n "$echo" ] && set -x

# note -- lecture note extraction group
#
# SYNOPSIS: this group of operations understands the format of lecture notes.
# It includes the following operations:
#	note examples - examples from all given files
#	note slides - slides (examples with page breaks) from all given files
#	note handouts - handouts (outline, questions, examples) from all files
#	note outline - the outline only from the files
# the default file argument is standard input, all output to stdout

noteGroup()
{
USAGE="Usage: 
$0 examples [file ...]
$0 slides [file ...]
$0 handouts [-n section] [file ...]
$0 outline [-n section] [file ...]
$0 print [-n section] [file ...]"

# The representation of lecture notes, in terms of sed patterns
HEADING='/^\*/'
EXAMPLE_START='/^[ 	]*------*[ 	]*$/'
EXAMPLE_END="$EXAMPLE_START"
EXAMPLE_END2='/^@[ ]*------*[ 	]*$/'
QUESTION_MARKER='Q: '
QUESTION_START="/^[ 	]*${QUESTION_MARKER}/"
QUESTION_END='/\?[ 	]*$/'

case "$1" in
examples)
	shift
	# Extract the examples (without dividing lines)
	sed -n -e "$EXAMPLE_START"'{
		:example
		n
		'"$EXAMPLE_END2"'{
			a\

			b done
			}
		'"$EXAMPLE_END"'{
			a\

			b done
			}
		p
		b example
		}
		:done
	' $*
	;;

slides)
	shift
	# Extract the examples (without dividing lines)
	sed -n -e "$EXAMPLE_START"'{
		:example
		n
		'"$EXAMPLE_END2"'{
			a\
			
			b done
			}
		'"$EXAMPLE_END"'{
			a\
			
			b done
			}
		p
		b example
		}
		:done
	' $* | sed -e '$d'
	;;

handouts|handout)
	shift
	# get -n argument for emacs_std_outline
	NARG=""
	case "$1" in
	-n)
		NARG="$1$2"
		shift; shift
		;;
	-n*)
		NARG="$1"
		shift
		;;
	esac
	# Extract the outline, questions, and examples
	sed -n -e ':begin
	'"$HEADING"'p
	'"$EXAMPLE_START"'{
		p
		:example
		n
		'"$EXAMPLE_END2"'{
			p
			n
			b begin
			}
		'"$EXAMPLE_END"'{
			p
			n
			b begin
			}
		p
		b example
	}
	'"$QUESTION_START"'{
		s/'"$QUESTION_MARKER"'//
		:questn
		p
		'"$QUESTION_END"'{
			b begin
			}
		n
		b questn
	}
    ' $* | emacs_std_outline $NARG
	;;

outline)
	shift
	# get -n argument for emacs_std_outline
	NARG=""
	case "$1" in
	-n)
		NARG="$1$2"
		shift; shift
		;;
	-n*)
		NARG="$1"
		shift
		;;
	esac
	# Extract the outline
	sed -n -e "$HEADING"'p' $* | emacs_std_outline $NARG
	;;

print)
	shift
	print outline $*	# doesn't do just the outline...
	;;

*)
	echo "$USAGE" >&2
	exit 2
	;;
esac
}

unitGroup()
{
USAGE="Usage: 
$0 files [directory ...]
$0 examples [directory ...]
$0 handouts [directory ...]
$0 outline [directory ...]
$0 print [directory ...]
$0 slides [directory ...]"

# default directory is .
if test $# -le 1
then
	set -- "$1" .	# argument 1 is "files" or "examples" etc...
fi


# file names for the unit (excluding Outline-files.txt) in each directory
mainunitfiles()
{
	for d
	do
	     sed -n -e 's%^\*\** \([-a-z0-9]*\)%'"${d}"'/\1.txt%p' \
	          ${d}/Outline-files.txt
	done
}

# all file names for the unit (including Outline-files.txt) in each directory
allunitfiles()
{
	for d
	do
	    echo ${d}/Outline-files.txt `mainunitfiles $d`
	done

}

case "$1" in
files)
	shift
	allunitfiles $* || exit 1
	;;

examples)
	shift
	# Extract the examples used in a unit (without dividing lines)
	noteGroup examples `mainunitfiles $*`
	;;

slides)
	shift
	# Extract the examples used in a unit (without dividing lines)
	noteGroup slides `mainunitfiles $*`
	;;

handouts|handout)
	shift
	# Extract the outline, questions, and examples used in a unit
	noteGroup handouts `mainunitfiles $*`	
	;;

outline)
	shift
	# Extract the outline used in a unit
	noteGroup outline `mainunitfiles $*`
	;;

dump)
	shift
	print ascii `allunitfiles $*`
	;;

print)
	shift
	# Print an outline and the lectures in the given directories
	for d
	do
		print outline ${d}/Outline-files.txt
		print outline `mainunitfiles $d`
	done
	;;

*)
	echo "$USAGE" >&2
	exit 2
	;;
esac
}


[ -n "$echo" ] && set -x

unitGroup $*

