#!/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 3
# 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
# Felix Natter on Sun Apr  6 13:28:09 2003:
# - replace "M: UP=_EXIT" with "M: UP=MAINMENU"
# - MORE_LESSONS: add the new kt*.typ lessons
# - generate the M: __SERIESMENU

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;
    # in the input-files the menus are top-level (UP=_EXIT); in gtypist.typ
    # there is a parent menu (MAINMENU) generated by this script
    sub( "M: UP=_EXIT", "M: UP=__SERIESMENU", $0 );
    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("B: More lessons\n");
  printf("T:\n");
  printf(" :... 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(" : cs.typ       -  Czech lessons\n");
  printf(" : ru.typ       -  Russian lessons\n");
  printf(" : ktde.typ     -  German lessons from ktouch\n");
  printf(" : ktdk.typ     -  Danish lessons 1 from ktouch\n");
  printf(" : ktdk2.typ    -  Danish lessons 2 from ktouch\n");
  printf(" : ktdvorak.typ -  Dvorak lessons from ktouch\n");
  printf(" : kten.typ     -  English lessons from ktouch\n");
  printf(" : ktfr.typ     -  French lessons 1 from ktouch\n");
  printf(" : ktfr2.typ    -  French lessons 2 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("B: Help screen\n");
  printf("T:\n");
  printf(" :Use the arrow-keys to navigate through the menus.\n");
  printf(" :\n");
  printf(" :You can repeat a lesson by pressing ESC or skip it by pressing ESC twice.\n");
  printf(" :\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");

# this limitation is eliminated by the use of M:
#  if ( scount > 9)
#    exit 1;

# now build a selection menu to get to each 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 the menu screen
    banner = "Series selection menu";
    printf( "B:" );
    for ( i = 0; i < ( 66 - length( banner )) / 2; i++ )
      printf( " " );
    printf( "%s\n", banner );
    printf( "M: \"The following %d lesson series ", scount );
    if ( scount > 1 )
      printf( "are" );
    else
      printf( "is" );
    printf( " available\"\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;
      # output 1 menu-item
      printf( " :__S_%s_SERIES \"Series %s     %s\"\n", 
	      series[i], series[i], series_description);
#      printf( " :        Fkey%2d - Series %s     %s\n", i,
#	      series[i], series_description );
    }
    printf( " :__MORE_LESSONS \"More lessons...\"\n");
    printf( " :__HELP \"Help...\"\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:
