cass.awk
by Winston Jenks <winston@capesoftware.com>

#---------------------------------------------------------------------+
#  Copyright (c) 2000 Winston Jenks. All Rights Reserved.             |
#                                                                     |
#                  http://thecassandraproject.org/                    |
#                                                                     |
#  This 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 software 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 (http://www.fsf.org/copyleft/gpl.htm)for    |
#  more details.                                                      |
#                                                                     |
#  To obtain the GNU General Public License, write to:                |
#                                                                     |
#      Free Software Foundation, Inc.                                 |
#      59 Temple Place - Suite 330                                    |
#      Boston, MA  02111-1307                                         |
#      USA                                                            |
#---------------------------------------------------------------------+

# (watch out here nawk does not exist on the AW70's) 
#
function PrintUsage()
{
    print "#Usage: " ARGV[0] " inpfile... [-?|help] "
    print "#\t      [-prog] [-[no]verb] [-[no]quiet]"
    print "#  where:"
    print "#   inpfile        contains the trip tables"
    print "#  and the the options are:"
    print "#   -help          prints this help"
    print "#   -prog          prints progress and summary"
    print "#   -[no]verb      [does not] print extra information"
    print "#   -config {file} uses {file} as config file"
    print "#"
    print "# This program does nothing. It exists to show a particular"
    print "# AWK style, which, if you find useful you can mimic. Try"
    print "# this with 'awk -f cass.awk cass.awk -progress cass.awk'"
}

# Note the two begin statements, this is OK. I do this because some
# AWKs allow an INIT section (which is what the first BEGIN {} was
BEGIN {
    # set up default option values here, g___ for intentionally global variables
    gRetStatus = 0
        # use o___ for things that are options set on the command line.
        oShowProgress = 0
        oVerbose = 0
        oQuiet = 0
        oHelp = 0
        oConfigFile = ""

        nFiles = 1
        for (i=1; i < ARGC; i++) {
            if (ARGV[i] ~ /^-/) {
                if (ARGV[i] ~ /-verb/) {
                    oVerbose = 1
                }              
                else if (ARGV[i] ~  /-noverb/) {
                    oVerbose = 0
                }              
                else if (ARGV[i] ~  /-prog/) {
                    oShowProgress = 1
                }              
                else if (ARGV[i] ~  /-noprog/) {
                    oShowProgress = 0
                }
                else if (ARGV[i] ~ /-help|-\?/) {
                    oHelp = 1
                }
                else if (ARGV[i] ~ /-config/) {
                    if ((i+1 < ARGC) && (ARGV[i+1] !~ /^-/)) {
                        i++
                        oConfigFile = ARGV[i]
                    }
                    else { 
                        print "## option", ARGV[i], "requires a filename"
                        gRetStatus = 1
                    }
                }
            else {
                # Unknown argument
                print "## Unknown option: " ARGV[i]
                gRetStatus = 1
            }
        }
        else {
            # The argument was not a switch, so it must be a file.
            # In this case if there is no extension, then assume a
            # ".txt"
            # print "File[" files "] =", ARGV[i]
            if (split(ARGV[i], ff, ".") == 1) {
                 ARGV[i] = ARGV[i] ".txt"
            }
            ARGV[nFiles++] = ARGV[i]
        }
    }
    ARGC = nFiles
    if ((oHelp) || (gRetStatus != 0)) {
        PrintUsage()
        exit(gRetStatus)
    }
}

# OK, now awk will get on with the show, parsing the files in
# the ARGC array as it should

BEGIN {
    print "Executing the BEGIN {} section"
    print "  oHelp = " oHelp
    print "  oShowProgress = " oShowProgress
    print "  oVerbose = " oVerbose
    print "  oQuiet = " oQuiet
    print "  oConfigfile = " oConfigFile
}

# this gets executed only once, because NR is not reset when a new
# file is read in.
NR == 1 {
    print "Executing the NR == 1 section"
}

# this gets executed once per file
FNR == 1 {
    print "Processing " FILENAME "..."
}

END {
    print "Executing the END {} section"
    exit(gRetStatus)
}