#!/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, see <http://www.gnu.org/licenses/>.
#
#
# 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
# Felix Natter on 2011-09-11:
# - update the T: commands for the "more lessons" menu item with the new
#   lesson files

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" );
  }

# TODO:
# - ttde.typ before kt*.de.typ
# - kt*.typ sorted alphabetically, look at manual, look at comment on top for source info
# - about 20 lines per T:
# - if you want to write lessons, see the manual, it's simple!
# - 

# 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(" : cs.typ            -  Czech lessons\n");
  printf(" : esp.typ           -  Spanish lessons\n");
  printf(" : ru.typ            -  Russian lessons\n");
  printf(" : ttde.typ          -  German lessons from tipptrainer 0.6.0\n");
  printf(" : ktbg.typ          -  Bulgarian lessons from KTouch 1.6\n");
  printf(" : ktbg_long.typ     -  Bulgarian lessons (longer) from KTouch 1.6\n");
  printf(" : ktde.typ          -  German lessons I from KTouch 1.6\n");
  printf(" : ktde2.typ         -  German lessons II from KTouch 1.6\n");
  printf(" : ktde_neo.typ      -  German lessons with NEO layout from KTouch 1.6\n");
  printf(" :  (see http://pebbles.schattenlauf.de/layout.php?language=de\n");
  printf(" : ktde_number.typ   -  German number keypad lessons from KTouch 1.6\n");
  printf(" : ktdk.typ          -  Danish lessons I from KTouch 1.6\n");
  printf(" : ktdk2.typ         -  Danish lessons II from KTouch 1.6\n");
  printf(" : ktdvorak.typ      -  Dvorak lessons from KTouch 1.6\n");
  printf(" :  (see http://en.wikipedia.org/wiki/Dvorak_Simplified_Keyboard)\n");
  printf(" : ktdvorak_es.typ   -  Spanish Dvorak lessons from KTouch 1.6\n");
  printf(" : ktdvorak_abcd.typ -  Dvorak lessons (basic) from KTouch 1.6\n");
  
  printf("T:\n");
  printf(" :... and even more lessons:\n");
  printf(" :\n");
  printf(" : kten.typ          -  English lessons from KTouch 1.6\n");
  printf(" : ktes.typ          -  Spanish lessons from KTouch 1.6\n");
  printf(" : ktes_cat.typ      -  Catalan lessons from KTouch 1.6\n");
  printf(" : ktfi.typ          -  Finnish lessons from KTouch 1.6\n");
  printf(" : ktfi_kids.typ     -  Finnish lessons for kids from KTouch 1.6\n");
  printf(" : ktfr.typ          -  French lessons I from KTouch 1.6\n");
  printf(" : ktfr2.typ         -  French lessons II from KTouch 1.6\n");
  printf(" : kthu.typ          -  Hungarian lessons I from KTouch 1.6\n");
  printf(" : kthu_expert.typ   -  Hungarian lessons II from KTouch 1.6\n");
  printf(" : ktit.typ          -  Italian lessons from KTouch 1.6\n");
  printf(" : ktnl.typ          -  Dutch lessons from KTouch 1.6\n");
  printf(" : ktnl_junior.typ   -  Dutch lessons for kids from KTouch 1.6\n");
  printf(" : ktno.typ          -  Norwegian lessons from KTouch 1.6\n");
  printf(" : ktpl.typ          -  Polish lessons from KTouch 1.6\n");
  printf(" : ktru.typ          -  Russian lessons from KTouch 1.6\n");
  printf(" : ktru_long.typ     -  Russian lessons (longer) from KTouch 1.6\n");
  printf(" : ktru_slava.typ    -  Russian lessons (hand-made) from KTouch 1.6\n");
  printf(" : ktsi.typ          -  Slovenian lessons from KTouch 1.6\n");
  printf(" : kttr.typ          -  Turkish lessons from KTouch 1.6\n");

  printf("T:\n");
  printf(" :\n");
  printf(" : See the comments at the top of each .typ file for more information\n");
  printf(" : about the source and the author of the lessons.\n");
  printf(" :\n");
  printf(" : If you want to write your own lessons, look at the gtypist manual,\n");
  printf(" : it's really simple! If you have any question, write to the\n");
  printf(" : gtypist mailing list: bug-gtypist@gnu.org\n");
  printf(" :\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(" :Press ESC to exit, repeat or skip a lesson.  If you are half way through a\n");
  printf(" :drill, pressing ESC will first reset it (so you will have to press it twice).\n");
  printf(" :\n");
  printf(" :You can access gtypist's manual either by running \"info gtypist\", or by\n");
  printf(" :loading \"gtypist.html\" in your browser.  The most recent version of the\n");
  printf(" :manual can also be found here:\n");
  printf(" :    http://www.gnu.org/software/gtypist/doc/\n");
  printf(" :\n");
  printf(" :If you have comments, questons, 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 more 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] == "C")
	series_description = "Colemak touch typing (C1 - C17)";
      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 c.typ m.typ s.typ n.typ > gtypist.typ)"
# truncate-lines: t
# End:
