#!/usr/bin/awk -f
#
# Typist v2.0 - improved typing tutor program for UNIX systems
# Copyright (C) 1998  Simon Baldwin (simonb@sco.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
# awk script to convert v1 .typ files into v2 format
#
BEGIN 	{
	# some initializations
	cindex = 0
	type = "I"
	cur_lesson = ""
	lcount = 0

	# generated labels include the value for 'series', to
	# enable several converted files to be concatenated;
	# if series is not set we try to invent something
	if ( series == "" ) {
		srand(); series = sprintf( "%6d", rand() * 1000000 )
	}

	# start by going to the menu we'll try to build later
	printf( "!\n! Series ID: %s\n!\n", series )
	printf( "G:_%s_MENU\n", series )
	printf( "*:_%s_NO_MENU\n", series )
}


#
# some lines had trailing \ with nothing after; strip these
#
$0 ~ /\\$/	{
	# strip off trailing backslashes
	while ( substr( $0, length( $0 ), 1 ) == "\\" )
		$0 = substr( $0, 1, length( $0 ) - 1 )
}


#
# handle a label line; one starting with *
#
$0 ~ /^\*/	{
	# label line - start or end a lesson block
	$0 = substr ( $0, 2, length( $0 ) - 1 )
	# stuff between the *s is the label
	if ( index( $0, "*" ) != 0 ) {

		# this is a lesson start
		label = substr( $0, 1, index( $0, "*" ) - 1)
		printf( "\n\n! Lesson %s\n\n", label )
		printf( "*:%s\n", label )
		lcount++
		labels[lcount] = label
		cur_lesson = label
		# now put out our own version of the label
		printf( "*:_%s_S_%s\n", series, label )
		$0 = substr ( $0, index( $0, "*" ), \
				length( $0 ) - index( $0, "*" ))
	}
	else {

		if ( $0 ~ "^ *$" ) {
			# this is a lesson end - go to a next lesson prompt
			if ( cur_lesson != "" )
				printf( "G:_%s_E_%s\n", series, cur_lesson )
			cur_lesson = ""
		}
		else {
			printf( "! WARNING: suspicious [non-]label below\n" )
			$0 = "*"$0
		}
	}

	# if this leaves the line empty don't go further
	if ( $0 == "" )
		next
}


#
# lines ending \<cmd> give the command that line represents
#
$0 ~ /\\[A-Z]$/	{
	# this is a line that gives the command type
	type = substr( $0, length( $0 ), 1 )
	cindex++
	lcache[cindex] = substr( $0, 1, length( $0 ) - 2 )

	# check for stuff that isn't in a lesson
	if ( cur_lesson == "" )
		printf( "! WARNING: found some lines not inside labels\n" )

	# check for commands we didn't expect
	if ( type != "T" && type != "I" && type != "D" && type != "P" \
				&& type != "B" )
		printf( "! WARNING: possible invalid command below\n" )

	# check for odd looking input
	if ( cindex > 2 && type == "I" || cindex > 1 && type == "B" )
		printf( "! WARNING: too long I/B detected below\n" )

	# dump out the stuff we have read in
	for ( i = 1; i <= cindex; i++ ) {
		if ( i == 1 )
			printf( "%s:%s\n", type, lcache[i] )
		else
			printf( " :%s\n", lcache[i] )
	}
	cindex = 0

	type = "I"
	next
}


#
# handle plain text lines that don't signify anything yet
#
{
	# nothing special line - just keep it
	cindex++
	lcache[cindex] = $0
}


#
# end of file processing
#
END	{
	# if trailing stuff was read then deal with it
	if ( cindex > 0 ) {
		printf( "! WARNING: trailing lines found in input\n" )

		# check for stuff that isn't in a lesson
		if ( cur_lesson == "" )
			printf( "! WARNING: found some lines not inside labels\n" )

		# check for odd looking input
		if ( cindex > 2 && type == "I" || cindex > 1 && type == "B" )
			printf( "! WARNING: too long I/B detected below\n" )

		# if stuff still cached output it (really shouldn't be)
		for ( i = 1; i <= cindex; i++ ) {
			if ( i == 1 )
				printf( "%s:%s\n", type, lcache[i] )
			else
				printf( " :%s\n", lcache[i] )
		}
		cindex = 0
	}

	# create the jump table and menu for the lessons we have seen go by
	if ( lcount > 0 ) {
		printf( "\n\n! Lesson jump table\n\n" )
		for ( i = 1; i < lcount; i++ ) {
			printf( "*:_%s_E_%s\n", series, labels[i] )
			printf( "Q: Do you want to continue to lesson %s [Y/N] ? \n", \
				labels[i+1] );
			printf( "N:_%s_QEXIT\n", series )
			printf( "B:\n" )
			printf( "G:_%s_S_%s\n", series, labels[i+1] )
		}
		printf( "*:_%s_E_%s\n", series, labels[lcount] )
		printf( "G:_%s_QEXIT\n", series )

		printf( "\n\n! Lesson menu\n\n" )
		printf( "*:_%s_MENU\n", series )
		# here we take the series name from labels[1]
		banner = sprintf( "Lesson selection menu - series %s", \
				substr( labels[1], 1, 1 ))
		printf( "B:" );
		for ( i = 0; i < ( 66 - length( banner )) / 2; i++ )
			printf( " " )
		printf( "%s\n", banner );
		printf( "T:The %s series contains the following %d lesson", \
				substr( labels[1], 1, 1 ), lcount )
		if ( lcount > 1 ) printf( "s" )
		printf( ":\n" )
		printf( " :\n" )
		for ( i = 1; i <= lcount; i++ )
			printf( " :        %2d - Lesson %s\n", i, labels[i] )
		for ( i = 1; i <= lcount; i++ ) {
			printf( "Q: Do you want to practise lesson %s [Y/N] ? \n", \
					labels[i] )
			printf( "N:_%s_R_%s\n", series, labels[i] )
			printf( "B:\n" )
			printf( "G:_%s_S_%s\n", series, labels[i] )
			printf( "*:_%s_R_%s\n", series, labels[i] )
		}
		printf( "*:_%s_QEXIT\n", series )
		printf( "Q: Do you want to leave this lesson series? [Y/N] ? \n" )
		printf( "N:_%s_MENU\n", series )
		printf( "G:_%s_EXIT\n", series )
	}
	else {
		# saw no labels; oh well...
		printf( "! WARNING: didn't find any labels\n" )
		printf( "G:_%s_EXIT\n", series )
		printf( "*:_%s_MENU\n", series )
		printf( "G:_%s_NO_MENU\n", series )
	}
	printf( "*:_%s_EXIT\n", series )
}
