#!/perl/bin/perl

# search.pl
#
# First version. Searches through the guest book database for
# the last name entered on the command line.

# Get header files.

    require "c:/Program Files/Sambar/cgi-bin/GuestBook.pm"
        || die "Can't open guest book header file: $!\n";

# Put the command-line argument in a local variable.

    $search = $ARGV[0];

# Make a flag to signify that a match has been found.

    $Found = 0;

# Open the guest book file; die if it's not available.

    open (GUEST_BOOK, $GuestBookPath)
        || die "Can't open guest book file: $!\n";

# Read each record, unpack it, then check the last name field
# for a match with the command-line argument.

    while (read (GUEST_BOOK, $buffer, $GuestEntrySize))
        {

    # Use unpack to load the record into an array of fields based
    # on the same template we used with pack to format them for
    # the file.
 
        @InfoArray = unpack ($GuestEntryStruct, $buffer);

    # Strip NULLs out of the data.

        for ($n = 0; $n < ($NumElements - 1); $n++)
            {
            $InfoArray[$n] =~ s/\0//g;
            }
 
    # Check the last-name field for a match; display the entire
    # entry if it matches, or get another if not.

        if ($InfoArray[$LastName] eq $search)
            {
            print "First name: $InfoArray[$FirstName]\n";
            print "Last name: $InfoArray[$LastName]\n";
            print "City: $InfoArray[$City]\n";
            print "State: $InfoArray[$State]\n";
            print "Country: $InfoArray[$Country]\n";
            print "E-mail address: $InfoArray[$EMail]\n";
            print "Comments: $InfoArray[$Comments]\n";
            print "Visited on ";

    # Convert the time in the same fashion we did in guestbook.pl.

            ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
                = localtime ($InfoArray[$NumEntryTime]);
            print "$WeekDay[$wday], $Month[$mon] $mday, ", $year + 1900;
            print " at $hour:";
            if ($min < 10)
                {
                print "0";
                }
            print "$min:";
            if ($sec < 10)
                {
                print "0";
                }
            print "$sec\n";

    # Set the $Found flag.

            $Found = 1;
            }            # End if ($InfoArray...)
        }                # End while (read...)

# If the $Found flag is still 0, put up a message.

    if (!$Found)
        {
        print "Sorry. No matches found for $search.\n";
        }

#                    End search.pl
