#
#
# Build a 'Search'  data base and keep the user informed.
#

#
# Make sure they specified a directory at all.
#
$_hosts = $_sara_search;
$go_search = 0;
$go_cnt = 0;
$form_data = "";

if ($_hosts eq "") {
	print CLIENT <<EOF;
<HTML>
<HEAD>
<TITLE>Error - Missing input </TITLE>
<LINK REV="made" HREF="mailto:sara\@arc.com">
</HEAD>
<BODY>
<H1><IMG SRC=$HTML_ROOT/images/sara.gif WIDTH="150"> Error - Missing input </H1>
<hr>
No selection criterion was specified.
</BODY>
</HTML>
EOF
	die "\n";
}

#
# Create the SEARCH database
#
$sara_data = SEARCH;
&init_sara_data();
#
# Read the data base, after resetting the in-core data structures.
#
print CLIENT <<EOF;
<HTML>
<HEAD>
<TITLE>SARA Search Selection</TITLE>
<LINK REV="made" HREF="mailto:sara\@arc.com">
</HEAD>
<BODY>
<H1><IMG SRC=$HTML_ROOT/images/sara.gif> SARA Search Selection</H1>
<FORM METHOD=POST ACTION="sara_search_build.pl">
<UL><UL><UL>
EOF
$_hosts =~ s/\+/ /g;
for (@_results) {
        $_directory = "$_";
        $directory  = "XxX";
        $directory .= "$_directory";
        if (-d "results/$_directory" ) {
         if ("$results/$directory" ne "SEARCH") {
          if ($$directory eq "on") {
            if ( $mode eq "host" ) {
              open (DB,"results/$_directory/all-hosts");
              $checked = "";
            } else {
              open (DB,"results/$_directory/facts");
              $checked = "checked";
            }
            while (<DB>) {
              if (/$_hosts/) {
                @_hostname=split(/\|/,$_);
                $go_search =1;
                $go_cnt= $go_cnt + 1;
		$namebox="A$go_cnt";
		$$namebox="";
                $form_data .= "<INPUT TYPE=\"checkbox\" NAME=\"$namebox\" VALUE=\"$_hostname[i]\|$directory\" $checked>";
                $form_data.= "<strong>$go_cnt: $_hostname[i] ($_directory)</strong><BR>";
             }
            }
         }
        }
}
}
if ($go_search) {
print CLIENT <<EOF;
$form_data
<INPUT TYPE="submit" VALUE=" Generate Search Database">
<P>
<I>Once started, Do Not Interrupt this process!</I>
</UL>
</FORM>
Select the hosts (database) that will be included in the SEARCH database.<BR>
Be careful!  Do not select a host more than once.  Unpredictable results <BR>
may occur.<BR>
</UL></UL></UL>
<hr> <a href=$HTML_SERVER> Back to the SARA start page </a>
| <a href=$HTML_SERVER/search/sara_search_form.pl>Go back to Search page</a>
</BODY>
</HTML>
EOF
} else {
  print CLIENT <<EOF;
  </FORM>
   <B>No records found!</B><BR>
<hr> <a href=$HTML_SERVER> Back to the SARA start page </a>
| <a href=$HTML_SERVER/search/sara_search_form.pl>Go back to Search page</a>
</BODY>
</HTML>
EOF
}
