#!/usr/local/bin/perl
# Written by Adam P. Jenkins <apj@twain.oit.umass.edu>
# Finished June 15, 1995

#set this to the name and path of LSM file.  
$LSMFILE = "/usr/local/lib/LSM.gz";
#set this to name of program to unzip LSMFILE
$UNZIPPER = "gunzip -c";

require 5.000;

require "dialog.pl";
use Getopt::Std;
use File::Basename;

$/ = "\nEnd";

sub printhelp {
my $prog = basename($0);
    print <<EOT
$prog does a case insensitive search of the Linux Software Map.
The options are:
	-d: Search only the Description field.
	-t: Search only the Title field.
	-k: Search only the Keywords field.
        -h: Get this help screen.
	regexpression: A regular expression to search for.
Default is to search every line.
EOT
}

sub mainmenu {
    return &rhs_menu("LSMSEARCH Main Menu", # Title
	      "Choose Options to limit the search, or Search to search for
a pattern",			# Message
	      60,		# width
	      3,		# Number of menu items
	      "Options", "Set search options.",
	      "Search", "Search for a pattern.",
	      "Quit", "Quit this program.");
}

sub setoptions {
    my $res = &rhs_checklist("LSMSEARCH Options", # Title
			     "Choose options to delimit the search.
If no options are checked, all fields will be searched.
Use SPACE to toggle an option on/off.",
			     60,		# width
			     3,		# number of items
			     "Description", "Search the description field.",
			     $options{'description'},
			     "Keywords", "Search the keywords field.",
			     $options{'keywords'},
			     "Title", "Search the title field.",
			     $options{'title'});

    $options{'description'} = grep(/Description/, @dialog_result) ? 1 : 0;
    $options{'keywords'} = grep(/Keywords/, @dialog_result) ? 1 : 0;
    $options{'title'} = grep(/Title/, @dialog_result) ? 1 : 0;
    return $res;
} 
 
sub getpattern {
    my $res = &rhs_inputbox("LSMSEARCH",	# Title
			    "Type in a regular expression to search for.",
			    60,	# width
			    $Pattern); # default pattern
    $Pattern = $dialog_result;
    return $res;
}

sub dosearch {
    my $BASENAME = basename($0);
    my $TMPFILE = "/tmp/$$.$BASENAME.tmp";
    open(TMPFILE, ">$TMPFILE") or die "Can't open temp file.\n";
    open(LSMFILE, "$UNZIPPER $LSMFILE |") or die "Can't open $LSMFILE\n";
    select(TMPFILE);

# if no options were given, do it the fastest way
    if (!$options{'title'} && !$options{'description'}
	&& !$options{'keywords'})
	{
	    # This actually reads in a whole record at a time, up to a line
	    # beginning with /^End/.  Because I set $/ to "\nEnd".
	    while (<LSMFILE>) {
		if (/$Pattern/i) {
		    print;	# prints record to the TMPFILE 
		} 
	    }
	} else {		# else process options.
	    while (<LSMFILE>) {
		$found = 0;
		if ($options{'title'}) {
		    if (/\nTitle\:.*?\n(?=[\w-]+\:)/s) {
			$tmp = $&;
			if ($tmp =~ /$Pattern/i) {
			    $found = 1;
			}
		    }
		}
		if ($options{'description'}) {
		    if (/\nDescription\:.*?\n(?=[\w-]+\:)/s) {
			$tmp = $&;
			if ($tmp =~ /$Pattern/i) {
			    $found = 1;
			}
		    }
		}
		if ($options{'keywords'}) {
		    if (/\nKeywords\:.*?\n(?=[\w-]+\:)/s) {
			$tmp = $&;
			if ($tmp =~ /$Pattern/i) {
			    $found = 1;
			}
		    }
		}
		if ($found) {
		    print;
		}
	    }			# while
	}			# else
    
    close LSMFILE;	      
    close TMPFILE;

    &rhs_textbox("Records containing $Pattern", $TMPFILE, 80, 24);
    unlink $TMPFILE;
}

sub mainloop {
    &mainmenu;
    while ($dialog_result !~ /^Quit/) {
	if ($dialog_result =~ /^Options/)
	    { &setoptions; }
	elsif ($dialog_result =~ /^Search/) {
	    if (&getpattern) {
		if ($Pattern) { &dosearch; }
	    }
	}		      
	&mainmenu;
    }

    &rhs_clear;
}
	

# Main program
getopts('tdkh') or $opt_h = 1;

if ($opt_h) {			# help option
    &printhelp;
    exit 1;
}

$options{'title'}       = $opt_t ? $opt_t : 0;
$options{'description'} = $opt_d ? $opt_d : 0;
$options{'keywords'}    = $opt_k ? $opt_k : 0;

$Pattern = '';
if ($#ARGV >= 0) {		# just do it once 
    $Pattern = shift;
    &dosearch;
} else {			# run program interactively
    &mainloop;
}
