#!/usr/bin/awk -f
#
# CAUTION: Only works with GNU awk (gawk)
# If /usr/bin/awk doesn't work, you can call this script by:
# gawk -f typcombine
#
# Typist v2.2 - 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 combine several typ files into a single large one
# modified by Felix Natter <fnatter@gmx.net> on Thu Aug 30 20:49:26 2001:
# - add two menu-items: "more lessons" (Fkey10) and "help"/"manual" (Fkey 11)
# - add ';' after each command to make emacs indentation work
# - add the "(function-key) help" and series descriptions

BEGIN 	{
# some initializations
  scount = 0;
  type = "I";
# use "-" here since that's what you get when you run
# awk against stdin, at least on Linux
  cur_filename = "-";
  cur_series = "";

# start as always by going to the menu we'll try to build later
  printf( "#" );
  for ( i = 0; i < 78; i++ )
    printf( "=" );
  printf( "\n# Combined series file (generated by tools/typcombine)\n#" );
  for ( i = 0; i < 78; i++ )
    printf( "=" );
  printf( "\n" );
  printf( "G:__SERIESMENU\n" );
  printf( "*:__NO_SERIESMENU\n" );
}


#
# look for change of input file on each record
#
FILENAME != cur_filename	{

# wrap up the previous series if there is one
  if ( cur_filename != "-" ) {
    printf( "# End of file %s\n", cur_filename );
    printf( "G:__SERIESMENU\n", cur_series );
    printf( "#" );
    for ( i = 0; i < 78; i++ )
    printf( "=" );
    printf( "\n" );
  }

# find the new series name, and store it
  cur_filename = FILENAME;
  cur_series = FILENAME;
  gsub( /\..*/, "", cur_series );
  
# we like the series names to be uppercase - I can't help
# but feel that there must be a better way to accomplish
# this, but I can't find it
# TODO: can we use toupper ?
  split( "q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m", lc,/,/ );
  split( "Q,W,E,R,T,Y,U,I,O,P,A,S,D,F,G,H,J,K,L,Z,X,C,V,B,N,M", uc,/,/ );
  for ( i = 1; i <= 26; i++ )
  gsub( lc[i], uc[i], cur_series );
  
# stash the series name
  scount++;
  series[scount] = cur_series;

# output the header for this series
  printf( "#" );
  for ( i = 0; i < 78; i++ )
  printf( "=" );
  printf( "\n" );
  printf( "*:__S_%s_SERIES\n", cur_series );
  printf( "# Start of file %s\n", cur_filename );
}


#
# print out every line read in
#
  {
    print $0;
  }


#
# end of file processing
#
END	{
# first finish up the last file's output if there was one
  if ( cur_filename != "-" ) {
    printf( "# End of file %s\n", cur_filename );
    printf( "G:__SERIESMENU\n" );
    printf( "#" );
    for ( i = 0; i < 78; i++ )
      printf( "=" );
    printf( "\n" );
  }

# build the T: commands for the "more lessons" and "help"/"manual" menu items
  printf("*:__MORE_LESSONS\n");
  printf("T:... there are more lessons which can be accessed by starting\n");
  printf(" : gtypist with an argument, the name of the file:\n");
  printf(" :\n");
  printf(" : esp.typ - Spanish lessons\n");
  printf(" : ktde.typ - German lessons from ktouch\n");
  printf(" : kten.typ - English lessons from ktouch\n");
  printf(" : ktfr.typ - French lessons from ktouch\n");
  printf(" : ktlecten.typ - Another set of English lessons from ktouch\n");
  printf(" : ktno.typ - Norwegian lessons from ktouch\n");
  printf(" : ktnumber.typ - Ktouch lessons for the numeric keypad\n");
  printf(" : ttde.typ - German lessons from tipptrainer\n");
  printf(" :\n");
  printf(" : See the comments at the top of each .typ-file for more information\n");
  printf("G:__SERIESMENU\n");

  printf("*:__HELP\n");
  printf("T:                      Help screen\n");
  printf(" :Use the function keys on the keyboard to navigate through the lesson\n");
  printf(" :series and lesson selection menus.  In a lesson series, pressing the\n");
  printf(" :function key Fkey12 will return you to the series menu; when already at\n");
  printf(" :the series menu, it will take you to the main program menu.  At the\n");
  printf(" :main program menu, Fkey12 exits the program.\n");
  printf(" :\n");
  printf(" :You can repeat a lesson by pressing ESC or skip it by pressing ESC twice.\n");
  printf(" :\n");
  printf(" :If you do not have function keys available on your terminal, or if they\n");
  printf(" :do not work for some reason (for example, they are intercepted by\n");
  printf(" :another program), you can use the keys 1 to 9 for F1 to F9, 0 for F10,\n");
  printf(" :A for F11 and S for F12.  Some combinations with the Ctrl key will work also.\n");
  printf(" :Ctrl-Q to Ctrl-P are alternatives for Fkey1 to Fkey10, and Ctrl-A and Ctrl-S\n");
  printf(" :for Fkey11 and Fkey12.\n");
  printf(" :\n");
  printf(" :You can access gtypist's manual either by running\n");
  printf(" :\"info gtypist\" (or \"info '(gtypist)'\" if that doesn't work),\n");
  printf(" :or by loading \"gtypist.htm\" in your browser (DOS).\n");
  printf(" :\n");
  printf(" :If you have comments, suggestions or bug-reports,\n");
  printf(" :please write to bug-gtypist@gnu.org.\n");
  printf("G:__SERIESMENU\n");

# TODO: find sth like "abort"
  if ( scount > 9)
    exit 1;

# now build a selection menu to get to each series
# (notice how this can't cope with more than 9 lesson series)
  if ( scount > 0 ) {
    printf( "#" );
    for ( i = 0; i < 78; i++ )
      printf( "=" );
    printf( "\n# Series menu\n#" );
    for ( i = 0; i < 78; i++ )
      printf( "=" );
    printf( "\n" );
    printf( "*:__SERIESMENU\n" );

# output a complete series of function key mappings
    for ( i = 1; i <= scount; i++ )
      printf( "K:%d:__S_%s_SERIES\n", i, series[i] );
    for ( ; i <= 9; i++ )
      printf( "K:%d:NULL\n", i );
    printf( "K:10:__MORE_LESSONS\n" );
    printf( "K:11:__HELP\n" );
    printf( "K:12:__SERIESQEXIT\n" );
    printf( "*:__SERIESMENU_DISP\n" );

# output the menu screen
    banner = "Series selection menu";
    printf( "B:" );
    for ( i = 0; i < ( 66 - length( banner )) / 2; i++ )
      printf( " " );
    printf( "%s\n", banner );
    printf( "T:The following %d lesson series ", scount );
    if ( scount > 1 )
      printf( "are" );
    else
      printf( "is" );
    printf( " available:\n" );
    printf( " :\n" );
    for ( i = 1; i <= scount; i++ ) {
      if (series[i] == "Q")
	series_description = "Quick QWERTY course (Q1 - Q5)";
      else if (series[i] == "R")
	series_description = "Long QWERTY course  (R1 - R14)";
      else if (series[i] == "T")
	series_description = "QWERTY touch typing (T1 - T16)";
      else if (series[i] == "V")
	series_description = "Yet Another QWERTY  (V1 - V19)";
      else if (series[i] == "U")
	series_description = "QWERTY Review       (U1 - U13)";
      else if (series[i] == "D")
	series_description = "Dvorak touch typing (D1 - D14)";
      else if (series[i] == "M")
	series_description = "Typing drills       (M1 - M11)";
      else if (series[i] == "S")
	series_description = "Speed drills        (S1 - S4)";
      else if (series[i] == "N")
	series_description = "Calculator keypad   (N1 - N3)";
      else # TODO: find sth like "abort"
	exit 1;
      printf( " :        Fkey%2d - Series %s     %s\n", i,
	      series[i], series_description );
    }
    for ( ;i <= 9; i++ )
      printf( " :\n" );
    printf( " :\n" );
    printf( " :        Fkey10                - More lessons...\n" );
    printf( " :        Fkey11 or A or Ctrl-A - Help...\n" );
    printf( " :\n" );
    printf( " :        Fkey12                - Exit the program\n" );

    printf( "Q: Please select a series, or Fkey12 to exit \n" );
    printf( "G:__SERIESMENU_DISP\n" );
    printf( "*:__SERIESQEXIT\n" );
    printf( "Q: Do you want to exit the program ? [Y/N] ? \n" );
    printf( "N:__SERIESMENU\n" );
    printf( "G:__SERIESEXIT\n" );
  }
  else {
# saw no series; oh well...
    printf( "! WARNING: didn't find any series\n" );
    printf( "G:__SERIESEXIT\n" );
    printf( "*:__SERIESMENU\n" );
    printf( "G:__NO_SERIESMENU\n" );
  }
  printf( "*:__SERIESEXIT\n" );
  printf( "#" );
  for ( i = 0; i < 78; i++ )
    printf( "=" );
  printf( "\n" );
}

# Local Variables:
# compile-command: "(cd ../lessons && gawk -f ../tools/typcombine q.typ r.typ t.typ v.typ u.typ d.typ m.typ s.typ n.typ > gtypist.typ)"
# truncate-lines: t
# End:
