Improve wording re bad durations

Improve wording re bad durations

# Create column files with lists of series titles, descriptions, number
# of seasons, and number of episodes, to be pasted into a spreadsheet.
# Note that IN_CANADA affects processing.
#
# Create a separate file with a line for each episode containing
# seriesNumber, episodeURL, seriesTitle, seasonNumber, episodeNumber,
# episodeTitle, & episodeDuration with the same columns as the primary
# spreadsheet so they can be combined into one.
#
# Create a separate file with a line for each series duration
#
# Return the total duration of all series
#
# INVOCATION:
#       curl -s https://acorn.tv/192/ \
#           | awk -v TITLE_FILE=$TITLE_FILE -v DESCRIPTION_FILE=$DESCRIPTION_FILE \
#           -v NUM_SEASONS_FILE=$NUM_SEASONS_FILE -v NUM_EPISODES_FILE=$NUM_EPISODES_FILE \
#           -v EPISODE_CURL_FILE=$EPISODE_CURL_FILE -v IN_CANADA=$IN_CANADA \
#           -v EPISODE_INFO_FILE=$EPISODE_INFO_FILE -v SERIES_NUMBER=$lastRow \
#           -v DURATION_FILE=$DURATION_FILE -v ERROR_FILE=$ERROR_FILE -f getAcornFrom-seriesPages.awk
#
# INPUT:
#        <title>Acorn TV | 19-2</title>
#  ---
#        19-2
#        <meta itemprop="numberOfEpisodes" content="30" />
#        <meta itemprop="numberOfSeasons" content="3" />
#  ---
#        <meta itemprop="numberOfEpisodes" content="10" />
#        <meta itemprop="numberOfEpisodes" content="10" />
#        <meta itemprop="numberOfEpisodes" content="10" />
#  ---
#
"The writing is sublime" (New \
#        York Times) in this anything-but-a-procedural cop drama ... as reluctant partners \
#        patrolling the streets of Montreal. Not Available in Canada. CC Available.

#  ---
#
We're sorry, but Acorn TV is not available in this territory.

#  ---
#        <meta itemprop="seasonNumber" content="1" />
#  ---
#
#  ---
#        <meta itemprop="timeRequired" content="T44M06S" />
#  ---
#
Partners

#  ---
#
Season 1: Episode 1

#
Season 9 Christmas Special: Episode 1

#
#
Series 1: Episode 1

#
Series 1 (Set 1): Episode 1

#
Series 10 (Sets 13 - 14): Episode 1

#
Series 19 (Part One): Episode 1

#
#
Set 1: Episode 1

#
#
Mini Series: Episode 1

#
Miniseries: Episode 1

#
#
Black Widows Season 1: Episode 1

#
#
A Dance to the Music of Time: Episode 1

#
Christmas Special : Episode 1

#
Feature Film: Episode 1

#
#
#
#  OUTPUT:
#       $EPISODE_INFO_FILE
#

# Extract the series title
/<title>/ {
    sub (/.*<title>Acorn TV \| /,"")
    sub (/<.*/,"")
    gsub (/&/,"\\&")
    gsub (/"/,"\"\"")
    # gsub (/'/,"'")
    if (match ($0, /^The /)) {
        $0 = substr($0, 5) ", The"
    }
    episodeLinesFound = 0
    numEpisodesStr = ""
    seriesTitle = $0
    print seriesTitle >> TITLE_FILE
    next
}

# Extract the number of episodes in the series
/itemprop="numberOfEpisodes"/ {
    split ($0,fld,"\"")
    episodeLinesFound += 1
    if (episodeLinesFound == 1)
        totalEpisodes = fld[4]
    if (episodeLinesFound != 1)
        numEpisodesStr = numEpisodesStr "+" fld[4]
    next
}

# Extract the number of seasons in the series
/itemprop="numberOfSeasons"/ {
    split ($0,fld,"\"")
    numSeasons = fld[4]
    if ((numSeasons + 0) == 0)
        printf ("==> No seasons: %d\t%s\n", SERIES_NUMBER, seriesTitle) >> ERROR_FILE
    print numSeasons >> NUM_SEASONS_FILE
    next
}

/Acorn TV is not available in this territory/ {
    printf ("==> Not available here: %d\t%s\n", SERIES_NUMBER, seriesTitle) >> ERROR_FILE
}

# Extract the series description
/id="franchise-description"/ {
    # get rid of boilerplate
    split ($0,fld,"[<>]")
    description = fld[3]
    # get rid of unnecessary characters and text
    gsub (/\\/,"",description)
    if (IN_CANADA != "yes") {
        sub (/Series [[:digit:]]* not available in Canada\./,"",description)
        sub (/Not [Aa]vailable in Canada\./,"",description)
        sub (/NOT AVAILABLE IN CANADA\./,"",description)
    }
    sub (/CC Available\. CC Available/,"CC Available",description)
    # fix sloppy input spacing
    sub (/\.CC Available/,". CC Available",description)
    gsub (/ \./,".",description)
    gsub (/  */," ",description)
    sub (/^ */,"",description)
    sub (/ *$/,"",description)
    # fix funky HTML characters
    gsub (/'/,"'",description)
    # fix unmatched quotes
    numQuotes = gsub(/"/,"\"",description)
    if ((numQuotes % 2) == 1) {
        printf ("==> Corrected unmatched quote (%d): %d\t%s\n", numQuotes, SERIES_NUMBER, \
                seriesTitle) >> ERROR_FILE
        description = description " \""
    }
    print description >> DESCRIPTION_FILE
    next
}

# Extract the seasonNumber
# Note that Acorn "season" numbers may not correspond to actual season numbers.
# They simply refer to the number of seasons available on Acorn.
/<meta itemprop="seasonNumber" content="/ {
    split ($0,fld,"\"")
    seasonNumber = fld[4]
    next
}

# Extract the episode URL
/> EPISODE_CURL_FILE
    next
}

# Extract the episode duration
/<meta itemprop="timeRequired"/ {
    split ($0,fld,"\"")
    # broken times in Jamaica Inn (may be temporary)
    if (episodeURL ~ /\/jamaicainn\// && fld[4] == "T-1M57S") {
        printf ("==> Corrected duration: %s  %s\n", episodeURL, fld[4]) >> ERROR_FILE
        fld[4] = "T59M18S"
    }
    if (episodeURL ~ /\/jamaicainn\/bonus\/cast-and-crew-interviews/ && fld[4] = "T09M08S") {
            printf ("==> Corrected duration: %s  %s\n", episodeURL, fld[4]) >> ERROR_FILE
        fld[4] = "T25M54S"
    }
    split (fld[4],tm,/[TMS]/)
    secs = tm[3]
    mins = tm[2] + int(secs / 60)
    hrs =  int(mins / 60)
    secs %= 60; mins %= 60
    #
    seriesSecs += secs
    seriesMins += mins + int(seriesSecs / 60)
    seriesHrs += hrs + int(seriesMins / 60)
    seriesSecs %= 60; seriesMins %= 60
    #
    totalSecs += secs
    totalMins += mins + int(totalSecs / 60)
    totalHrs += hrs + int(totalMins / 60)
    totalSecs %= 60; totalMins %= 60
    #
    episodeDuration = sprintf ("%02d:%02d:%02d", hrs, mins, secs)
    if (episodeDuration == "00:00:00")
        printf ("==> No duration: %d\t%s  %s\n", SERIES_NUMBER, seriesTitle, episodeURL) >> ERROR_FILE
    next
}

# Extract the episode title
/
/ {
    split ($0,fld,"[<>]")
    episodeTitle = fld[3]
    gsub (/&/,"\\&", episodeTitle)
    next
}

# There are a number of variants in the "episodeNumber" string
#
#
Season 1: Episode 1

#
Season 9 Christmas Special: Episode 1

#
#
Series 1: Episode 1

#
Series 1 (Set 1): Episode 1

#
Series 10 (Sets 13 - 14): Episode 1

#
Series 19 (Part One): Episode 1

#
#
Set 1: Episode 1

#
#
Mini Series: Episode 1

#
Miniseries: Episode 1

#
#
Black Widows Season 1: Episode 1

#
#
A Dance to the Music of Time: Episode 1

#
Christmas Special : Episode 1

#
Feature Film: Episode 1

# Extract episode number
/
.*span itemprop="episodeNumber">/ {
    # If show is a Prequel, use "P"
    if (episodeURL ~ /\/prequel/)
        showType = "P"
    # If show is a Miniseries, use "M"
    if (episodeURL ~ /\/miniseries/)
        showType = "M"
    # If we don't know what kind of show it is, use "S"
    if (showType == "")
        showType = "S"
    # Default episodeType to "E"
    episodeType = "E"
    # Default bonus, parttwo, and christmasspecials episodeType to "X"
    if (episodeURL ~ /bonus\/|[0-9]{1,2}parttwo\/|christmasspecial\//)
        episodeType = "X"
    # If episode is a Trailer, set episodeType to "T"
    if (episodeURL ~ /\/trailer/)
        episodeType = "T"
    # jamaicainn, newworlds & newtonslaw bonus or trailer seasonNumber should be 1
    if (episodeURL ~ \
        /\/jamaicainn\/bonus\/|\/jamaicainn\/trailer\/|\/newworlds\/bonus\/|\/newtonslaw\/bonus\// \
        && seasonNumber != 1) {
        split (episodeURL, part, "/")
        printf ("==> Corrected mismatch: https://acorn.tv/%s/%s was series %d\n", \
               part[4], part[5], seasonNumber) >> ERROR_FILE
        seasonNumber = 1
    }
    # Plain christmasspecial, seriesfinale don't increment seasonNumber
    if (episodeURL ~ /\/christmasspecial\/|\/seriesfinale\//) {
        split (episodeURL, part, "/")
        printf ("==> Corrected mismatch: https://acorn.tv/%s/%s was series %d\n", \
               part[4], part[5], seasonNumber) >> ERROR_FILE
        seasonNumber -= 1
    }
    #
    split ($0,fld,"[<>]")
    episodeNumber = fld[5]
    # Feature films don't have episodes
    if (episodeURL ~ /\/featurefilm\//)
        next
    # neither should single episode series
    if (totalEpisodes == 1)
        next
    # The season number should match that in the URL
    # Birds of a Feather, Doc Martin, Murdoch, Poirot, Rebus, Vera, and others have problems
    if (episodeURL ~ \
           /\/series[0-9]{1,2}\/|[0-9]{1,2}bonus\/|[0-9]{1,2}parttwo\/|\/murdoch\/season[0-9]{1,2}/) {
        split (episodeURL, part, "/")
        URLseasonNumber = part[5]
        sub (/bonus/,"",URLseasonNumber)
        # Foyle's War has sets: foyleswar/series9set8bonus/
        sub (/set[0-9]/,"",URLseasonNumber)
        # Midsomer Murders has two parts: midsomermurders/series19parttwo/
        sub (/parttwo/,"",URLseasonNumber)
        sub (/christmasspecial/,"",URLseasonNumber)
        sub (/[[:alpha:]]*/,"",URLseasonNumber)
        if (URLseasonNumber != seasonNumber) {
            printf ("==> Corrected mismatch: https://acorn.tv/%s/%s was series %d\n", \
               part[4], part[5], seasonNumber) >> ERROR_FILE
            seasonNumber = URLseasonNumber
        }
    }
    # Jamaica Inn has wrong episode numbers as of 180918 (may be temporary)
    if (episodeURL ~ /jamaicainn\/episode-[1-9]/) {
        split (episodeURL, episode, "-")
        if (episodeNumber != episode[2]) {
            printf ("==> Corrected mismatch: %s was episode %d\n", \
               episodeURL, episodeNumber) >> ERROR_FILE
            episodeNumber = episode[2]
        }
    }

    if ((episodeNumber + 0) == 0)
        print "==> Episode number is 00: " episodeURL >> ERROR_FILE
    printf ("%d\t=HYPERLINK(\"%s\";\"%s, %s%02d%s%02d, %s\"\)\t\t\t%s\n", \
        SERIES_NUMBER, episodeURL, seriesTitle, showType, seasonNumber, episodeType, episodeNumber, \
        episodeTitle, episodeDuration) >> EPISODE_INFO_FILE
    showType = ""
    next
}

/-- Viewers Also Watched --/ {
    print (episodeLinesFound == 1 ? "=0" : "=" numEpisodesStr) >> NUM_EPISODES_FILE
    if (description == "")
        printf ("==> No description: %d\t%s\n", SERIES_NUMBER, seriesTitle) >> ERROR_FILE
    description = ""
    #
    seriesDuration = sprintf ("%02d:%02d:%02d", seriesHrs, seriesMins, seriesSecs)
    print seriesDuration >> DURATION_FILE
    if (seriesDuration == "00:00:00")
        printf ("==> No duration: %d\t%s\n", SERIES_NUMBER, seriesTitle) >> ERROR_FILE
    seriesSecs = 0
    seriesMins = 0
    seriesHrs = 0
    seriesDuration = ""
    #
    SERIES_NUMBER += 1
}

END {
    # Return the total duration of all series
    printf ("%02d:%02d:%02d\n", totalHrs, totalMins, totalSecs)
}

# Create column files with lists of series titles, descriptions, number
# of seasons, and number of episodes, to be pasted into a spreadsheet.
# Note that IN_CANADA affects processing.
#
# Create a separate file with a line for each episode containing
# seriesNumber, episodeURL, seriesTitle, seasonNumber, episodeNumber,
# episodeTitle, & episodeDuration with the same columns as the primary
# spreadsheet so they can be combined into one.
#
# Create a separate file with a line for each series duration
#
# Return the total duration of all series
#
# INVOCATION:
#       curl -s https://acorn.tv/192/ \
#           | awk -v TITLE_FILE=$TITLE_FILE -v DESCRIPTION_FILE=$DESCRIPTION_FILE \
#           -v NUM_SEASONS_FILE=$NUM_SEASONS_FILE -v NUM_EPISODES_FILE=$NUM_EPISODES_FILE \
#           -v EPISODE_CURL_FILE=$EPISODE_CURL_FILE -v IN_CANADA=$IN_CANADA \
#           -v EPISODE_INFO_FILE=$EPISODE_INFO_FILE -v SERIES_NUMBER=$lastRow \
#           -v DURATION_FILE=$DURATION_FILE -v ERROR_FILE=$ERROR_FILE -f getAcornFrom-seriesPages.awk
#
# INPUT:
#        <title>Acorn TV | 19-2</title>
#  ---
#        19-2
#        <meta itemprop="numberOfEpisodes" content="30" />
#        <meta itemprop="numberOfSeasons" content="3" />
#  ---
#        <meta itemprop="numberOfEpisodes" content="10" />
#        <meta itemprop="numberOfEpisodes" content="10" />
#        <meta itemprop="numberOfEpisodes" content="10" />
#  ---
#
"The writing is sublime" (New \
#        York Times) in this anything-but-a-procedural cop drama ... as reluctant partners \
#        patrolling the streets of Montreal. Not Available in Canada. CC Available.

#  ---
#
We're sorry, but Acorn TV is not available in this territory.

#  ---
#        <meta itemprop="seasonNumber" content="1" />
#  ---
#
#  ---
#        <meta itemprop="timeRequired" content="T44M06S" />
#  ---
#
Partners

#  ---
#
Season 1: Episode 1

#
Season 9 Christmas Special: Episode 1

#
#
Series 1: Episode 1

#
Series 1 (Set 1): Episode 1

#
Series 10 (Sets 13 - 14): Episode 1

#
Series 19 (Part One): Episode 1

#
#
Set 1: Episode 1

#
#
Mini Series: Episode 1

#
Miniseries: Episode 1

#
#
Black Widows Season 1: Episode 1

#
#
A Dance to the Music of Time: Episode 1

#
Christmas Special : Episode 1

#
Feature Film: Episode 1

#
#
#
#  OUTPUT:
#       $EPISODE_INFO_FILE
#

# Extract the series title
/<title>/ {
    sub (/.*<title>Acorn TV \| /,"")
    sub (/<.*/,"")
    gsub (/&/,"\\&")
    gsub (/"/,"\"\"")
    # gsub (/'/,"'")
    if (match ($0, /^The /)) {
        $0 = substr($0, 5) ", The"
    }
    episodeLinesFound = 0
    numEpisodesStr = ""
    seriesTitle = $0
    print seriesTitle >> TITLE_FILE
    next
}

# Extract the number of episodes in the series
/itemprop="numberOfEpisodes"/ {
    split ($0,fld,"\"")
    episodeLinesFound += 1
    if (episodeLinesFound == 1)
        totalEpisodes = fld[4]
    if (episodeLinesFound != 1)
        numEpisodesStr = numEpisodesStr "+" fld[4]
    next
}

# Extract the number of seasons in the series
/itemprop="numberOfSeasons"/ {
    split ($0,fld,"\"")
    numSeasons = fld[4]
    if ((numSeasons + 0) == 0)
        printf ("==> No seasons: %d\t%s\n", SERIES_NUMBER, seriesTitle) >> ERROR_FILE
    print numSeasons >> NUM_SEASONS_FILE
    next
}

/Acorn TV is not available in this territory/ {
    printf ("==> Not available here: %d\t%s\n", SERIES_NUMBER, seriesTitle) >> ERROR_FILE
}

# Extract the series description
/id="franchise-description"/ {
    # get rid of boilerplate
    split ($0,fld,"[<>]")
    description = fld[3]
    # get rid of unnecessary characters and text
    gsub (/\\/,"",description)
    if (IN_CANADA != "yes") {
        sub (/Series [[:digit:]]* not available in Canada\./,"",description)
        sub (/Not [Aa]vailable in Canada\./,"",description)
        sub (/NOT AVAILABLE IN CANADA\./,"",description)
    }
    sub (/CC Available\. CC Available/,"CC Available",description)
    # fix sloppy input spacing
    sub (/\.CC Available/,". CC Available",description)
    gsub (/ \./,".",description)
    gsub (/  */," ",description)
    sub (/^ */,"",description)
    sub (/ *$/,"",description)
    # fix funky HTML characters
    gsub (/'/,"'",description)
    # fix unmatched quotes
    numQuotes = gsub(/"/,"\"",description)
    if ((numQuotes % 2) == 1) {
        printf ("==> Corrected unmatched quote (%d): %d\t%s\n", numQuotes, SERIES_NUMBER, \
                seriesTitle) >> ERROR_FILE
        description = description " \""
    }
    print description >> DESCRIPTION_FILE
    next
}

# Extract the seasonNumber
# Note that Acorn "season" numbers may not correspond to actual season numbers.
# They simply refer to the number of seasons available on Acorn.
/<meta itemprop="seasonNumber" content="/ {
    split ($0,fld,"\"")
    seasonNumber = fld[4]
    next
}

# Extract the episode URL
/> EPISODE_CURL_FILE
    next
}

# Extract the episode duration
/<meta itemprop="timeRequired"/ {
    split ($0,fld,"\"")
    # broken times in Jamaica Inn (may be temporary)
    if (episodeURL ~ /\/jamaicainn\// && fld[4] == "T-1M57S") {
        printf ("==> Bad duration: %s  %s\n", episodeURL, fld[4]) >> ERROR_FILE
        fld[4] = "T59M18S"
    }
    if (episodeURL ~ /\/jamaicainn\/bonus\/cast-and-crew-interviews/ && fld[4] = "T09M08S") {
            printf ("==> Bad duration: %s  %s\n", episodeURL, fld[4]) >> ERROR_FILE
        fld[4] = "T25M54S"
    }
    split (fld[4],tm,/[TMS]/)
    secs = tm[3]
    mins = tm[2] + int(secs / 60)
    hrs =  int(mins / 60)
    secs %= 60; mins %= 60
    #
    seriesSecs += secs
    seriesMins += mins + int(seriesSecs / 60)
    seriesHrs += hrs + int(seriesMins / 60)
    seriesSecs %= 60; seriesMins %= 60
    #
    totalSecs += secs
    totalMins += mins + int(totalSecs / 60)
    totalHrs += hrs + int(totalMins / 60)
    totalSecs %= 60; totalMins %= 60
    #
    episodeDuration = sprintf ("%02d:%02d:%02d", hrs, mins, secs)
    if (episodeDuration == "00:00:00")
        printf ("==> No duration: %d\t%s  %s\n", SERIES_NUMBER, seriesTitle, episodeURL) >> ERROR_FILE
    next
}

# Extract the episode title
/
/ {
    split ($0,fld,"[<>]")
    episodeTitle = fld[3]
    gsub (/&/,"\\&", episodeTitle)
    next
}

# There are a number of variants in the "episodeNumber" string
#
#
Season 1: Episode 1

#
Season 9 Christmas Special: Episode 1

#
#
Series 1: Episode 1

#
Series 1 (Set 1): Episode 1

#
Series 10 (Sets 13 - 14): Episode 1

#
Series 19 (Part One): Episode 1

#
#
Set 1: Episode 1

#
#
Mini Series: Episode 1

#
Miniseries: Episode 1

#
#
Black Widows Season 1: Episode 1

#
#
A Dance to the Music of Time: Episode 1

#
Christmas Special : Episode 1

#
Feature Film: Episode 1

# Extract episode number
/
.*span itemprop="episodeNumber">/ {
    # If show is a Prequel, use "P"
    if (episodeURL ~ /\/prequel/)
        showType = "P"
    # If show is a Miniseries, use "M"
    if (episodeURL ~ /\/miniseries/)
        showType = "M"
    # If we don't know what kind of show it is, use "S"
    if (showType == "")
        showType = "S"
    # Default episodeType to "E"
    episodeType = "E"
    # Default bonus, parttwo, and christmasspecials episodeType to "X"
    if (episodeURL ~ /bonus\/|[0-9]{1,2}parttwo\/|christmasspecial\//)
        episodeType = "X"
    # If episode is a Trailer, set episodeType to "T"
    if (episodeURL ~ /\/trailer/)
        episodeType = "T"
    # jamaicainn, newworlds & newtonslaw bonus or trailer seasonNumber should be 1
    if (episodeURL ~ \
        /\/jamaicainn\/bonus\/|\/jamaicainn\/trailer\/|\/newworlds\/bonus\/|\/newtonslaw\/bonus\// \
        && seasonNumber != 1) {
        split (episodeURL, part, "/")
        printf ("==> Corrected mismatch: https://acorn.tv/%s/%s was series %d\n", \
               part[4], part[5], seasonNumber) >> ERROR_FILE
        seasonNumber = 1
    }
    # Plain christmasspecial, seriesfinale don't increment seasonNumber
    if (episodeURL ~ /\/christmasspecial\/|\/seriesfinale\//) {
        split (episodeURL, part, "/")
        printf ("==> Corrected mismatch: https://acorn.tv/%s/%s was series %d\n", \
               part[4], part[5], seasonNumber) >> ERROR_FILE
        seasonNumber -= 1
    }
    #
    split ($0,fld,"[<>]")
    episodeNumber = fld[5]
    # Feature films don't have episodes
    if (episodeURL ~ /\/featurefilm\//)
        next
    # neither should single episode series
    if (totalEpisodes == 1)
        next
    # The season number should match that in the URL
    # Birds of a Feather, Doc Martin, Murdoch, Poirot, Rebus, Vera, and others have problems
    if (episodeURL ~ \
           /\/series[0-9]{1,2}\/|[0-9]{1,2}bonus\/|[0-9]{1,2}parttwo\/|\/murdoch\/season[0-9]{1,2}/) {
        split (episodeURL, part, "/")
        URLseasonNumber = part[5]
        sub (/bonus/,"",URLseasonNumber)
        # Foyle's War has sets: foyleswar/series9set8bonus/
        sub (/set[0-9]/,"",URLseasonNumber)
        # Midsomer Murders has two parts: midsomermurders/series19parttwo/
        sub (/parttwo/,"",URLseasonNumber)
        sub (/christmasspecial/,"",URLseasonNumber)
        sub (/[[:alpha:]]*/,"",URLseasonNumber)
        if (URLseasonNumber != seasonNumber) {
            printf ("==> Corrected mismatch: https://acorn.tv/%s/%s was series %d\n", \
               part[4], part[5], seasonNumber) >> ERROR_FILE
            seasonNumber = URLseasonNumber
        }
    }
    # Jamaica Inn has wrong episode numbers as of 180918 (may be temporary)
    if (episodeURL ~ /jamaicainn\/episode-[1-9]/) {
        split (episodeURL, episode, "-")
        if (episodeNumber != episode[2]) {
            printf ("==> Corrected mismatch: %s was episode %d\n", \
               episodeURL, episodeNumber) >> ERROR_FILE
            episodeNumber = episode[2]
        }
    }

    if ((episodeNumber + 0) == 0)
        print "==> Episode number is 00: " episodeURL >> ERROR_FILE
    printf ("%d\t=HYPERLINK(\"%s\";\"%s, %s%02d%s%02d, %s\"\)\t\t\t%s\n", \
        SERIES_NUMBER, episodeURL, seriesTitle, showType, seasonNumber, episodeType, episodeNumber, \
        episodeTitle, episodeDuration) >> EPISODE_INFO_FILE
    showType = ""
    next
}

/-- Viewers Also Watched --/ {
    print (episodeLinesFound == 1 ? "=0" : "=" numEpisodesStr) >> NUM_EPISODES_FILE
    if (description == "")
        printf ("==> No description: %d\t%s\n", SERIES_NUMBER, seriesTitle) >> ERROR_FILE
    description = ""
    #
    seriesDuration = sprintf ("%02d:%02d:%02d", seriesHrs, seriesMins, seriesSecs)
    print seriesDuration >> DURATION_FILE
    if (seriesDuration == "00:00:00")
        printf ("==> No duration: %d\t%s\n", SERIES_NUMBER, seriesTitle) >> ERROR_FILE
    seriesSecs = 0
    seriesMins = 0
    seriesHrs = 0
    seriesDuration = ""
    #
    SERIES_NUMBER += 1
}

END {
    # Return the total duration of all series
    printf ("%02d:%02d:%02d\n", totalHrs, totalMins, totalSecs)
}


dbcreate.csh -gld_has_db_fullpath now creates mumps.gld file with a pointer to corresponding mumps.repl file (#270)

#################################################################
#								#
# Copyright (c) 2002-2016 Fidelity National Information		#
# Services, Inc. and/or its subsidiaries. All rights reserved.	#
#								#
# Copyright (c) 2017-2018 YottaDB LLC. and/or its subsidiaries.	#
# All rights reserved.						#
#								#
#	This source code contains the intellectual property	#
#	of its copyright holder(s), and is made available	#
#	under a license.  If you do not know the terms of	#
#	the license, please stop and do not read further.	#
#								#
#################################################################
BEGIN { split("file_name n_regions key_size record_size block_size allocation global_buffer_count extension_count reserved_bytes collation_default null_subscripts access_method acc_meth_env journal test_collation test_stdnull_collation test_gtcm qdbrundown freeze defer_allocate epoch_taper lock_space", argmnts, " ")
	# name_override is an argument disguised from the user, used to take the $gtmgbldir definition from the shell if it exists
	# -different_gld is useful on replication tests with multi-region databases:
	#  	instead of a*, b*, c*, d* going to different regions, on the remote directories, they go in pairs: a* and b* to areg, c* and d* to creg, e* and f* to ereg, ...
	fn = "a b c d e f g h i j k l m n o p q r s t u v w x y z"
	FN = toupper(fn);
	split(fn,filenames," ")
	split(FN, FILENAMES," ")
	if (ENVIRON["tst_on_remote"]) tst_on_remote=1
	if (1 == ENVIRON["gtm_test_qdbrundown"]) value["qdbrundown"]="-qdbrundown"
	if (1 == ENVIRON["gtm_test_freeze_on_error"]) value["freeze"]="-inst_freeze_on_error"
	if (0 == ENVIRON["gtm_test_defer_allocate"]) value["defer_allocate"]="-nodefer_allocate"
	if (0 == ENVIRON["gtm_test_epoch_taper"]) value["epoch_taper"]="-noepochtaper"
	value["jnl_prefix"] = "***unspec***"
	value["jnl_suffix"] = "***unspec***"
	value["move"]=1
       }

      { for (i=1;i<=NF;i++)
	   {tmp=$i
	    gsub(/.*=/,"",tmp)
	    if (tmp == $i) tmp="NULL"
	    if ($i ~/-f/) value["file_name"]=tmp
	    else if ($i ~/^-bl/) value["block_size"]=tmp
	    else if ($i ~/^-al/) value["allocation"]=tmp
	    else if ($i ~/^-glo/) value["global_buffer_count"]=tmp
	    else if ($i ~/^-e/) value["extension_count"]=tmp
	    else if ($i ~/^-c/) value["collation_default"]=tmp
	    else if ($i ~/^-rec/) value["record_size"]=tmp
	    else if ($i ~/^-res/) value["reserved_bytes"]=tmp
	    else if ($i ~/^-k/) value["key_size"]=tmp
	    else if ($i ~/^-nom/) value["move"]=0
	    # null_subscripts has multiple entries to differentiate from n_reg
	    else if ($i ~/^-nu.*$/) value["null_subscripts"]=tmp
	    else if ($i ~/^-n$/) value["null_subscripts"]="NOVAL"
	    else if ($i ~/^-n=/) value["null_subscripts"]=tmp
	    else if ($i ~/^-nu=/) value["null_subscripts"]=tmp
	    else if ($i ~/^-acc_meth/) value["access_method"]=toupper(tmp)
	    else if ($i ~/^-acc_meth_env/) value["acc_meth_env"]=toupper(tmp)
	    else if ($i ~/^-journal/) value["journal" ++jnl_count]=toupper(tmp)
	    else if ($i ~/^-n_regions/) value["n_regions"]=tmp
	    else if ($i ~/^-qdbrundown/) value["qdbrundown"]="-qdbrundown"
	    else if ($i ~/^-noqdbrundown/) value["qdbrundown"]="-noqdbrundown"
	    else if ($i ~/^-inst_freeze_on_error/) value["freeze"]="-inst_freeze_on_error"
	    else if ($i ~/^-noinst_freeze_on_error/) value["freeze"]="-noinst_freeze_on_error"
	    else if ($i ~/^-test_collation/) value["test_collation"]=tmp
	    else if ($i ~/^-name_override/) value["filename_override"]=tmp
	    else if ($i ~/^-test_gtcm/)  value["test_gtcm"]=tmp
	    else if ($i ~/^-t/)  value["template"]=tmp #template
	    else if ($i ~/^-stdnull/) value["test_stdnull_collation"]="-stdnull"
	    else if ($i ~/^-nostdnull/) value["test_stdnull_collation"]="-nostdnull"
	    else if ($i ~/^-different_gld/) value["different_gld"]=1
	    else if ($i ~/^-gld_has_db_fullpath/) value["gld_has_db_fullpath"]=1
	    else if ($i ~/^-jnl_prefix=/) value["jnl_prefix"]=tmp
	    else if ($i ~/^-jnl_suffix=/) value["jnl_suffix"]=tmp
	    else if ($i ~/^-jnl_auto/) value["journal" ++jnl_count]="autoswitchlimit=" tmp
	    else if ($i ~/^-defer_allocate/) value["defer_allocate"]="-defer_allocate"
	    else if ($i ~/^-nodefer_allocate/) value["defer_allocate"]="-nodefer_allocate"
	    else if ($i ~/^-epoch_taper/) value["epoch_taper"]="-epochtaper"
	    else if ($i ~/^-noepoch_taper/) value["epoch_taper"]="-noepochtaper"
	    else if ($i ~/^-lock_space/) value["lock_space"]=tmp
	    else  { value[argmnts[i]]=$i;
		  }
	    }
      }

END {
      if (value["test_gtcm"])
      {
	#if the value is a "x;y,z" x is the local host, this means, just create the regions for that one.y or z must be same as x
	loc_semicolon=index(value["test_gtcm"],";")
	if (loc_semicolon)
	{
		# this is a GT.CM server, nullify all the others.
		this_host=substr(value["test_gtcm"],1,loc_semicolon-1);
		print "#This is a GT.CM server ("this_host"). Only some regions will be here"
		gsub(".*;","",value["test_gtcm"])
		this_is_a_gtcm_server=1
	}
	total_gtcm_serv_no=split(value["test_gtcm"],gtcm_servers,",")
	if (this_is_a_gtcm_server)
		for (serv in gtcm_servers)
			if (!index(gtcm_servers[serv],this_host":")) gtcm_servers[serv]=""
	gtcm_serv_no=1
	cur_gtcm_server = gtcm_servers[gtcm_serv_no++]
      }
      default_filename="mumps"
      sub("\\.gld$","",value["filename_override"])
      if (value["filename_override"]!="") default_filename=value["filename_override"]
      if (value["file_name"] !~/..*/ || value["file_name"] ~/\./) value["file_name"]=default_filename
      # Check if value["file_name"] points to a subdirectory. If so, cd to that subdir before creating the db
      nsubdirs = split(value["file_name"], fullpath, "/")
      if ((1 < nsubdirs) && ("" != fullpath[1]))	# fullpath[1] check needed to ensure it is not an absolute path
      {
	      subdir = ""
	      for (i = 1; i < nsubdirs; i++)
		subdir = subdir "" fullpath[i] "/"
	      printf "mkdir -p %s\n", subdir
	      printf "cd %s\n", subdir
	      value["file_name"] = fullpath[nsubdirs]
      }
      if (value["gld_has_db_fullpath"])
              value["file_name"] = ENVIRON["PWD"] "/" value["file_name"]
      print "setenv dbname " value["file_name"]
      print "setenv gtmgbldir \"$dbname.gld\""
      print "set timenow = `date +%H_%M_%S`"
      print "set renamecount = 0 ; set rename = \"$timenow\""
      print "if (-e $gtmgbldir) then"
      print "	while ( -e ${gtmgbldir}_$rename)"
      print "		@ renamecount++"
      print "		set rename = \"${timenow}_$renamecount\""
      print "	end"
      print " 	mv $gtmgbldir ${gtmgbldir}_$rename"
      print "endif"
      if (value["move"])
      {
	      print "set datpat = '*.dat'"
	      print "set nonomatch"
	      print "set datfiles = $datpat"
	      print "unset nonomatch"
	      print "if (\"$datfiles\" != \"$datpat\") then"
	      print "	foreach datfile ($datfiles)"
	      print "		mv $datfile ${datfile}_$rename"
	      print "	end"
	      print "endif"
      }
      print "if (($?test_replic) && (-e mumps.repl)) mv mumps.repl mumps.repl_$rename"
      if (value["access_method"] !~ /..*/ || value["access_method"] ~/\./) value["access_method"]=value["acc_meth_env"]
      if (value["access_method"] !~ /..*/ ) value["access_method"]="BG"
      print "setenv acc_meth " value["access_method"]
      #-------- temporary change till GDE works for GLOBAL_BUFFERS for MM   -- nars - 98/02/25_09:05am
      print "set mmglobalbuffs=0"
      #-------- temporary change ends ----------------------------------------------------------------
      #-------- temporary change till GDE works for GLOBAL_BUFFERS for MM   -- nars - 98/02/25_09:05am
      if (value["access_method"] == "MM") { printf "set mmglobalbuffs = "
				       if (value["global_buffer_count"] ~ /..*/ )
					   print value["global_buffer_count"]
				       else
					   print 1024
					   }
      #-------- temporary change ends ----------------------------------------------------------------

      if (value["template"])
	print "#DBCREATE-I-TEMPLATE Will use the file "value["template"] " for \n# template commands, and will not use any of the other settings"
      print "cat << \\_GDE_EOF >>&! dbcreate.gde"
      if (value["template"])
      {
	command1 = "cat "value["template"]
	command2 = "sed -n 's/template.*-segment/change -segment DEFAULT/gp' "value["template"]
	command3 = "sed -n 's/template.*-region/change -region DEFAULT/gp' "value["template"]
	system(command1" ; "command2" ; "command3)
      }

      {	#DEFAULT region
	      if (this_is_a_gtcm_server) cur_gtcm_server = "" ; #local
	      sub(/\.[^:]*:/,":",cur_gtcm_server)
	      print "change -segment DEFAULT -file_name=" cur_gtcm_server value["file_name"] ".dat"
	      if (value["access_method"] ~ /MM/)
		  {
		   if (value["global_buffer_count"] !~/..*/ || value["global_buffer_count"] ~/\./)
			value["global_buffer_count"]=1024
		   }

	      if (! value["template"])
	      print_block("DEFAULT","DEFAULT")
		    if (value["test_collation"]) print "change -region DEFAULT -collation_default=" value["test_collation"]
      }
      # If value["n_regions"] is not an integer, the for loop below would go forever.
      if ( (value["n_regions"] ~ /^[0-9]+$/) && (value["n_regions"]>1) )
	{
	 for (i=1;i<value["n_regions"];i++)
	   {
	    skip=0
	    if (value["test_gtcm"])
	    {
		    if (1 != i) {cur_gtcm_server = gtcm_servers[gtcm_serv_no++]; if (gtcm_serv_no > total_gtcm_serv_no) gtcm_serv_no=1}
		    #print "#Region no:"i " Server:" cur_gtcm_server
		    if ("" == cur_gtcm_server) skip = 1
		    # i=1 means region A, which is going to be local on the original (client) server
		    cur_gtcm_server_hostname = substr(cur_gtcm_server,1,index(cur_gtcm_server,":")-1)
		    # remove any domain bits from the hostname, leaving only the first part, and bracket it
		    if ("" != cur_gtcm_server_hostname)
		    {
			    sub(/\.[^:]*$/,"",cur_gtcm_server_hostname)
			    cur_gtcm_server = cur_gtcm_server_hostname substr(cur_gtcm_server,index(cur_gtcm_server,":"))
		    }
		    #if ((1 != i)&&(cur_gtcm_server_hostname != this_host)) skip = 1
		    if (gtcm_server[i]) skip = 1
		    #print "#XXXa" skip "." cur_gtcm_server "."this_host "." cur_gtcm_server_hostname"."
		    if ((1 == i)&&(this_is_a_gtcm_server)) skip = 1
	    }
	    if (!skip)
	    {
		    if ((1 == i)||(this_is_a_gtcm_server)) cur_gtcm_server = "" #local
		    if (tst_on_remote && value["different_gld"])
		    {
			    print "! different_gld-specified-in-input"	# this output is relied upon by dbcreate_base.csh
			    if (i%2)
			    {
				fname = filenames[i]".dat"
				seg = filenames[i]"seg"
				reg = filenames[i]"reg"
			    }
		    } else
		    {
			    fname = filenames[i]".dat"
			    seg = filenames[i]"seg"
			    reg = filenames[i]"reg"
		    }
		    if (value["gld_has_db_fullpath"])
			fname = ENVIRON["PWD"] "/" fname
		    print "add -name " filenames[i] "* -region="  reg
		    print "add -name " FILENAMES[i] "* -region="  reg
		    if (!(tst_on_remote && value["different_gld"] && !(i%2)))
		    {
			print "add -region " reg " -dyn=" seg
			if ((0 != value["test_gtcm"])&&(i>1))
			{
				   print "add -segment " seg " -file=" cur_gtcm_server fname
				   # to test broken database
				   # print "add -segment " seg " -file=" cur_gtcm_server fname
			} else
				    print "add -segment " seg " -file=" fname
			if (! value["template"])
				    print_block(seg, reg)
			# for all regions but A, change collation
			if (value["test_collation"]) if (i>1) print "change -region " reg " -collation_default=" value["test_collation"]
		    }
	    }
	    }
      }
      if (value["gld_has_db_fullpath"])
          print "change -instance -file_name=" ENVIRON["PWD"] "/mumps.repl"

      print "\\_GDE_EOF"
      print "$gtm_tst/com/usesprgde.csh >> dbcreate.gde"
      print "$convert_to_gtm_chset dbcreate.gde"
      print "$GDE_SAFE @dbcreate.gde"
    }

function print_block(segname,regname){
      if (value["access_method"] == "MM")
      {
	    print_jnl(regname, 0)
	    print_out("access_method", "segment", segname)
	    print "change -region " regname " -nojournal"
      }
      else
	    print_jnl(regname, 1)
      print_out("block_size", "segment",segname)
      print_out("allocation","segment",segname)
      if (value["access_method"] != "MM") print_out("global_buffer_count","segment",segname)
      print_out("extension_count","segment",segname)
      print_out("reserved_bytes","segment",segname)
      print_out("lock_space","segment",segname)
      print_out("record_size","region",regname)
      print_out("key_size","region",regname)
      print_out("collation_default","region",regname)
      if (value["test_stdnull_collation"])
		print "change -region " regname " " value["test_stdnull_collation"]
      major_ver = gensub(/^.*V([0-9])[0-9]+.*/,"\\1","g",ENVIRON["gtm_dist"])
      # QDBRUNDOWN and INST_FREEZE_ON_ERROR options are not available for versions older than V60000
      if (6 <= major_ver)
      {
	  if (value["freeze"])
	      print "change -region " regname " " value["freeze"]
	  if (value["qdbrundown"])
		print "change -region " regname " " value["qdbrundown"]
	  # DEFER_ALLOCATE option is available from V62002
	  if ((6 < major_ver) || (2002 <= gensub(/^.*V[0-9]([0-9]+).*/,"\\1","g",ENVIRON["gtm_dist"])))
	  {
	      if (value["defer_allocate"])
		  print "change -segment " segname " " value["defer_allocate"]
	      if (value["epoch_taper"])
		  print "change -region " regname " " value["epoch_taper"]
	  }
      }
      if  ("NULL" == value["null_subscripts"]) # this NULL has nothing to do with "NULL"_subscripts, it is dbcreate_multi.awk's "NULL"
		value["null_subscripts"]="ALWAYS"
      if  (value["null_subscripts"])
      {
	if (value["null_subscripts"] == "NOVAL")
		print "change -region " regname " -NULL_SUBSCRIPTS"
	else if (value["null_subscripts"] != ".")
		print "change -region " regname " -NULL_SUBSCRIPTS="value["null_subscripts"]
      }
}

function print_jnl(regname, addfn) {
      jnlarg = ""
      if (addfn && ((value["jnl_prefix"] != "***unspec***") || (value["jnl_suffix"] != "***unspec***")))
      {
	      prefix = (value["jnl_prefix"] != "***unspec***") ? value["jnl_prefix"] : ""
	      suffix = (value["jnl_suffix"] != "***unspec***") ? value["jnl_suffix"] : ".mjl"
	      jnlname = (regname == "DEFAULT") ? value["file_name"] : regname
	      jnlarg = "file_name=\"" prefix jnlname suffix "\""
      }
      if (0 < jnl_count)
      {
	      jnlarg = "(" jnlarg
	      for (jnlargnum=1; jnlargnum <= jnl_count; jnlargnum++)
	      {
		if (jnlarg != "(")
			jnlarg = jnlarg ","
		jnlarg = jnlarg value["journal" jnlargnum]
	      }
	      jnlarg = jnlarg ")"
      }
      if (jnlarg != "")
	      print "change -region " regname " -journal=" jnlarg
}

function print_out(x,seg_reg,name) {
     # x is the property, seg_reg is segment or region, name is segment or region name
     # so for segment or region, with name "name", the propery x is set to value[x]
     # if it is specified (not null or ".")
     # if value[x] matches anything but NULL or . (~/..*/ && !~/\./)
     #
     # Dbcreate also accepts multi-value strings of the form a:b:c, meaning a goes to first region, b to the next and c to the rest
     # Look for : in the values, use the first piece and remove it from the string for the next region to process
     if (((x == "record_size" && value[x] == "0") || value[x]) && value[x] != ".")
     {
        multi=index(value[x],":")
	if (multi)
	{
		val=substr(value[x],1,multi-1)
		value[x]=substr(value[x],multi+1,length(value[x]))
	}
	else
		val=value[x]
     	print "change -" seg_reg " " name " -" x "=" val
     }
}

#################################################################
#								#
# Copyright (c) 2002-2016 Fidelity National Information		#
# Services, Inc. and/or its subsidiaries. All rights reserved.	#
#								#
# Copyright (c) 2017-2018 YottaDB LLC. and/or its subsidiaries.	#
# All rights reserved.						#
#								#
#	This source code contains the intellectual property	#
#	of its copyright holder(s), and is made available	#
#	under a license.  If you do not know the terms of	#
#	the license, please stop and do not read further.	#
#								#
#################################################################
BEGIN { split("file_name n_regions key_size record_size block_size allocation global_buffer_count extension_count reserved_bytes collation_default null_subscripts access_method acc_meth_env journal test_collation test_stdnull_collation test_gtcm qdbrundown freeze defer_allocate epoch_taper lock_space", argmnts, " ")
	# name_override is an argument disguised from the user, used to take the $gtmgbldir definition from the shell if it exists
	# -different_gld is useful on replication tests with multi-region databases:
	#  	instead of a*, b*, c*, d* going to different regions, on the remote directories, they go in pairs: a* and b* to areg, c* and d* to creg, e* and f* to ereg, ...
	fn = "a b c d e f g h i j k l m n o p q r s t u v w x y z"
	FN = toupper(fn);
	split(fn,filenames," ")
	split(FN, FILENAMES," ")
	if (ENVIRON["tst_on_remote"]) tst_on_remote=1
	if (1 == ENVIRON["gtm_test_qdbrundown"]) value["qdbrundown"]="-qdbrundown"
	if (1 == ENVIRON["gtm_test_freeze_on_error"]) value["freeze"]="-inst_freeze_on_error"
	if (0 == ENVIRON["gtm_test_defer_allocate"]) value["defer_allocate"]="-nodefer_allocate"
	if (0 == ENVIRON["gtm_test_epoch_taper"]) value["epoch_taper"]="-noepochtaper"
	value["jnl_prefix"] = "***unspec***"
	value["jnl_suffix"] = "***unspec***"
	value["move"]=1
       }

      { for (i=1;i<=NF;i++)
	   {tmp=$i
	    gsub(/.*=/,"",tmp)
	    if (tmp == $i) tmp="NULL"
	    if ($i ~/-f/) value["file_name"]=tmp
	    else if ($i ~/^-bl/) value["block_size"]=tmp
	    else if ($i ~/^-al/) value["allocation"]=tmp
	    else if ($i ~/^-glo/) value["global_buffer_count"]=tmp
	    else if ($i ~/^-e/) value["extension_count"]=tmp
	    else if ($i ~/^-c/) value["collation_default"]=tmp
	    else if ($i ~/^-rec/) value["record_size"]=tmp
	    else if ($i ~/^-res/) value["reserved_bytes"]=tmp
	    else if ($i ~/^-k/) value["key_size"]=tmp
	    else if ($i ~/^-nom/) value["move"]=0
	    # null_subscripts has multiple entries to differentiate from n_reg
	    else if ($i ~/^-nu.*$/) value["null_subscripts"]=tmp
	    else if ($i ~/^-n$/) value["null_subscripts"]="NOVAL"
	    else if ($i ~/^-n=/) value["null_subscripts"]=tmp
	    else if ($i ~/^-nu=/) value["null_subscripts"]=tmp
	    else if ($i ~/^-acc_meth/) value["access_method"]=toupper(tmp)
	    else if ($i ~/^-acc_meth_env/) value["acc_meth_env"]=toupper(tmp)
	    else if ($i ~/^-journal/) value["journal" ++jnl_count]=toupper(tmp)
	    else if ($i ~/^-n_regions/) value["n_regions"]=tmp
	    else if ($i ~/^-qdbrundown/) value["qdbrundown"]="-qdbrundown"
	    else if ($i ~/^-noqdbrundown/) value["qdbrundown"]="-noqdbrundown"
	    else if ($i ~/^-inst_freeze_on_error/) value["freeze"]="-inst_freeze_on_error"
	    else if ($i ~/^-noinst_freeze_on_error/) value["freeze"]="-noinst_freeze_on_error"
	    else if ($i ~/^-test_collation/) value["test_collation"]=tmp
	    else if ($i ~/^-name_override/) value["filename_override"]=tmp
	    else if ($i ~/^-test_gtcm/)  value["test_gtcm"]=tmp
	    else if ($i ~/^-t/)  value["template"]=tmp #template
	    else if ($i ~/^-stdnull/) value["test_stdnull_collation"]="-stdnull"
	    else if ($i ~/^-nostdnull/) value["test_stdnull_collation"]="-nostdnull"
	    else if ($i ~/^-different_gld/) value["different_gld"]=1
	    else if ($i ~/^-gld_has_db_fullpath/) value["gld_has_db_fullpath"]=1
	    else if ($i ~/^-jnl_prefix=/) value["jnl_prefix"]=tmp
	    else if ($i ~/^-jnl_suffix=/) value["jnl_suffix"]=tmp
	    else if ($i ~/^-jnl_auto/) value["journal" ++jnl_count]="autoswitchlimit=" tmp
	    else if ($i ~/^-defer_allocate/) value["defer_allocate"]="-defer_allocate"
	    else if ($i ~/^-nodefer_allocate/) value["defer_allocate"]="-nodefer_allocate"
	    else if ($i ~/^-epoch_taper/) value["epoch_taper"]="-epochtaper"
	    else if ($i ~/^-noepoch_taper/) value["epoch_taper"]="-noepochtaper"
	    else if ($i ~/^-lock_space/) value["lock_space"]=tmp
	    else  { value[argmnts[i]]=$i;
		  }
	    }
      }

END {
      if (value["test_gtcm"])
      {
	#if the value is a "x;y,z" x is the local host, this means, just create the regions for that one.y or z must be same as x
	loc_semicolon=index(value["test_gtcm"],";")
	if (loc_semicolon)
	{
		# this is a GT.CM server, nullify all the others.
		this_host=substr(value["test_gtcm"],1,loc_semicolon-1);
		print "#This is a GT.CM server ("this_host"). Only some regions will be here"
		gsub(".*;","",value["test_gtcm"])
		this_is_a_gtcm_server=1
	}
	total_gtcm_serv_no=split(value["test_gtcm"],gtcm_servers,",")
	if (this_is_a_gtcm_server)
		for (serv in gtcm_servers)
			if (!index(gtcm_servers[serv],this_host":")) gtcm_servers[serv]=""
	gtcm_serv_no=1
	cur_gtcm_server = gtcm_servers[gtcm_serv_no++]
      }
      default_filename="mumps"
      sub("\\.gld$","",value["filename_override"])
      if (value["filename_override"]!="") default_filename=value["filename_override"]
      if (value["file_name"] !~/..*/ || value["file_name"] ~/\./) value["file_name"]=default_filename
      # Check if value["file_name"] points to a subdirectory. If so, cd to that subdir before creating the db
      nsubdirs = split(value["file_name"], fullpath, "/")
      if ((1 < nsubdirs) && ("" != fullpath[1]))	# fullpath[1] check needed to ensure it is not an absolute path
      {
	      subdir = ""
	      for (i = 1; i < nsubdirs; i++)
		subdir = subdir "" fullpath[i] "/"
	      printf "mkdir -p %s\n", subdir
	      printf "cd %s\n", subdir
	      value["file_name"] = fullpath[nsubdirs]
      }
      if (value["gld_has_db_fullpath"])
              value["file_name"] = ENVIRON["PWD"] "/" value["file_name"]
      print "setenv dbname " value["file_name"]
      print "setenv gtmgbldir \"$dbname.gld\""
      print "set timenow = `date +%H_%M_%S`"
      print "set renamecount = 0 ; set rename = \"$timenow\""
      print "if (-e $gtmgbldir) then"
      print "	while ( -e ${gtmgbldir}_$rename)"
      print "		@ renamecount++"
      print "		set rename = \"${timenow}_$renamecount\""
      print "	end"
      print " 	mv $gtmgbldir ${gtmgbldir}_$rename"
      print "endif"
      if (value["move"])
      {
	      print "set datpat = '*.dat'"
	      print "set nonomatch"
	      print "set datfiles = $datpat"
	      print "unset nonomatch"
	      print "if (\"$datfiles\" != \"$datpat\") then"
	      print "	foreach datfile ($datfiles)"
	      print "		mv $datfile ${datfile}_$rename"
	      print "	end"
	      print "endif"
      }
      print "if (($?test_replic) && (-e mumps.repl)) mv mumps.repl mumps.repl_$rename"
      if (value["access_method"] !~ /..*/ || value["access_method"] ~/\./) value["access_method"]=value["acc_meth_env"]
      if (value["access_method"] !~ /..*/ ) value["access_method"]="BG"
      print "setenv acc_meth " value["access_method"]
      #-------- temporary change till GDE works for GLOBAL_BUFFERS for MM   -- nars - 98/02/25_09:05am
      print "set mmglobalbuffs=0"
      #-------- temporary change ends ----------------------------------------------------------------
      #-------- temporary change till GDE works for GLOBAL_BUFFERS for MM   -- nars - 98/02/25_09:05am
      if (value["access_method"] == "MM") { printf "set mmglobalbuffs = "
				       if (value["global_buffer_count"] ~ /..*/ )
					   print value["global_buffer_count"]
				       else
					   print 1024
					   }
      #-------- temporary change ends ----------------------------------------------------------------

      if (value["template"])
	print "#DBCREATE-I-TEMPLATE Will use the file "value["template"] " for \n# template commands, and will not use any of the other settings"
      print "cat << \\_GDE_EOF >>&! dbcreate.gde"
      if (value["template"])
      {
	command1 = "cat "value["template"]
	command2 = "sed -n 's/template.*-segment/change -segment DEFAULT/gp' "value["template"]
	command3 = "sed -n 's/template.*-region/change -region DEFAULT/gp' "value["template"]
	system(command1" ; "command2" ; "command3)
      }

      {	#DEFAULT region
	      if (this_is_a_gtcm_server) cur_gtcm_server = "" ; #local
	      sub(/\.[^:]*:/,":",cur_gtcm_server)
	      print "change -segment DEFAULT -file_name=" cur_gtcm_server value["file_name"] ".dat"
	      if (value["access_method"] ~ /MM/)
		  {
		   if (value["global_buffer_count"] !~/..*/ || value["global_buffer_count"] ~/\./)
			value["global_buffer_count"]=1024
		   }

	      if (! value["template"])
	      print_block("DEFAULT","DEFAULT")
		    if (value["test_collation"]) print "change -region DEFAULT -collation_default=" value["test_collation"]
      }
      # If value["n_regions"] is not an integer, the for loop below would go forever.
      if ( (value["n_regions"] ~ /^[0-9]+$/) && (value["n_regions"]>1) )
	{
	 for (i=1;i<value["n_regions"];i++)
	   {
	    skip=0
	    if (value["test_gtcm"])
	    {
		    if (1 != i) {cur_gtcm_server = gtcm_servers[gtcm_serv_no++]; if (gtcm_serv_no > total_gtcm_serv_no) gtcm_serv_no=1}
		    #print "#Region no:"i " Server:" cur_gtcm_server
		    if ("" == cur_gtcm_server) skip = 1
		    # i=1 means region A, which is going to be local on the original (client) server
		    cur_gtcm_server_hostname = substr(cur_gtcm_server,1,index(cur_gtcm_server,":")-1)
		    # remove any domain bits from the hostname, leaving only the first part, and bracket it
		    if ("" != cur_gtcm_server_hostname)
		    {
			    sub(/\.[^:]*$/,"",cur_gtcm_server_hostname)
			    cur_gtcm_server = cur_gtcm_server_hostname substr(cur_gtcm_server,index(cur_gtcm_server,":"))
		    }
		    #if ((1 != i)&&(cur_gtcm_server_hostname != this_host)) skip = 1
		    if (gtcm_server[i]) skip = 1
		    #print "#XXXa" skip "." cur_gtcm_server "."this_host "." cur_gtcm_server_hostname"."
		    if ((1 == i)&&(this_is_a_gtcm_server)) skip = 1
	    }
	    if (!skip)
	    {
		    if ((1 == i)||(this_is_a_gtcm_server)) cur_gtcm_server = "" #local
		    if (tst_on_remote && value["different_gld"])
		    {
			    print "! different_gld-specified-in-input"	# this output is relied upon by dbcreate_base.csh
			    if (i%2)
			    {
				fname = filenames[i]".dat"
				seg = filenames[i]"seg"
				reg = filenames[i]"reg"
			    }
		    } else
		    {
			    fname = filenames[i]".dat"
			    seg = filenames[i]"seg"
			    reg = filenames[i]"reg"
		    }
		    if (value["gld_has_db_fullpath"])
			fname = ENVIRON["PWD"] "/" fname
		    print "add -name " filenames[i] "* -region="  reg
		    print "add -name " FILENAMES[i] "* -region="  reg
		    if (!(tst_on_remote && value["different_gld"] && !(i%2)))
		    {
			print "add -region " reg " -dyn=" seg
			if ((0 != value["test_gtcm"])&&(i>1))
			{
				   print "add -segment " seg " -file=" cur_gtcm_server fname
				   # to test broken database
				   # print "add -segment " seg " -file=" cur_gtcm_server fname
			} else
				    print "add -segment " seg " -file=" fname
			if (! value["template"])
				    print_block(seg, reg)
			# for all regions but A, change collation
			if (value["test_collation"]) if (i>1) print "change -region " reg " -collation_default=" value["test_collation"]
		    }
	    }
	    }
      }

      print "\\_GDE_EOF"
      print "$gtm_tst/com/usesprgde.csh >> dbcreate.gde"
      print "$convert_to_gtm_chset dbcreate.gde"
      print "$GDE_SAFE @dbcreate.gde"
    }

function print_block(segname,regname){
      if (value["access_method"] == "MM")
      {
	    print_jnl(regname, 0)
	    print_out("access_method", "segment", segname)
	    print "change -region " regname " -nojournal"
      }
      else
	    print_jnl(regname, 1)
      print_out("block_size", "segment",segname)
      print_out("allocation","segment",segname)
      if (value["access_method"] != "MM") print_out("global_buffer_count","segment",segname)
      print_out("extension_count","segment",segname)
      print_out("reserved_bytes","segment",segname)
      print_out("lock_space","segment",segname)
      print_out("record_size","region",regname)
      print_out("key_size","region",regname)
      print_out("collation_default","region",regname)
      if (value["test_stdnull_collation"])
		print "change -region " regname " " value["test_stdnull_collation"]
      major_ver = gensub(/^.*V([0-9])[0-9]+.*/,"\\1","g",ENVIRON["gtm_dist"])
      # QDBRUNDOWN and INST_FREEZE_ON_ERROR options are not available for versions older than V60000
      if (6 <= major_ver)
      {
	  if (value["freeze"])
	      print "change -region " regname " " value["freeze"]
	  if (value["qdbrundown"])
		print "change -region " regname " " value["qdbrundown"]
	  # DEFER_ALLOCATE option is available from V62002
	  if ((6 < major_ver) || (2002 <= gensub(/^.*V[0-9]([0-9]+).*/,"\\1","g",ENVIRON["gtm_dist"])))
	  {
	      if (value["defer_allocate"])
		  print "change -segment " segname " " value["defer_allocate"]
	      if (value["epoch_taper"])
		  print "change -region " regname " " value["epoch_taper"]
	  }
      }
      if  ("NULL" == value["null_subscripts"]) # this NULL has nothing to do with "NULL"_subscripts, it is dbcreate_multi.awk's "NULL"
		value["null_subscripts"]="ALWAYS"
      if  (value["null_subscripts"])
      {
	if (value["null_subscripts"] == "NOVAL")
		print "change -region " regname " -NULL_SUBSCRIPTS"
	else if (value["null_subscripts"] != ".")
		print "change -region " regname " -NULL_SUBSCRIPTS="value["null_subscripts"]
      }
}

function print_jnl(regname, addfn) {
      jnlarg = ""
      if (addfn && ((value["jnl_prefix"] != "***unspec***") || (value["jnl_suffix"] != "***unspec***")))
      {
	      prefix = (value["jnl_prefix"] != "***unspec***") ? value["jnl_prefix"] : ""
	      suffix = (value["jnl_suffix"] != "***unspec***") ? value["jnl_suffix"] : ".mjl"
	      jnlname = (regname == "DEFAULT") ? value["file_name"] : regname
	      jnlarg = "file_name=\"" prefix jnlname suffix "\""
      }
      if (0 < jnl_count)
      {
	      jnlarg = "(" jnlarg
	      for (jnlargnum=1; jnlargnum <= jnl_count; jnlargnum++)
	      {
		if (jnlarg != "(")
			jnlarg = jnlarg ","
		jnlarg = jnlarg value["journal" jnlargnum]
	      }
	      jnlarg = jnlarg ")"
      }
      if (jnlarg != "")
	      print "change -region " regname " -journal=" jnlarg
}

function print_out(x,seg_reg,name) {
     # x is the property, seg_reg is segment or region, name is segment or region name
     # so for segment or region, with name "name", the propery x is set to value[x]
     # if it is specified (not null or ".")
     # if value[x] matches anything but NULL or . (~/..*/ && !~/\./)
     #
     # Dbcreate also accepts multi-value strings of the form a:b:c, meaning a goes to first region, b to the next and c to the rest
     # Look for : in the values, use the first piece and remove it from the string for the next region to process
     if (((x == "record_size" && value[x] == "0") || value[x]) && value[x] != ".")
     {
        multi=index(value[x],":")
	if (multi)
	{
		val=substr(value[x],1,multi-1)
		value[x]=substr(value[x],multi+1,length(value[x]))
	}
	else
		val=value[x]
     	print "change -" seg_reg " " name " -" x "=" val
     }
}


SCCS mkdoc.awk 11/01/24 00:08:28: Updated Copyright

#-------------------------------------------------------------------------------
# Phorward Software Development Kit
# Copyright (C) 2009-2011 by Phorward Software Technologies, Jan Max Meyer
# http://www.phorward-software.com ++ contact<at>phorward<dash>software<dot>com
# All rights reserved. See $PHOME/LICENSE for more information.
#
# Script:		mkdoc.awk
# Author:		Jan Max Meyer
# Usage:		Template-based documentation generator
#-------------------------------------------------------------------------------

BEGIN	{
			SUBSEP = "#"

			print_sections = 1
			full_index = 0
			tpldir = getenv( "PHOME" ) "/tpl"
			manual = ""

			t = 1

			# Begin of program
			for( i = 1; i < ARGC; i++ )
			{
				if( ARGV[i] == "-o" )
					output = ARGV[++i]
				else if( ARGV[i] == "-i" )
					full_index = 1
				else if( ARGV[i] == "-ns" )
					print_sections = 0
				else if( ARGV[i] == "-tpl" && i+1 < ARGC )
					tpldir = ARGV[++i]
				else if( ARGV[i] == "-m" && i+1 < ARGC )
					manual = readfile( ARGV[++i] )
				else if( ARGV[i] == "-c" && i+1 < ARGC )
					allcopyright = ARGV[++i]
				else if( ARGV[i] == "-t" && i+1 < ARGC )
					alltitle = ARGV[++i]
				else if( ARGV[i] == "-u" && i+1 < ARGC )
					allusage = ARGV[++i]
				else
					files[ j++ ] = ARGV[i]
			}

			if( substr( tpldir, length( tpldir ) - 1, 1 ) != "/" )
				tpldir = tpldir "/"

			# Read all templates
			tpl[ "DOCUMENT" ] = readfile( tpldir "document.tpl" )
			tpl[ "MODULE" ] = readfile( tpldir "module.tpl" )
			tpl[ "FUNCTION" ] = readfile( tpldir "function.tpl" )
			tpl[ "parameters" ] = readfile( tpldir "parameter.tpl" )
			tpl[ "returns" ] = readfile( tpldir "returns.tpl" )
			tpl[ "refentry" ] = readfile( tpldir "refentry.tpl" )

			document = tpl[ "DOCUMENT" ]

			# Process the files
			for( i in files )
			{
				process( files[i] )

				if( print_sections )
				{
					#Output the header and index of contents
					gsub( "!!" "index", print_idx(), head_text )
					gsub( "!!" "contents", print_ref(), head_text )

					#all_text = all_text head_text
					#all_text = all_text print_ref()

					all_text = all_text head_text

					head_text = ""
				}

				if( full_index )
				{
					for( s in functions )
						all_functions[ s ] = functions[ s ]
				}

				for( s in functions )
					delete functions[s]
			}

			if( full_index )
			{
				for( s in all_functions )
					functions[ s ] = all_functions[ s ]

				if( !print_sections )
					all_text = print_ref()

				allindex = print_idx()
			}

			if( document != "" )
			{
				gsub( "!!" "title", alltitle, document )
				gsub( "!!" "usage", allusage, document )
				gsub( "!!" "copyright", allcopyright, document )
				gsub( "!!" "manual", manual, document )
				gsub( "!!" "index", allindex, document )
				gsub( "!!" "reference", all_text, document )
			}
			else
				document = all_text

			#Finally print all!
			print document
		}

function process( file )
{
	section = ""
	current_item = ""
	old_item = ""
	nodoc = 0
	line = 1

	while( getline line <file > 0 )
	{
		#this could be windoof files
		gsub( "\r", "", line )
		line = trim( line )

		section_line++

		if( line == "" )
		{
			if( current_item == "usage" || current_item == "copyright" )
				items[ current_item ] = items[ current_item ] "\n"

			continue
		}

		#Look for section?
		if( section == "NODOC" && index( line, "COD_ON" ) >= 1 )
			section = "";
		else if( section == "" )
		{
			if( index( line, "/* -MODULE" ) == 1 )
				section = "MODULE"
			else if( index( line, "/* -FUNCTION" ) == 1 )
				section = "FUNCTION"
			else if( index( line, "NO_DOC" ) >= 1 )
				section = "NODOC";

			current_item = ""
			section_line = 0
		}
		else if( section != "NODOC" )
		{
			#End of a section
			if( index( line, "--------------------------------------------" \
					"--------------------------------- */" ) == 1 \
				|| index( line, "~~~ CHANGES & NOTES ~~~" ) > 0 )
			{
				# create a function parameter synopsis!
				if( section == "FUNCTION" )
				{
					#Generate RETURNTYPE and RETURNDESC
					if( items[ "returns" ] != "" )
					{
						if( index( items[ "returns" ], " " ) )
						{
							datatype = substr( items[ "returns" ], 1, \
										index( items[ "returns" ], " " ) - 1 )
							desc = 	substr( items[ "returns" ], \
										index( items[ "returns" ], " " ) + 1 )
						}
						else
						{
							datatype = items[ "returns" ]
							desc = "Nothing"
						}

						items[ "returntype" ] = datatype

						if( desc == "" )
							desc = "Nothing."

						items[ "returndesc" ] = desc
					}

					#Generate function-name
					gsub( /\(.*\)$/, "", items[ "function" ] )

					#GENERATE SYNOPSIS
					items[ "synopsis" ] = items[ "function" ]
					#gsub( /\(.*\)$/, "", items[ "synopsis" ] )
					items[ "synopsis" ] = items[ "synopsis" ] "( "

					first = 1
					for( item in items )
					{
						if( match( item, "parameters" SUBSEP "[0-9]+" ) )
						{
							datatype = substr( items[ item ], 1, \
								index( items[ item ], " " ) - 1 )
							desc = substr( items[ item ], \
										index( items[ item ], " " ) + 1 )

							if( datatype != "..." )
							{
								param = substr( desc, 1, index( desc, " " ) - 1 )
								desc = 	substr( desc, index( desc, " " ) + 1 )
							}
							else
								param = ""

							if( !first )
								items[ "synopsis" ] = items[ "synopsis" ] ", "
							first = 0

							items[ "synopsis" ] = items[ "synopsis" ] datatype
							if( param != "" )
								items[ "synopsis" ] = items[ "synopsis" ] \
									" " param
						}
					}

					if( first )
						items[ "synopsis" ] = items[ "synopsis" ] "void )"
					else
						items[ "synopsis" ] = items[ "synopsis" ] " )"

					items[ "synopsis" ] = items[ "returntype" ] " " \
											items[ "synopsis" ]

					#Put code in array first, for sorting
					funcname =  items[ "function" ]
					#gsub( /_|\(|\)/, "", funcname )

					# GENERATE OUTPUT...
					#write index of contents as a quick reference
					if( !index( items[ "usage" ], "." ) )
						items[ "usage" ] = trim( items[ "usage" ] ) "."

					content[ funcname ] = substr( items[ "usage" ], \
											1, index( items[ "usage" ], "." ) )

					#Write output into functions array
					functions[ funcname ] = fill_tex_tpl( section, items )
				}
				else if( head_text == "" )
					head_text = fill_tex_tpl( section, items )

				#for( item in items )
					#print item " => \"" items[ item ] "\""

				for( item in items )
					delete items[ item ]

				section = ""
				current_item = ""
				continue
			}
			else if( index( line, "File:" ) == 1 )
				current_item = "file"
			else if( index( line, "Author:" ) == 1 )
				current_item = "author"
			else if( index( line, "Usage:" ) == 1 )
				current_item = "usage"
			else if( index( line, "Function:" ) == 1 )
				current_item = "function"
			else if( index( line, "Parameters:" ) == 1 )
			{
				akt_parm = 1
				current_item = "parameters" SUBSEP akt_parm
			}
			else if( index( line, "Returns:" ) == 1 )
			{
				current_item = "returns"
			}
			else if( section == "MODULE" && section_line == 1 )
				current_item = "title"
			else if( section == "MODULE" && section_line == 2 )
				current_item = "copyright"

			#Read in the item information
			if( current_item != "" )
			{
				if( current_item != old_item && items[ current_item ] == "" )
				{
					if( current_item != "title" \
						&& current_item != "copyright" )
					{
						line = trim( substr( line, index( line, ":" ) + 1 ) )
					}

					gsub( /[ \t]+/, " ", line )

					items[ current_item ] = line
				}
				else
				{
					if( match( current_item, "parameters" SUBSEP "[0-9]+" ) )
					{
						if( match( line, \
							"[A-Za-z0-9_*]+[ \t][ \t]+[A-Za-z0-9_*()]+[ \t]+|\\.\\.\\.[ \t]+" ) )
						{
							akt_parm++
							current_item = "parameters" SUBSEP akt_parm

							gsub( /[ \t]+/, " ", line )
							items[ current_item ] = line
						}
					}

					if( old_item == current_item )
					{
						gsub( /[ \t]+/, " ", line )

						if( ( section == "MODULE" && current_item == "usage" ) \
							|| current_item == "copyright" )
							delim = "\n"
						else
							delim = " "

						items[ current_item ] = items[ current_item ] delim line
					}
				}
			}

			old_item = current_item
		}

		line++;
	}

	close( getline line <file > 0 )
}

function fill_tex_tpl( section, items )
{
	final = tpl[ section ]
	parameters = ""

	for( item in items )
	{
		#print "!!" item " " items[ item ]

		#for LaTeX
		#gsub( "_", "\\_", items[ item ] )

		if( match( item, "parameters" SUBSEP "[0-9]+" ) )
		{
			datatype = substr( items[ item ], 1, \
						index( items[ item ], " " ) - 1 )
			desc = 	substr( items[ item ], index( items[ item ], " " ) + 1 )

			if( datatype != "..." )
			{
				param = substr( desc, 1, index( desc, " " ) - 1 )
				desc = 	substr( desc, index( desc, " " ) + 1 )
			}
			else
				param = ""

			subfinal = tpl[ "parameters" ]
			gsub( "!!" "datatype", datatype, subfinal )
			gsub( "!!" "param", param, subfinal )
			gsub( "!!" "desc", desc, subfinal )

			parameters = parameters subfinal
		}
		else if( item == "returns" )
		{
			subfinal = tpl[ "returns" ]
			gsub( "!!" "datatype", items[ "returntype" ], subfinal )
			gsub( "!!" "desc", items[ "returndesc" ], subfinal )

			gsub( "!!" item, subfinal, final )
		}
		else
		{
			gsub( "!!" item, items[ item ], final )
		}
	}

	if( section == "FUNCTION" )
		gsub( "!!parameters", parameters, final )

	return final
}

function print_idx()
{
	t = 1
	for( s in functions )
		sort[t++] = s

	n = asort( sort )

	refindex = ""
	for( s = 1; s <= n; s++ )
	{
		refentry = tpl[ "refentry" ]

		gsub( "!!" "function", sort[s], refentry )
		gsub( "!!" "usage", content[ sort[s] ], refentry )
		refindex = refindex refentry
	}

	for( s in sort )
		delete sort[s]

	return refindex
}

function print_ref()
{
	t = 1
	for( s in functions )
		sort[t++] = s

	n = asort( sort )

	text = ""
	for( s = 1; s <= n; s++ )
		text = text functions[sort[s]]

	for( s in sort )
		delete sort[s]

	return text
}

#--- Utility functions go here ---

function ltrim( s )
{
	sub( /^[ \t\n]+/, "", s )
	return s
}

function rtrim( s )
{
	sub( /[ \t\n]+$/, "", s )
	return s
}

function trim( s )
{
	return rtrim( ltrim( s ) )
}

function readfile( s )
{
	f = ""

	while( ( getline l < s ) > 0 )
		f = f l "\n"

	close( s )
	return f
}

function getenv( s )
{
	"echo $" s | getline v
	return v
}

#-------------------------------------------------------------------------------
# Phorward Software Development Kit
# Copyright (C) 2009, 2010 by Phorward Software Technologies, Jan Max Meyer
# http://www.phorward-software.com ++ contact<at>phorward<dash>software<dot>com
# All rights reserved. See $PHOME/LICENSE for more information.
#
# Script:		mkdoc.awk
# Author:		Jan Max Meyer
# Usage:		Template-based documentation generator
#-------------------------------------------------------------------------------

BEGIN	{
			SUBSEP = "#"

			print_sections = 1
			full_index = 0
			tpldir = getenv( "PHOME" ) "/tpl"
			manual = ""

			t = 1

			# Begin of program
			for( i = 1; i < ARGC; i++ )
			{
				if( ARGV[i] == "-o" )
					output = ARGV[++i]
				else if( ARGV[i] == "-i" )
					full_index = 1
				else if( ARGV[i] == "-ns" )
					print_sections = 0
				else if( ARGV[i] == "-tpl" && i+1 < ARGC )
					tpldir = ARGV[++i]
				else if( ARGV[i] == "-m" && i+1 < ARGC )
					manual = readfile( ARGV[++i] )
				else if( ARGV[i] == "-c" && i+1 < ARGC )
					allcopyright = ARGV[++i]
				else if( ARGV[i] == "-t" && i+1 < ARGC )
					alltitle = ARGV[++i]
				else if( ARGV[i] == "-u" && i+1 < ARGC )
					allusage = ARGV[++i]
				else
					files[ j++ ] = ARGV[i]
			}

			if( substr( tpldir, length( tpldir ) - 1, 1 ) != "/" )
				tpldir = tpldir "/"

			# Read all templates
			tpl[ "DOCUMENT" ] = readfile( tpldir "document.tpl" )
			tpl[ "MODULE" ] = readfile( tpldir "module.tpl" )
			tpl[ "FUNCTION" ] = readfile( tpldir "function.tpl" )
			tpl[ "parameters" ] = readfile( tpldir "parameter.tpl" )
			tpl[ "returns" ] = readfile( tpldir "returns.tpl" )
			tpl[ "refentry" ] = readfile( tpldir "refentry.tpl" )

			document = tpl[ "DOCUMENT" ]

			# Process the files
			for( i in files )
			{
				process( files[i] )

				if( print_sections )
				{
					#Output the header and index of contents
					gsub( "!!" "index", print_idx(), head_text )
					gsub( "!!" "contents", print_ref(), head_text )

					#all_text = all_text head_text
					#all_text = all_text print_ref()

					all_text = all_text head_text

					head_text = ""
				}

				if( full_index )
				{
					for( s in functions )
						all_functions[ s ] = functions[ s ]
				}

				for( s in functions )
					delete functions[s]
			}

			if( full_index )
			{
				for( s in all_functions )
					functions[ s ] = all_functions[ s ]

				if( !print_sections )
					all_text = print_ref()

				allindex = print_idx()
			}

			if( document != "" )
			{
				gsub( "!!" "title", alltitle, document )
				gsub( "!!" "usage", allusage, document )
				gsub( "!!" "copyright", allcopyright, document )
				gsub( "!!" "manual", manual, document )
				gsub( "!!" "index", allindex, document )
				gsub( "!!" "reference", all_text, document )
			}
			else
				document = all_text

			#Finally print all!
			print document
		}

function process( file )
{
	section = ""
	current_item = ""
	old_item = ""
	nodoc = 0
	line = 1

	while( getline line <file > 0 )
	{
		#this could be windoof files
		gsub( "\r", "", line )
		line = trim( line )

		section_line++

		if( line == "" )
		{
			if( current_item == "usage" || current_item == "copyright" )
				items[ current_item ] = items[ current_item ] "\n"

			continue
		}

		#Look for section?
		if( section == "NODOC" && index( line, "COD_ON" ) >= 1 )
			section = "";
		else if( section == "" )
		{
			if( index( line, "/* -MODULE" ) == 1 )
				section = "MODULE"
			else if( index( line, "/* -FUNCTION" ) == 1 )
				section = "FUNCTION"
			else if( index( line, "NO_DOC" ) >= 1 )
				section = "NODOC";

			current_item = ""
			section_line = 0
		}
		else if( section != "NODOC" )
		{
			#End of a section
			if( index( line, "--------------------------------------------" \
					"--------------------------------- */" ) == 1 \
				|| index( line, "~~~ CHANGES & NOTES ~~~" ) > 0 )
			{
				# create a function parameter synopsis!
				if( section == "FUNCTION" )
				{
					#Generate RETURNTYPE and RETURNDESC
					if( items[ "returns" ] != "" )
					{
						if( index( items[ "returns" ], " " ) )
						{
							datatype = substr( items[ "returns" ], 1, \
										index( items[ "returns" ], " " ) - 1 )
							desc = 	substr( items[ "returns" ], \
										index( items[ "returns" ], " " ) + 1 )
						}
						else
						{
							datatype = items[ "returns" ]
							desc = "Nothing"
						}

						items[ "returntype" ] = datatype

						if( desc == "" )
							desc = "Nothing."

						items[ "returndesc" ] = desc
					}

					#Generate function-name
					gsub( /\(.*\)$/, "", items[ "function" ] )

					#GENERATE SYNOPSIS
					items[ "synopsis" ] = items[ "function" ]
					#gsub( /\(.*\)$/, "", items[ "synopsis" ] )
					items[ "synopsis" ] = items[ "synopsis" ] "( "

					first = 1
					for( item in items )
					{
						if( match( item, "parameters" SUBSEP "[0-9]+" ) )
						{
							datatype = substr( items[ item ], 1, \
								index( items[ item ], " " ) - 1 )
							desc = substr( items[ item ], \
										index( items[ item ], " " ) + 1 )

							if( datatype != "..." )
							{
								param = substr( desc, 1, index( desc, " " ) - 1 )
								desc = 	substr( desc, index( desc, " " ) + 1 )
							}
							else
								param = ""

							if( !first )
								items[ "synopsis" ] = items[ "synopsis" ] ", "
							first = 0

							items[ "synopsis" ] = items[ "synopsis" ] datatype
							if( param != "" )
								items[ "synopsis" ] = items[ "synopsis" ] \
									" " param
						}
					}

					if( first )
						items[ "synopsis" ] = items[ "synopsis" ] "void )"
					else
						items[ "synopsis" ] = items[ "synopsis" ] " )"

					items[ "synopsis" ] = items[ "returntype" ] " " \
											items[ "synopsis" ]

					#Put code in array first, for sorting
					funcname =  items[ "function" ]
					#gsub( /_|\(|\)/, "", funcname )

					# GENERATE OUTPUT...
					#write index of contents as a quick reference
					if( !index( items[ "usage" ], "." ) )
						items[ "usage" ] = trim( items[ "usage" ] ) "."

					content[ funcname ] = substr( items[ "usage" ], \
											1, index( items[ "usage" ], "." ) )

					#Write output into functions array
					functions[ funcname ] = fill_tex_tpl( section, items )
				}
				else if( head_text == "" )
					head_text = fill_tex_tpl( section, items )

				#for( item in items )
					#print item " => \"" items[ item ] "\""

				for( item in items )
					delete items[ item ]

				section = ""
				current_item = ""
				continue
			}
			else if( index( line, "File:" ) == 1 )
				current_item = "file"
			else if( index( line, "Author:" ) == 1 )
				current_item = "author"
			else if( index( line, "Usage:" ) == 1 )
				current_item = "usage"
			else if( index( line, "Function:" ) == 1 )
				current_item = "function"
			else if( index( line, "Parameters:" ) == 1 )
			{
				akt_parm = 1
				current_item = "parameters" SUBSEP akt_parm
			}
			else if( index( line, "Returns:" ) == 1 )
			{
				current_item = "returns"
			}
			else if( section == "MODULE" && section_line == 1 )
				current_item = "title"
			else if( section == "MODULE" && section_line == 2 )
				current_item = "copyright"

			#Read in the item information
			if( current_item != "" )
			{
				if( current_item != old_item && items[ current_item ] == "" )
				{
					if( current_item != "title" \
						&& current_item != "copyright" )
					{
						line = trim( substr( line, index( line, ":" ) + 1 ) )
					}

					gsub( /[ \t]+/, " ", line )

					items[ current_item ] = line
				}
				else
				{
					if( match( current_item, "parameters" SUBSEP "[0-9]+" ) )
					{
						if( match( line, \
							"[A-Za-z0-9_*]+[ \t][ \t]+[A-Za-z0-9_*()]+[ \t]+|\\.\\.\\.[ \t]+" ) )
						{
							akt_parm++
							current_item = "parameters" SUBSEP akt_parm

							gsub( /[ \t]+/, " ", line )
							items[ current_item ] = line
						}
					}

					if( old_item == current_item )
					{
						gsub( /[ \t]+/, " ", line )

						if( ( section == "MODULE" && current_item == "usage" ) \
							|| current_item == "copyright" )
							delim = "\n"
						else
							delim = " "

						items[ current_item ] = items[ current_item ] delim line
					}
				}
			}

			old_item = current_item
		}

		line++;
	}

	close( getline line <file > 0 )
}

function fill_tex_tpl( section, items )
{
	final = tpl[ section ]
	parameters = ""

	for( item in items )
	{
		#print "!!" item " " items[ item ]

		#for LaTeX
		#gsub( "_", "\\_", items[ item ] )

		if( match( item, "parameters" SUBSEP "[0-9]+" ) )
		{
			datatype = substr( items[ item ], 1, \
						index( items[ item ], " " ) - 1 )
			desc = 	substr( items[ item ], index( items[ item ], " " ) + 1 )

			if( datatype != "..." )
			{
				param = substr( desc, 1, index( desc, " " ) - 1 )
				desc = 	substr( desc, index( desc, " " ) + 1 )
			}
			else
				param = ""

			subfinal = tpl[ "parameters" ]
			gsub( "!!" "datatype", datatype, subfinal )
			gsub( "!!" "param", param, subfinal )
			gsub( "!!" "desc", desc, subfinal )

			parameters = parameters subfinal
		}
		else if( item == "returns" )
		{
			subfinal = tpl[ "returns" ]
			gsub( "!!" "datatype", items[ "returntype" ], subfinal )
			gsub( "!!" "desc", items[ "returndesc" ], subfinal )

			gsub( "!!" item, subfinal, final )
		}
		else
		{
			gsub( "!!" item, items[ item ], final )
		}
	}

	if( section == "FUNCTION" )
		gsub( "!!parameters", parameters, final )

	return final
}

function print_idx()
{
	t = 1
	for( s in functions )
		sort[t++] = s

	n = asort( sort )

	refindex = ""
	for( s = 1; s <= n; s++ )
	{
		refentry = tpl[ "refentry" ]

		gsub( "!!" "function", sort[s], refentry )
		gsub( "!!" "usage", content[ sort[s] ], refentry )
		refindex = refindex refentry
	}

	for( s in sort )
		delete sort[s]

	return refindex
}

function print_ref()
{
	t = 1
	for( s in functions )
		sort[t++] = s

	n = asort( sort )

	text = ""
	for( s = 1; s <= n; s++ )
		text = text functions[sort[s]]

	for( s in sort )
		delete sort[s]

	return text
}

#--- Utility functions go here ---

function ltrim( s )
{
	sub( /^[ \t\n]+/, "", s )
	return s
}

function rtrim( s )
{
	sub( /[ \t\n]+$/, "", s )
	return s
}

function trim( s )
{
	return rtrim( ltrim( s ) )
}

function readfile( s )
{
	f = ""

	while( ( getline l < s ) > 0 )
		f = f l "\n"

	close( s )
	return f
}

function getenv( s )
{
	"echo $" s | getline v
	return v
}


Use FILENAME built-in variable.

Use FILENAME built-in variable.

# CASE-MAPPING.awk -- awk script to generate CASE-MAPPING.tab
# Copyright (C) 2005
#   National Institute of Advanced Industrial Science and Technology (AIST)
#   Registration Number H15PRO112

# This file is part of the m17n database; a sub-part of the m17n
# library.

# The m17n library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.

# The m17n library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with the m17n library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307, USA.

BEGIN {
  FS = "[ \t]*;[ \t]*";
}

function mtext (str) {
  if (n = split (str, A, " "))
    {
      printf ("\"");
      for (i = 1; i <= n; i++)
	printf ("\\u%s", A[i]);
      printf ("\"");
    }
  else
    printf ("\"\\u%s\"", str);
  printf (" ");
}

/^[^\#]/ {
  if (FILENAME == "UNIDATA/SpecialCasing.txt" && NF == 5) {
    printf ("0x%s ( ", $1);
    mtext ($2);
    mtext ($3);
    mtext ($4);
    printf (")\n");
    X[$1] = 1;
  }

  else if (($13 || $14 || $15) && ! X[$1]) {
    if (! $13) $13 = $1;
    if (! $14) $14 = $1;
    if (! $15) $15 = $1;
    printf ("0x%s ( \"\\u%s\" \"\\u%s\" \"\\u%s\" )\n",
	    $1, $14, $15, $13);
  }
}

# CASE-MAPPING.awk -- awk script to generate CASE-MAPPING.tab
# Copyright (C) 2005
#   National Institute of Advanced Industrial Science and Technology (AIST)
#   Registration Number H15PRO112

# This file is part of the m17n database; a sub-part of the m17n
# library.

# The m17n library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.

# The m17n library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with the m17n library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307, USA.

BEGIN {
  FS = ";";
  FILE = 1;
}

function mtext (str) {
  if (n = split (str, A, " "))
    {
      printf ("\"");
      for (i = 1; i <= n; i++)
	printf ("\\u%s", A[i]);
      printf ("\"");
    }
  else
    printf ("\"\\u%s\"", str);
  printf (" ");
}

FILE == 1 && /^[^\#]/ && NF == 5 {
  printf ("0x%s ( ", $1);
  mtext ($2);
  mtext ($3);
  mtext ($4);
  printf (")\n");
  X[$1] = 1;
  next;
}

/^0000;/ {
  FILE = 2;
}

FILE == 2 && /^[^\#]/ && ($13 || $14 || $15) && ! X[$1] {
  if (! $13) $13 = $1;
  if (! $14) $14 = $1;
  if (! $15) $15 = $1;
  printf ("0x%s ( \"\\u%s\" \"\\u%s\" \"\\u%s\" )\n",
	  $1, $14, $15, $13);
}


Fix awk lint #chunks

Fix awk lint #chunks

match($0, /^mix ([^ ]+) +# +(.*)/, arr) { print arr[1] "\t" arr[2] }

# https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html
#
# sub(regexp, replacement [, target])

{
  print "something" > "/dev/stderr"
}

match($0, /^mix ([^ ]+) +# +(.*)/, arr) { print arr[1] "\t" arr[2] }

# https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html
#
# sub(regexp, replacement [, target])

print "something" > "/dev/stderr"


Deleted awk script (old dependency)

Deleted awk script (old dependency)

/string  *"xesam:artist/{
    while (1) {
        getline line
        if (line ~ /string "/) {
            sub(/.*string "/, "artist:", line)
            sub(/".*$/, "", line)
            print line
            break
        }
    }
}
/string  *"xesam:title/{
    while(1) {
        getline line
        if (line ~ /string "/) {
            sub(/.*string "/, "title:", line)
            sub(/".*$/, "", line)
            print line
            break
        }
    }
}


HvD: The SVN Id: string looks a little bit different from the CVS one. However, we cannot dispose of the CVS one as this appears in a quite a few Global Array files.  So I simply added a special set of commands to deal with SVN Id: strings to ensure they are properly extracted.

#
# This script is used by the makefile in building util_version.f.
# It expects on standard input lines containing
#
# a CVS ID    -> produce a write statement with the ID ("Exp" at the end)
# a SVN ID    -> produce a write statement with the ID (no "Exp" at the end)
# module name -> produce a write statement for the module name
# other       -> produce nothing
#
# $Id$

BEGIN {
		underline = "---------------------------------------------------------";
		printf("      subroutine util_version\n");
                printf("#include \"global.fh\"\n");
                printf("      if (ga_nodeid().eq.0) then\n");
		printf("      write(6,*)\n");
		printf("      write(6,*) ' Software version information'\n");
		printf("      write(6,*) ' ----------------------------'\n");
}

/^module/			{
				 len = length($0);
				 printf("      write(6,*)\n");
				 printf("      write(6,*) ' %s'\n",$0);
				 printf("      write(6,*) ' %s'\n",substr(underline,1,len));
				}

/\$\I\d: [^\n]*Exp \$/		{
				 i = index($0, "$Id: ") + 5;
				 j = index($0, "Exp $") - 1;
				 n = j - i + 1;
				 if (n > (72 - 20)) n = 72 - 20;
				 printf("      write(6,*) ' %s'\n",substr($0,i,n));
				}

/\$\I\d: [^\n]* \$/		{
				 i = index($0, "$Id: ") + 5;
				 n = index(substr($0,i), " $") - 1;
				 if (n > (72 - 20)) n = 72 - 20;
				 printf("      write(6,*) ' %s'\n",substr($0,i,n));
				}

END {
		printf("      write(6,*)\n");
		printf("      call util_flush(6)\n");
                printf("      endif\n");
                printf("      call ga_sync()\n");
		printf("      end\n");
}

#
# This script is used by the makefile in building util_version.f.
# It expects on standard input lines containing
#
# a CVS ID    -> produce a write statement with the ID
# module name -> produce a write statement for the module name
# other       -> produce nothing
#
# $Id$

BEGIN {
		underline = "---------------------------------------------------------";
		printf("      subroutine util_version\n");
                printf("#include \"global.fh\"\n");
                printf("      if (ga_nodeid().eq.0) then\n");
		printf("      write(6,*)\n");
		printf("      write(6,*) ' Software version information'\n");
		printf("      write(6,*) ' ----------------------------'\n");
}

/^module/			{
				 len = length($0);
				 printf("      write(6,*)\n");
				 printf("      write(6,*) ' %s'\n",$0);
				 printf("      write(6,*) ' %s'\n",substr(underline,1,len));
				}

/\$\I\d: [^\n]*Exp \$/		{
				 i = index($0, "$Id: ") + 5;
				 j = index($0, "Exp $") - 1;
				 n = j - i + 1;
				 if (n > (72 - 20)) n = 72 - 20;
				 printf("      write(6,*) ' %s'\n",substr($0,i,n));
				}

END {
		printf("      write(6,*)\n");
		printf("      call util_flush(6)\n");
                printf("      endif\n");
                printf("      call ga_sync()\n");
		printf("      end\n");
}


For now, comment out the broken part

For now, comment out the broken part

#! /usr/bin/awk -f
# Contributed by Christian MICHON <christian_michon@yahoo.fr> to
#   eliminate the compile time dependancy on perl introduced by
#   Erik's older initfini.pl
# vim:ai:sw=2:

BEGIN \
{ alignval="";
  endp=0;
  end=0;
  system("/bin/touch crt[in].S");
  system("/bin/rm -f crt[in].S");
  omitcrti=0;
  omitcrtn=0;
  glb_idx = 0;
  while(getline < "initfini.s")
  { if(/\.endp/) {endp=1}
    if(/\.end/) {end=1}
    if(/\.align/) {alignval=$2}
# here comes some special stuff for the SuperH targets
# Search for all labels
#    if(/_GLOBAL_OFFSET_TABLE_/) {
#      sub (":","",last);
#      glb_label[glb_idx] = last;
#      glb_idx += 1;
#      glb = $0;
#    }
    last = $1;
  }
  close("initfini.s");
}

/^_init:/{omitcrtn=1;if (glb_idx>0) print glb_label[0] ":" glb >> "crti.S";}
/^_fini:/{omitcrtn=1;if (glb_idx>1) print glb_label[1] ":" glb >> "crti.S";}
/HEADER_ENDS/{omitcrti=1;omitcrtn=1;getline}
/PROLOG_BEGINS/{omitcrti=0;omitcrtn=0;getline}
/i_am_not_a_leaf/{getline}
/PROLOG_PAUSES/{omitcrti=1;getline}
/PROLOG_UNPAUSES/{omitcrti=0;getline}
/PROLOG_ENDS/{omitcrti=1;getline}
/EPILOG_BEGINS/{omitcrtn=0;getline}
/EPILOG_ENDS/{omitcrtn=1;getline}
/TRAILER_BEGINS/{omitcrti=0;omitcrtn=0;getline}

/END_INIT/ \
{ if(endp)
  { gsub("END_INIT",".endp _init",$0)
  }
  else
  { if(end)
    { gsub("END_INIT",".end _init",$0)
    }
    else
    { gsub("END_INIT","",$0)
    }
  }
}

/END_FINI/ \
{ if(endp)
  { gsub("END_FINI",".endp _fini",$0)
  }
  else
  { if(end)
    { gsub("END_FINI",".end _fini",$0)
    }
    else
    { gsub("END_FINI","",$0)
    }
  }
}

/ALIGN/ \
{ if(alignval!="")
  { gsub("ALIGN",sprintf(".align %s",alignval),$0)
  }
  else
  { gsub("ALIGN","",$0)
  }
}
# substitude all label references of the _GLOBAL_OFFSET_TABLE_
# .L10  ==> .L10b etc.
glb_idx>0 { gsub (glb_label[0],sprintf("%sb",glb_label[0]),$0)}
glb_idx>1 { gsub (glb_label[1],sprintf("%sb",glb_label[1]),$0)}

omitcrti==0 {print >> "crti.S"}
omitcrtn==0 {print >> "crtn.S"}

END \
{ close("crti.S");
  close("crtn.S");
}

#! /usr/bin/awk -f
# Contributed by Christian MICHON <christian_michon@yahoo.fr> to
#   eliminate the compile time dependancy on perl introduced by
#   Erik's older initfini.pl
# vim:ai:sw=2:

BEGIN \
{ alignval="";
  endp=0;
  end=0;
  system("/bin/touch crt[in].S");
  system("/bin/rm -f crt[in].S");
  omitcrti=0;
  omitcrtn=0;
  glb_idx = 0;
  while(getline < "initfini.s")
  { if(/\.endp/) {endp=1}
    if(/\.end/) {end=1}
    if(/\.align/) {alignval=$2}
# here comes some special stuff for the SuperH targets
# Search for all labels
    if(/_GLOBAL_OFFSET_TABLE_/) {
      sub (":","",last);
      glb_label[glb_idx] = last;
      glb_idx += 1;
      glb = $0;
    }
    last = $1;
  }
  close("initfini.s");
}

/^_init:/{omitcrtn=1;if (glb_idx>0) print glb_label[0] ":" glb >> "crti.S";}
/^_fini:/{omitcrtn=1;if (glb_idx>1) print glb_label[1] ":" glb >> "crti.S";}
/HEADER_ENDS/{omitcrti=1;omitcrtn=1;getline}
/PROLOG_BEGINS/{omitcrti=0;omitcrtn=0;getline}
/i_am_not_a_leaf/{getline}
/PROLOG_PAUSES/{omitcrti=1;getline}
/PROLOG_UNPAUSES/{omitcrti=0;getline}
/PROLOG_ENDS/{omitcrti=1;getline}
/EPILOG_BEGINS/{omitcrtn=0;getline}
/EPILOG_ENDS/{omitcrtn=1;getline}
/TRAILER_BEGINS/{omitcrti=0;omitcrtn=0;getline}

/END_INIT/ \
{ if(endp)
  { gsub("END_INIT",".endp _init",$0)
  }
  else
  { if(end)
    { gsub("END_INIT",".end _init",$0)
    }
    else
    { gsub("END_INIT","",$0)
    }
  }
}

/END_FINI/ \
{ if(endp)
  { gsub("END_FINI",".endp _fini",$0)
  }
  else
  { if(end)
    { gsub("END_FINI",".end _fini",$0)
    }
    else
    { gsub("END_FINI","",$0)
    }
  }
}

/ALIGN/ \
{ if(alignval!="")
  { gsub("ALIGN",sprintf(".align %s",alignval),$0)
  }
  else
  { gsub("ALIGN","",$0)
  }
}
# substitude all label references of the _GLOBAL_OFFSET_TABLE_
# .L10  ==> .L10b etc.
glb_idx>0 { gsub (glb_label[0],sprintf("%sb",glb_label[0]),$0)}
glb_idx>1 { gsub (glb_label[1],sprintf("%sb",glb_label[1]),$0)}

omitcrti==0 {print >> "crti.S"}
omitcrtn==0 {print >> "crtn.S"}

END \
{ close("crti.S");
  close("crtn.S");
}


parsegaspriceshtml.awk: Updated to work with changed simpler HTML.

They use to have tricky image offsets for the price.
They recently changed that to plain text. Thanks!

BEGIN {
   False=0
   True=!False
   i=0
   inGasPriceRecord=False
   expectName=False
}

// {
   sub("^.*
]*>","")
   sub("
.*$","")
   # print
   price[i]=$0
}

inGasPriceRecord && /
/ {
   sub("^[ ]*
","")
   sub("
[ ]*$","")
   sub("&","\\&")
   address[i,ai[i]]=$0
   sub("^[ ]*","",address[i,ai[i]])
   # print "address["ai[i]"]="address[i,ai[i]]
   ai[i]++
}

inGasPriceRecord && /
/ {
   expectName=True
}

inGasPriceRecord && expectName && / <a href="/ {
   gsub("","Marathon")
   gsub("","BP")
   gsub("","Thorntons")
   gsub("","Thorntons")
   gsub("","Certified")
   gsub("","Marathon")
   expectName=False
   sub("[ ]*$","")
   sub("^.*>","")
   sub("&","\\&")
   name[i]=$0
   # print
}

inGasPriceRecord && / class="p_area">/ {
   sub("^.* class=\"p_area\">","")
   sub("[ ]*$","")
   area[i]=$0
   # print
}

inGasPriceRecord && /<a href="\/Profile[.]aspx[?]member=/ {
   sub("^.* ",",")
   sub("<.*$","")
   split($0,foo,",")
   reporter[i]=foo[1]
   time[i]=foo[2]
   timeAgo[i]=foo[3]
   # print
}

inGasPriceRecord && /<\/tr>/ {
   i ++
   # print "EOR"
   # print $0
   inGasPriceRecord=False
}

END {
   for (j=0;j<i && True;j++) {
      if (False) {
         print j
         print price[j]
         print name[j]
         print address[j,0]
         print area[j]
         print reporter[j]
         print time[j]
         print timeAgo[j]
      }

      delimiter="\t"
      print price[j] delimiter name[j] delimiter address[j,0] delimiter area[j] delimiter reporter[j] delimiter time[j] delimiter timeAgo[j]
   }
}

BEGIN {
   False=0
   True=!False
   i=0
   inGasPriceRecord=False
   expectName=False
}

/
<div class=\"p","")
   sub("^.*

.*$","")
   # print
   price[i]=$0
}

inGasPriceRecord && /
/ {
   sub("^[ ]*
","")
   sub("
[ ]*$","")
   sub("&","\\&")
   address[i,ai[i]]=$0
   sub("^[ ]*","",address[i,ai[i]])
   # print "address["ai[i]"]="address[i,ai[i]]
   ai[i]++
}

inGasPriceRecord && /
/ {
   expectName=True
}

inGasPriceRecord && expectName && / <a href="/ {
   gsub("","Marathon")
   gsub("","BP")
   gsub("","Thorntons")
   gsub("","Thorntons")
   gsub("","Certified")
   gsub("","Marathon")
   expectName=False
   sub("[ ]*$","")
   sub("^.*>","")
   sub("&","\\&")
   name[i]=$0
   # print
}

inGasPriceRecord && / class="p_area">/ {
   sub("^.* class=\"p_area\">","")
   sub("[ ]*$","")
   area[i]=$0
   # print
}

inGasPriceRecord && /<a href="\/Profile[.]aspx[?]member=/ {
   sub("^.* ",",")
   sub("<.*$","")
   split($0,foo,",")
   reporter[i]=foo[1]
   time[i]=foo[2]
   timeAgo[i]=foo[3]
   # print
}

inGasPriceRecord && /<\/tr>/ {
   i ++
   # print "EOR"
   # print $0
   inGasPriceRecord=False
}

END {
   for (j=0;j<i && True;j++) {
      if (False) {
         print j
         print price[j]
         print name[j]
         print address[j,0]
         print area[j]
         print reporter[j]
         print time[j]
         print timeAgo[j]
      }

      delimiter="\t"
      print price[j] delimiter name[j] delimiter address[j,0] delimiter area[j] delimiter reporter[j] delimiter time[j] delimiter timeAgo[j]
   }
}