#!/bin/bash

#tmp=/tmp/Ci.$$
#trap 'rm $tmp 2>/dev/null' 0

database=/home/omnister/public_html/botany/cp/data/jans.db

pgrep=/home/omnister/public_html/bin/pgrep
awk=awk

accepted=0
cultivar=0
while [ $# -gt 0 ] ; do
    case $1 in
        -C )    cultivar=1; shift ;;
        -A )    accepted=1; shift ;;
        * )     break ;;
        -* )    echo "$0: script error: option $1 is not handled" 1>&2; exit
    esac
done

# Now only pattern is left

pat=$*
pwd=`pwd`

# if the -A flag is given then adjust pattern search to 
# only allow accepted name entries.
#
if [ $accepted = 1 ]
then
    pat="[NFG]:.\%2b.*$pat"
fi

# if the -C flag is given then adjust pattern search to 
# only allow cultivar name entries.
#
if [ $cultivar = 1 ]
then
    pat="[NFG]:.\%24.*$pat"
fi

#echo "pattern = $pat"
#echo "pwd = $pwd"

##############################################################
# The old nawk-based system version of pgrep: 
# 	pgrep -i $pat < $database | nawk '
# is replaced by a custom program that runs ~10x faster
# The "-u" option treats the pattern as URL encoded
##############################################################

if [ $accepted = 1 ]
then
    echo "<h3> Query results: Accepted names only </h3>"
elif [ $cultivar = 1 ]
then
    echo "<h3> Query results: Cultivar names only </h3>"
else
    echo "<h3> Query results: All matching fields</h3>"
fi

echo "<hr>"

eval $pgrep -iu \'$pat\' < $database | $awk '

    BEGIN {
	 #initialize LFR translation array:
	 lfr[1] ="Arctic";		lfr[2] ="Atlantic Europe";	
	 lfr[3] ="Central Europe";	lfr[4] ="Illyria or Balkan";	
	 lfr[5] ="Pontus Euxinus";	lfr[6] ="Caucasus";	
	 lfr[7] ="Eastern Europe";	lfr[8] ="Northern Europe";	
	 lfr[9] ="Western Siberia";	lfr[10] ="Altai-Sayan";	
	lfr[11] ="Central Siberia";	lfr[12] ="Transbaikalia";	
	lfr[13] ="Northeastern Siberia";	
	lfr[14] ="Okhotsk-Kamchatka";	lfr[15] ="Canada incl. Great Lakes";
	lfr[16] ="Manchuria";	
	lfr[17] ="Sakhalin-Hokkaido";	lfr[18] ="Japan-Korea";	
	lfr[19] ="Volcano-Bonin";	lfr[20] ="Ryukyu or Tokara-Okinawa";	
	lfr[21] ="Taiwan";		lfr[22] ="Northern China";	
	lfr[23] ="Central China";	lfr[24] ="Southeastern China";	
	lfr[25] ="Sikang-Yunnan";	lfr[26] ="Northern Burma";	
	lfr[27] ="Eastern Himalaya";	lfr[28] ="Khasi-Manipur";	
	lfr[29] ="Appalachians";	
	lfr[30] ="Atlantic and Gulf Coastal Plain";	
	lfr[31] ="North American Prairies";	lfr[32] ="Vancouver";	
	lfr[33] ="Rocky Mountains";	lfr[34] ="Azores";	
	lfr[35] ="Madeira";		lfr[36] ="Canaries";	
	lfr[37] ="Cape Verde";		lfr[38] ="Southern Morocco";	
	lfr[39] ="Southwestern Mediterranean";	
	lfr[40] ="South Mediterranean";	
	lfr[41] ="Iberia";		lfr[42] ="Baleares";	
	lfr[43] ="Liguria-Tyrrhenia";	lfr[44] ="Adriatic";	
	lfr[45] ="East Mediterranean";	lfr[46] ="Crimea-Novorossijsk";	
	lfr[47] ="Sahara";		lfr[48] ="Egypt-Arabia";	
	lfr[49] ="Mesopotamia";		lfr[50] ="Central Anatolia";	
	lfr[51] ="Armenia-Iran";	lfr[52] ="Hyrcania";	
	lfr[53] ="Turania or Aralo-Caspia";	
	lfr[54] ="Turkestan";		lfr[55] ="Northern Baluchistan";	
	lfr[56] ="Western Himalaya";	lfr[57] ="Central Tien Shan";	
	lfr[58] ="Dzungaria-Tien Shan";	lfr[59] ="Mongolia";	
	lfr[60] ="Tibet";		lfr[61] ="Great Basin";	
	lfr[62] ="California";		lfr[63] ="Sonora";	
	lfr[64] ="Mexican Highlands";	lfr[65] ="Upper Guinea";	
	lfr[66] ="Nigeria-Cameroon";	lfr[67] ="Congo";	
	lfr[68] ="Zanzibar-Inhambane";	lfr[69] ="Tongoland-Pondoland";	
	lfr[70] ="Zambezi";		lfr[71] ="Sahel";	
	lfr[72] ="Sudan";		lfr[73] ="Somalia-Ethiopia";	
	lfr[74] ="South Arabia";	lfr[75] ="Socotra";	
	lfr[76] ="Oman";		lfr[77] ="South Iran";	
	lfr[78] ="Sindia";		lfr[79] ="Namibia";	
	lfr[80] ="Namaland";		lfr[81] ="Western Cape";	
	lfr[82] ="Karoo";	
	lfr[83] ="St. Helena and Ascension";	
	lfr[84] ="Eastern Madagascar";	lfr[85] ="Western Madagascar";	
	lfr[86] ="Southern and Southwestern Madagascar";	
	lfr[87] ="Comoro";		lfr[88] ="Mascarenes";	
	lfr[89] ="Seychelles";		lfr[90] ="Ceylon/Sri Lanka";	
	lfr[91] ="Malabar";		lfr[92] ="Deccan";	
	lfr[93] ="Upper Gangetic Plain";	
	lfr[94] ="Bengal";		lfr[95] ="South Burma";	
	lfr[96] ="Andamanes";		lfr[97] ="South China";	
	lfr[98] ="Thailand";		lfr[99] ="North Indochina";	
       lfr[100] ="Annam";		lfr[101] ="South Indochina";	
       lfr[102] ="Malaya";		lfr[103] ="Borneo";	
       lfr[104] ="Philippines";		lfr[105] ="Sumatra";	
       lfr[106] ="South Malesia";	lfr[107] ="Celebes";	
       lfr[108] ="Muluccas";		lfr[109] ="Papua";	
       lfr[110] ="Bismarck Archipelago";	
       lfr[111] ="New Hebrides";	lfr[112] ="Fiji";	
       lfr[113] ="Micronesia";		lfr[114] ="Polynesia";	
       lfr[115] ="Hawaii";		lfr[116] ="New Caledonia";	
       lfr[117] ="Central America";	lfr[118] ="West Indies";	
       lfr[119] ="Galapagos";		lfr[120] ="Guayana";	
       lfr[121] ="Amazonia";		lfr[122] ="Llanos";	
       lfr[123] ="Caatingas";	
       lfr[124] ="Central Brazilian Uplands";	
       lfr[125] ="Chaco";		lfr[126] ="Atlantic Brazil";	
       lfr[127] ="Parana";		lfr[128] ="Northern Andes";	
       lfr[129] ="Central Andes";	lfr[130] ="Cape";	
       lfr[131] ="North Australia";	lfr[132] ="Queensland";	
       lfr[133] ="Southeast Australia";	lfr[134] ="Tasmania";	
       lfr[135] ="Southwest Australia";	lfr[136] ="Eremaea";	
       lfr[137] ="Juan Fernandez";	lfr[138] ="Northern Chile";	
       lfr[139] ="Central Chile";	lfr[140] ="Pampas";	
       lfr[141] ="Patagonia";		lfr[142] ="Tierra del Fuego";	
       lfr[143] ="Tristan-Gough";	lfr[144] ="Kerguelen";	
       lfr[145] ="Lord Howe";		lfr[146] ="Norfolk";	
       lfr[147] ="Kermadec";		lfr[148] ="New Zealand North Is.";	
       lfr[149] ="New Zealand N South Is.";	
       lfr[150] ="New Zealand S South Is.";	
       lfr[151] ="Chatham";	
       lfr[152] ="New Zealand Subantarctic Islands";	
   }


    /./ {
	# this pattern counts on matching every line
	# and allowing lines to fall through to the
        # next cases... (no "next" command)
	matches++
    }

    /^[^+]/ {
	# if a line does not begin with a "+", then we
	# are not in a text block, so set intext=0 and
	# fall through to next case
	if (intext) {
	    print "</blockquote>"
	}
	intext=0;
    }

    /^N: +/ {
	print "<dl>"
	print "<dt> <b>" $0 "</b>"
	bold=1
	next
    }

    /^N:/ {
	print "<dl>"
	print "<dt>" $0
	bold=0
	next
    }

    /^VN:/ {
	print "<dl>"
	print "<dt>" $0
	bold=0
	next
    }

    /^F:/ {
	print "<h1>" $0 "</h1>"
	next
    }

    /^LFR:/ {
	x=0
	gsub("LFR: ","");
	gsub(" ","");
	n = split($0,a,",")
	if(bold)  printf("<b>"); 
	if (n>1) {
		printf("<dl><dt>")
	} else {
		printf("<dd>")
	}
	printf("LFR:");
	if (n>1) {
		printf("<DL COMPACT>");
	}
	for (i=1; i<=n; i++) {
	    if (a[i] ~ "?") {
		possible = "?"
	    } else {
		possible = ""
	    }
	    gsub("?","",a[i])
	    if (n>1) printf("<DT>");
	    printf("%3d:\n", a[i])
	    if (n>1) printf("<DD>");
	    printf("%s%s\n", possible, lfr[a[i]])
	} 
	if (n>1) printf("</dl></DL>\n");
	if(bold)  printf("</b>"); 
	next;
    }
    
    /^G:/ {
	print "<h1>" $0 "</h1>"
	next
    }

    # if a TEXT directive is found, the generate the hot-link to
    # the descriptive html file.
    #
    /^TEXT:/ {
	line = $0
	sub("^TEXT: ","",line)
	n=split(line, fields, " ")
	printf("<dd> <b> <A ")
	printf("HREF=\"/botany/cp/pictures/%s\">text: %s: </A></b> ",fields[1], fields[1]);
	for (i=2; i<=n; i++) {
	    printf("%s ",fields[i]);
	}
	printf("\n");
	next
    }

    # if a LINK directive is found, the generate the hot-link to the web page
    #
    /^LINK:/ {
	line = $0
	sub("^LINK: ","",line)
	n=split(line, fields, " ")
	printf("<dd> <b> <A ")
	printf("HREF=\"%s\">%s: </A></b> ",fields[1], fields[1]);
	for (i=2; i<=n; i++) {
	    printf("%s ",fields[i]);
	}
	printf("\n");
	next
    }

    # if an IMAGE directive is found, the generate the hot-link to
    # the descriptive html file.
    #
    /^IMAGE:/ {
	line = $0
	sub("^IMAGE: ","",line)
	n=split(line, fields, " ")
	printf("<dd><b> <A ")
	printf("HREF=\"/botany/cp/pictures/%s.htm\">image: %s: </A> </b>",fields[1], fields[1]);
	for (i=2; i<=n; i++) {
	    printf("%s ",fields[i]);
	}
	printf("\n");
	next
    }
    
    /^[A-Za-z]*:/ {
	if (bold) {
	    print "<dd>" "<b>" $0 "</b>" "</dd>"
	} else {
	    print "<dd>" $0 "</dd>"
	}
	next
    }

    /^[ ]*$/ ||
    /^$/ {
	print "</dl>"
	bold=0
	next
    }

    #text continuation line
    /^+/ {
	if (!intext) {
	    print "<blockquote>"
	}
	intext++

	xyz = $0
	sub("^+[ ]*","",xyz)
	print xyz
	next;
    }

    // {
	# ignore anything not explicitly matched
	next;
    }

    END {
        if (matches == 0) {
            print "<h2> No matches found! </h2>"
            if ( '$accepted' == 1) { 
                print "<p>If you are searching for something other than"
                print "an accepted plant name, (eg: author, location, date,"
                print " etc.), then you might want to try using the"
                print "<b>\"show all matching entries\"</b> option."
                print "This option will allow your search string"
                print "to match <em>any field</em> in the database rather"
                print "than just the <em>plant name field</em>.</p>"
	    } else if ( '$cultivar' == 1) {
                print "<p>If you are searching for something other than"
                print "a cultivar name, (eg: the author, location,"
		print "date, etc.), then you might want to try using the"
                print "<b>\"show all matching entries\"</b> option."
                print "This option will allow your search string"
                print "to match <em>any field</em> in the database rather"
                print "than just the <em>plant name field</em>.</p>"
	    } else {
		print "<p>Your search string does not match anything in"
		print "the <b>Carnivorous Plant Database</b>.  Perhaps you are"
		print "misspelling your query.  You might try a shorter"
		print "abbreviation of your word, or a different spelling."
		print "If you still have trouble, then perhaps you might"
		print "try reading about how"
		print "<a href=/botany/cp/html/srchpatn.htm>search patterns</a> work."
	    }
        }
    }

'
