#!/opt/hpwebsuite/perl5/bin/perl
#
#
# This will filter MARC records from the zclient and add HTML formatting.
#
# Author: Harold Finkbeiner (harold@lindy.stanford.edu)
#

$HREF = "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}$ENV{SCRIPT_NAME}?";
$BREF = '<a href="' . $HREF;    # Matching quote (") for perl f
$PREF = $BREF . $ZDB;

if( !($ENV{QUERY_STRING} =~ /USMARC/))
{$ENV{QUERY_STRING} .= "&filtername=USMARC/marc.pl";
}

$HYPERTAG = 'NONE';
$LOCALTAG = 'NONE';



print '<DL>' . $/;
if (defined($FORM{marc})) {

    $cache = '';

    while (&read_marc_record()) { &marc_display(); }
}
elsif ($elemname eq 'F')  {

    $cache = '';

    &setup_hypertext();

    while (&read_marc_record()) { &full_display(); }
}
else {
    if ($cache) {
        $cache = '' unless open(CACHE, ">/tmp/$cache");
    }
        $flag = 1;
    while (&read_marc_record()) { &brief_display(); }

    close(CACHE) if ($cache);
}

print '</DL>' . $/;

sub full_display {

    $_ = "$BREF$ENV{QUERY_STRING}";
    $_ .= '&records=1'  unless (s/&records=\d+/&records=1/);
    s/&start=\d+//;
    print "<DT><H3>$_&marc\">$DISWRD $DISNUM</A></H3>" . $/;

    if ($FULL) { &display_marc_record($FULL); }
    else       { &default_full_marc_record(); }
    print << "ENDE";
ENDE
    &print_local_link() if ($LOCALIDX);
}

sub marc_display {

    print "<DT><H3>$DISWRD $DISNUM</A></H3><DD>";
    print '<PRE>';

    for ($i = 0; $i <= $#TAG; $i++) {
         $VALUE[$i] =~ tr/\x1F/$/;
         print '<BR><B>' . $TAG[$i] . '</B>' . $VALUE[$i];
    }

    print '</PRE>';
}

sub brief_display {
    if ($KEY_ACCESS) {
        ($KEYTAG, $KEYINDEX, $KEYCMD) = eval("($KEY_ACCESS)");
    }

    $link = &link_to_record();

    print "<DT><B>$link\">$DISWRD $DISNUM</A></B><DD>";
    if ($BRIEF) { &display_marc_record($BRIEF); }
    else        { &default_brief_marc_record(); }

    print "\n";
}

sub read_marc_record {

#   Initialize

    @TAG = ();
    @VALUE = ();

    $LOCALIDX = '';
    $LOCALCNT = 0;

    if ($RECORD_HEADER_READ) {
        $_ = $RECORD_HEADER_READ;
        $RECORD_HEADER_READ = '';
    }
    else {
        $_ = eof(STDIN) ? '' : <STDIN>;
    }

    if ($_) {
        print CACHE $_ if ($cache);

        ($DISWRD, $DISNUM) = split;

# für deutsche Anzeige hinzugefügt
#       if($ZDB =~ /db_ge/)
#       {$DISWRD = "Eintrag";}


        $DISNUM = $FORM{disnum} if (  (defined($FORM{disnum})) && (!$flag) );

        while (<STDIN>) {
            print CACHE $_ if ($cache);
            chop;
            last unless ($_);
            $tag = substr($_,0,3);
            $value = substr($_,4);
            push(@TAG,$tag);
            push(@VALUE,$value);
            &create_local_link($tag, $value) if ($tag =~ m/$LOCALTAG/o);
        }
        $_ = 'CONTINUE';
    }
    return $_;
}

sub print_label {
    local($label,$field,$subfields,$subfsep,$endpunct,$cmd) = @_;

    $value = '';

    TAGLOOP: for ($i = 0; $i <= $#TAG; $i++) {
        if ($TAG[$i] =~ m/$field/) {
            if ($TAG[$i] =~ m/$HYPERTAG/o) {
                #
                # A hypertexted Marc field
                #

                ($hypersubfield, $index, $hcmd) = split(' ', $HYPERLINK{$TAG[$i]}, 3);

                $subfields = '.' unless ($subfields);

                foreach (split("\x1F",substr($VALUE[$i],3))) {
                    if (m/^$subfields/) {

                        $sf = substr($_,0,1);

                        $text = substr($_,2);

                        if ($sf =~ m/^$hypersubfield/) {
                            $_ = &hypertext_value($text, $index, $hcmd);
                            $text =~  s/&/&amp;/g;
                            $text =~  s/</&lt;/g;
                            $text =~  s/>/&gt;/g;
                            $text = $PREF . $_ . "$ZINFO\">$text</A>" if ($_);

                        }
                        $value .= $subfsep if ($value);
                        $value .= $text;
                    }
                }
            }
            else {
                if (!$subfields) {
                    $_ = $VALUE[$i];
                    eval($cmd) if ($cmd);
                    s/^.. \x1F. *//g;
                    s/\x1F./$subfsep/g;
                    $value .= $subfsep if ($value);
                    $value .= $_;
                }
                else {
                    foreach (split("\x1F",substr($VALUE[$i],3))) {
                        if (m/^$subfields/) {
                            eval($cmd) if ($cmd);
                            $values .= s/&/&amp;/g;
                            $values .= s/</&lt;/g;
                            $values .= s/>/&gt;/g;
                            $value .= $subfsep if ($value);
                            $value .= substr($_,2);

#                           $value =~ s/&/&amp;/g;
#                           $value =~ s/</&lt;/g;
#                           $value =~ s/>/&gt;/g;
        
                        }
                    }
                }
            }
            $TAG[$i] = '   ';
        }
    }

    $value .= $subfsep if ($value && $PREVIOUS);
    $value .= $PREVIOUS;

    if ($label) {
# diese Bedingung wurde für die Anzeige des URL hinzugefügt
        if($value)
        {
                if($label =~ /HREF=/)
                {
                        if($elemname eq "F")
                        {
                                print "<DT><B>URL:</B>\n";
                                $value =~ s/\; //;

                                split('http://', $value);
                                foreach(@_)
                                {       if ($_){
                                                print "<DD><A HREF=http://$_>http://$_</A>\n";
                                        }
                                }
                        }
                        else
                        {
                                split('http://', $value);
                                foreach(@_)
                                {
                                        if ($_){
                                                        print "<DD><IMG SRC=http://$_>\n";
        
                                        }
                                }

                        }
                }

                else
                {
                        print "$label$value$endpunct\n" if ($value);
                        $PREVIOUS = '';
                }
        }
    }
    else {
        $PREVIOUS = $value;
    }
}

sub display_marc_record {
    local($fields) = shift;

    # Use the value of $fields to output each label

    foreach (split("\n", $fields)) {
        &print_label(eval("($_)"));
    }
}

sub default_full_marc_record
{
if($ZDB =~ /db_en/)
{

    &print_label('<DT><B>document number:</B><DD> '  , '010', '[a]', ' ', "\n")
;
    &print_label('<DT><B>ISBN:</B><DD> ', '020', '[a]', ' ', "\n");
    &print_label('<DT><B>ISSN:</B><DD> ', '022', '[a]', ' ', "\n");
    &print_label('<DT><B>Issue Number:</B><DD> ', '028', '[a-z]', ', ', "\n");
    &print_label('<DT><B>Coden:</B><DD> ', '030', '[a]', ' ', "\n");
    &print_label('<DT><B>Record Number:</B><DD> ', '060', '[b]', ' ', "\n");
    &print_label('<DT><B>Standard Number:</B><DD> ', '069', '[a]', ' ', "\n");
    &print_label('<DT><B>Government Document Number:</B><DD> ', '086', '[a-z]', ', ', "\n");
    &print_label('<DT><B>personal name:</B><DD> ' , '100', '[a-z]', ' <BR>', ".\n");
    &print_label('<DT><B>corporate name:</B><DD> ' , '110', '[a-z]', ' <BR>', ".\n");
    &print_label('<DT><B>Author:</B><DD> ' , '1..', '[a-z]', ' <BR>', ".\n");
    &print_label('<DT><B>abbreviated title:</B><DD> '  , '210', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Also Known As:</B><DD> '  , '211', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>variant title:</B><DD> '  , '212', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Augmented Title:</B><DD> '  , '214', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>uniform title:</B><DD> '  , '240', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Collective Title:</B><DD> '  , '243', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>title:</B><DD> '  , '245', '[a-z]', '', ".\n");

    &print_label('<DT><B>title variant:</B><DD> '  , '246', '[ahnpbgfde]', '<BR>', ".\n");
    &print_label('<DT><B>Former Title:</B><DD> '  , '247', '', '[ahnpbgfde]', ".\n");
    &print_label('<DT><B>publication:</B><DD> ', '260', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>date of production:</B><DD> ', '261', '[d]', ' ', ".\n");
    &print_label('<DT><B>place of production:</B><DD> ', '262', '[f]', ' ', ".\n");
    &print_label('<DT><B>imprint:</B><DD> ', '262', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>date:</B><DD> ', '263', '[a]', ' ', ".\n");
    &print_label('<DT><B>city:</B><DD> ', '270', '[b]', ' ', ".\n");
    &print_label('<DT><B>Techical Report Number:</B><DD> ', '26[02]', '[a-z]',', ', "\n");
    &print_label('<DT><B>description:</B><DD> '  , '300', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>filmtyp:</B><DD> ','340', '[a]', '<DD>',"\n");
    &print_label('<DT><B>description positive, negative:</B><DD> ','340', '[i]', '<DD>',"\n");
    &print_label('<DT><B>physical description:</B><DD> '  , '30[15]', '[a-z]',' ', ".\n");
    &print_label('<DT><B>geographic code:</B><DD> ' , '431', '[a-z]', '<DD> ',"\n");
    &print_label('<DT><B>serie:</B><DD> ' , '440', '[a-z]', '<BR>', "\n");
    &print_label('<DT><B>series:</B><DD> ' , '490', '[a-z]', '<BR>', "\n");
    &print_label('<DT><B>content:</B><DD> '  , '505', '[a-z]', ' ',"\n");
    &print_label('<DT><B>Participant:</B><DD> '  , '511', '[a-z]', ' ',"\n");
    &print_label('<DT><B>abstract:</B><DD> '  , '520', '[a-z]', '<BR>',"\n");
    &print_label('<DT><B>date of aquisition:</B><DD> ', '541', '[d]', ' ',"\n");
    &print_label('<DT><B>language:</B><DD> '  , '546', '[a-z]', ' ',"\n");
    &print_label('<DT><B>Publication Type:</B><DD> ', '567', '[a-z]', ' ',"\n");
    &print_label('<DT><B>source:</B><DD> ' , '550', '[a-z]', ' ',"\n", 's|^.. \x1F. *([^\x1F]+)|"<CITE>$1</CITE> "|ge;');
    &print_label('<DT><B>note:</B><DD> '  , '500', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>dissertation note:</B><DD> '  , '502', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>note:</B><DD> '  , '5..', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>description:</B><DD> '  , '520', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>local subject:</B><DD> ','650', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>subject:</B><DD> ','653', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>place:</B><DD> ','651', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>res_type:</B><DD> ','655', '[a-z]', '<DD>',".\n");
    &print_label('<DT><B>format:</B><DD> ','656', '[a-z]', '<DD>',".\n");
    &print_label('<DT><B>source:</B><DD> ','7..', '[a-z]', ', ',".\n");
    &print_label('<DT><B>positive- negative- nr.:</B><DD> ','841', '[a-z]', ',',"\n");
    &print_label('<DT><B>URL:</B><DD><A HREF="','856', '[u]', '',"\">Link\</A>\n");
#   &print_label('<DT><B>URL:</B><DD> ','856', '[u]', ' ',"\n", 'if (/^3.(.*)/) {$u = $1; $_ = ""} s|^u. *(.*)[ \n]$|"u <A HREF=\"$1\">$u\</A>"|e');
#   &print_label('<DT><B>URL:</B><DD> ','856', '[u]', ' ',"\n", 's|^u. *(.*)[ \n]$|"u <A HREF=\"$1\">$1\</A>"|e');
    &print_label('<DT><B>URL:</B><DD><A HREF= ','856', '[u]', ' ',">Link\</A>");
    &print_label('<DT><B>local classification:</B><DD> ','084', '[a]', ' ',".\n");
    &print_label('<DT><B>local call number:</B><DD> ', '090', '[a-z]', ', ', "\n");
    &print_label('<DT><B>Call Number:</B><DD> ', '90', '[a-z]', ', ', "\n");
    &print_label('<DT><B>Holdings:</B><DD> ','999', '[a-z]', ' ',".\n");
    &print_label('<DT><B>series:</B><DD> ','8..', '[a-z]', ' ',".\n");


}
else
{
    &print_label('<DT><B>Dokumentnummer:</B><DD> '  , '010', '[a]', ' ', "\n");
    &print_label('<DT><B>ISBN:</B><DD> ', '020', '[a]', ' ', "\n");
    &print_label('<DT><B>ISSN:</B><DD> ', '022', '[a]', ' ', "\n");
    &print_label('<DT><B>Issue Number:</B><DD> ', '028', '[a-z]', ', ', "\n");
    &print_label('<DT><B>Coden:</B><DD> ', '030', '[a]', ' ', "\n");
    &print_label('<DT><B>Record Number:</B><DD> ', '060', '[b]', ' ', "\n");
    &print_label('<DT><B>Standard Number:</B><DD> ', '069', '[a]', ' ', "\n");
    &print_label('<DT><B>Government Document Number:</B><DD> ', '086', '[a-z]', ', ', "\n");
    &print_label('<DT><B>Personenname:</B><DD> ' , '100', '[a-z]', ' <BR>', ".\n");
    &print_label('<DT><B>Körperschaft:</B><DD> ' , '110', '[a-z]', ' <BR>', ".\n");
    &print_label('<DT><B>Autor:</B><DD> ' , '1..', '[a-z]', ' <BR>', ".\n");
    &print_label('<DT><B>Abbreviated Title:</B><DD> '  , '210', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Also Known As:</B><DD> '  , '211', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Variant Title:</B><DD> '  , '212', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Augmented Title:</B><DD> '  , '214', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Einheitssachtitel:</B><DD> '  , '240', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Collective Title:</B><DD> '  , '243', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Titel:</B><DD> '  , '245', '[a-z]', '', ".\n");
    &print_label('<DT><B>Titelvariante:</B><DD> '  , '246', '[ahnpbgfde]', '<BR>', ".\n");
    &print_label('<DT><B>Former Title:</B><DD> '  , '247', '', '[ahnpbgfde]', ".\n");
    &print_label('<DT><B>Verlag:</B><DD> ', '260', '[a,b,c]', ' ', ".\n");
    &print_label('<DT><B>Verlag / Druck:</B><DD> ', '260', '[b]', ' ', ".\n");
    &print_label('<DT><B>Erscheinungsjahr:</B><DD> ', '260', '[c]', ' ', ".\n");
    &print_label('<DT><B>Verlag:</B><DD> ', '260', '[d-z]', ' ', ".\n");
    &print_label('<DT><B>Entstehungsdatum:</B><DD> ', '261', '[d]', ' ', ".\n");
    &print_label('<DT><B>Aufnahmeort:</B><DD> ', '262', '[f]', ' ', ".\n");
    &print_label('<DT><B>Imprint:</B><DD> ', '262', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Datierung:</B><DD> ', '263', '[a]', ' ', ".\n");
    &print_label('<DT><B>Stadt:</B><DD> ', '270', '[b]', ' ', ".\n");
    &print_label('<DT><B>Techical Report Number:</B><DD> ', '26[02]', '[a-z]',', ', "\n");
    &print_label('<DT><B>Umfang:</B><DD> '  , '300', '[a-z]', ' ', ".\n");
    &print_label('<DT><B>Filmtyp:</B><DD> ','340', '[a]', '<DD>',"\n");
    &print_label('<DT><B>techn. Beschreibung Pos. und Neg.:</B><DD> ','340', '[i]', '<DD>',"\n");
    &print_label('<DT><B>Physical Description:</B><DD> '  , '30[15]', '[a-z]',' ', ".\n");
    &print_label('<DT><B>Landes- Bezirks- Code:</B><DD> ' , '431', '[a-z]', '<DD> ', "\n");
    &print_label('<DT><B>Serie:</B><DD> ' , '440', '[a-z]', '<BR>', "\n");
    &print_label('<DT><B>Serien:</B><DD> ' , '490', '[a-z]', '<BR>', "\n");
    &print_label('<DT><B>Inhalt:</B><DD> '  , '505', '[a-z]', ' ',"\n");
    &print_label('<DT><B>Participant:</B><DD> '  , '511', '[a-z]', ' ',"\n");
    &print_label('<DT><B>Abstract:</B><DD> '  , '520', '[a-z]', '<BR>',"\n");
    &print_label('<DT><B>Aquisitionsdatum:</B><DD> ', '541', '[d]', ' ',"\n");
    &print_label('<DT><B>Sprache:</B><DD> '  , '546', '[a-z]', ' ',"\n");
    &print_label('<DT><B>Publication Type:</B><DD> ', '567', '[a-z]', ' ',"\n");
    &print_label('<DT><B>Quelle:</B><DD> ' , '550', '[a-z]', ' ',"\n", 's|^.. \x1F. *([^\x1F]+)|"<CITE>$1</CITE> "|ge;');
    &print_label('<DT><B>Fußnote:</B><DD> '  , '500', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>Hochschulschriftenvermerk:</B><DD> '  , '502', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>Fußnote:</B><DD> '  , '5..', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>Beschreibung:</B><DD> '  , '520', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>Schlagwort:</B><DD> ','650', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>Thesaurus:</B><DD> ','653', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>Ort:</B><DD> ','651', '[a-z]', '<DD>',"\n");
    &print_label('<DT><B>Res_type:</B><DD> ','655', '[a-z]', '<DD>',".\n");
    &print_label('<DT><B>Format:</B><DD> ','656', '[a-z]', '<DD>',".\n");
    &print_label('<DT><B>Quelle:</B><DD> ','7..', '[a-z]', ', ',".\n");
    &print_label('<DT><B>Positiv- Negativ- Nr.:</B><DD> ','841', '[a-z]', ', ',"\n");
    &print_label('<DT><B>URL:</B><DD><A HREF="','856', '[u]', '',"\">Link\</A>\n");
#   &print_label('<DT><B>URL:</B><DD> ','856', '[u]', ' ',"\n", 'if (/^3.(.*)/) {$u = $1; $_ = ""} s|^u. *(.*)[ \n]$|"u <A HREF=\"$1\">$u\</A>"|e');
#   &print_label('<DT><B>URL:</B><DD> ','856', '[u]', ' ',"\n", 's|^u. *(.*)[ \n]$|"u <A HREF=\"$1\">$1\</A>"|e');
    &print_label('<DT><B>lokale Systematik:</B><DD> ','084', '[a]', ' ',".\n");
    &print_label('<DT><B>lokale Signatur:</B><DD> ', '090', '[a-z]', ', ', "\n");
    &print_label('<DT><B>Standort:</B><DD> ','999', '[a-z]', ' ',".\n");
    &print_label('<DT><B>Series:</B><DD> ','8..', '[a-z]', ' ',".\n");
    &print_label('<DT><B>Signatur:</B><DD> ', '090', '[a-z]', ', ', "\n");

}

}




sub default_brief_marc_record {
    &print_label('', '1..', '.', ', ', '', 's/\s+$//; $_ .= "." if (m/[^.]$/);');
    &print_label(' "', '245', '', ' ', '"');
    &print_label(' ', '550', '', ' ', '');
    &print_label(' (', '045', '.', ' ', ')', 'tr/\[\]//d; s/^\s+//;');
    &print_label('<DT><B>URL:</B><DD><A HREF="','856', '[u]', '',"\">Link\</A>\n");
    &print_label('<DD><IMG SRC="','856', '[u]', ' <BR>',"\">");


}

sub link_to_record {

    #
    # Key access uses a key to access the record directly.  First
    # see if you can match the tag which holds the key.
    #

    $vcache = ($cache) ? "&cache=$cache" : '';

    if ($KEY_ACCESS) {
        for ($i = 0; $i <= $#TAG; $i++) {
            if ( $TAG[$i] =~ m/$KEYTAG/o ) {

                # A key exists, alter it and trim blanks.

                $_ = &hypertext_value($VALUE[$i], $KEYINDEX, $KEYCMD);
                return $PREF . $_ . "&filtername=USMARC/marc.pl&disnum=$DISNUM&records=1". $vcache;
# return $PREF . $_ . "&filtername=USMARC&records=1". $vcache;



            }
        }
    }

    #
    # This is a regular link, repeat the search and set the $start to
    # pick up the record in the appropriate postion.
    #

    $_ = $BREF . $ENV{QUERY_STRING};

    $_ .= "&start=$DISNUM" unless (s/&start=\d+/&start=$DISNUM/);
    $_ .= '&records=1'     unless (s/&records=\d+/&records=1/);
    $_ .= '&elemname=F'    unless (s/&elemname=\w+/&elemname=F/);
    return $_ . $vcache ;
}

sub create_local_link {
    local($tag, $value) = @_;

    local($subfields, $index, $lop, $hcmd) = split(' ', $LOCALLINK{$tag}, 4);

    $subfields = '.' unless ($subfields);

    foreach (split("\x1F",substr($value,3))) {
        if (m/^$subfields/) {

            $_ = &hypertext_value(substr($_,2), $index, $hcmd, $LOCALCNT);

            if ($_) {
                $LOCALIDX .= "&lop$LOCALCNT=$lop" if ($LOCALIDX);

                $LOCALIDX .= $_;

                $LOCALCNT++;
            }
        }
    }
}

sub print_local_link {

    if (defined $LOCAL_SUBMIT) {
        print "<DT>\n<FORM METHOD=GET ACTION=\"";
        $_ = $HREF;
        s/search.CGI/searchspi.CGI/ if ($ENV{SERVER_NAME} =~ m/stanford.edu$/);
        print "$_\">\n";
        print "<INPUT NAME=QS TYPE=hidden VALUE=\"$LOCAL_PROFILE$LOCALIDX$ZINFO\">\n";
        print "<INPUT TYPE=submit VALUE=\"Search $LOCAL_NAME\">\n";
        print "</FORM>";
    }
    else {
        print "<DT><BR><IMG SRC=image/Find.gif ALIGN=MIDDLE ALT=Find><B> ";
        print 'this record in ';
        $_ = $BREF . $LOCAL_PROFILE . $LOCALIDX . $ZINFO;
        s/search.CGI/searchspi.CGI/ if ($ENV{SERVER_NAME} =~ m/stanford.edu$/);
        print "$_\">$LOCAL_NAME</A></B>\n";
    }

}

sub hypertext_value {

    local($value, $index, $hcmd, $indexcnt) = @_;

    $indexcnt = '0' unless ($indexcnt);

    #
    # Create a hypertext link from a Marc field
    #

    $_ = $value;
                        

    # Trim leading and trailing blanks
    # This has been benchmarked to be the fastest

    s/^\s+//; s/\s+$//;

    # Perform possible command on link.
    # This can be used to filter the search text.

    eval($hcmd) if ($hcmd);

    # If nothing left then do not perform a
    # hypertext link and just used the regular
    # text.

    if ($_) {
        # Convert special characters to ascii with % escape character.

        s|([\x00-\x20\"\#%&/;<>?\x7F-\xFF])|sprintf("%%%02lx",unpack('C',$1))|ge; # The " balances the quote marks for the editor.
                
        $_ = '&' . $indexcnt . '_' . "$index=$_";
    }

    return $_;
}

sub setup_hypertext {

    # Setup the ending reference for hyptext links

    $_ = $ENV{QUERY_STRING};
    s/^$ZDB//;
    s/[&]*\d+_[^&]+|&lop[^&]+//g;   #  Get rid of search terms and logical operators
    s/&start=\d+/&start=1/;
    s/&records=\d+/&records=25/;
    s/&elemname=\w+/&elemname=B/;
    $ZINFO = $_;

#
#   Index the hypertext with the tag you want linked.
#

    if (defined($HYPERTEXT)) {
        foreach (split("\n", $HYPERTEXT)) {
           ($tag, $match, $index, $cmd) = eval('(' . $_ . ')');
           $HYPERLINK{$tag} = join(' ',$match, $index, $cmd);
        }
    }

    $HYPERTAG = join('|', keys %HYPERLINK);
    $HYPERTAG = 'NOTAGS' unless ($HYPERTAG);

#
#   Now check for any local link that you want.
#

    if ($LOCAL_LOOKUP && $LOCAL_PROFILE) {
        foreach (split("\n", $LOCAL_LOOKUP)) {
           ($field, $subfields, $index, $lop, $cmd) = eval('(' . $_ . ')');
           $LOCALLINK{$field} = join(' ', $subfields, $index, $lop, $cmd);
        }
    }
        
    $LOCALTAG = join('|', keys %LOCALLINK);
    $LOCALTAG = 'NOTAGS' unless ($LOCALTAG);
}

