####################################################
# Edit the setup file
####################################################

sub EDITOR {
    my $out = shift;
    my $subaction = lc($in{'s'});

    &DEBUG("Starting; subaction = $subaction");
    &HeaderEDITOR();

# Save entry

    if ($subaction eq "save") {
      if ($DEMO) {
        $out .= "<hr>Settings cannot be saved in DEMO mode.<hr>\n";
      } else {
  	    open(F, ">$Configfile") || &Error("Unable to save $Configfile");
	    $in{'text'} =~ s/\r//g;
	    print F $in{'text'};
	    close(F);
        $out .= "<hr>Settings Saved.<hr>\n";
      }
    }

# Normal entry

	$out .= "<form method=get action=\"$nPulseurl\">\n".
        "<input type=hidden name=a value=\"setup\">\n".
        "<input type=hidden name=s value=\"save\">\n".
        "<textarea style=\"$Fxs;width:100%\" rows=10 cols=80 name=text>";
    $out .= &ReadConfigfile() if (-f $Configfile);
    print $out."</textarea>\n" .
		"<br>\n<input type=submit name=submit value=\"Save\">\n</form>".
        '<hr><span style="'.$Fss.'">Note: nPulse runs **much** faster if '.
        'you use ranges of ip address rather than a single entry for each address. '.
        'For example 192.168.1.10-50 21,23,80 is about 20 times faster than single '.
        'entries for each ip address.</span>'.
        &Footer();
	&DEBUG("Exiting");
    exit;
}

sub DISCOVER { 
    my $ipmask = "";
    &DEBUG("Starting");
    &Error("Discover mode not available in DEMO mode") if $DEMO;
    ($ipmask = $in{'ipmask'}) =~ s/[^\d,\*\.-]//g;
    &Error("DISCOVER: Invalid IP mask: $ipmask") unless $ipmask =~ /^[\d,\*\.-]+$/;

    if ($in{'savecurrent'}) {
      &DEBUG("savecurrent checked");
      if (open(C, "<$Configfile")) {
        my @cdata = <C>;
        close(C);
        chomp @cdata;
        open(C, ">$Configfile.bak") or &Error("Unable to copy $Configfile");
        print C join("\n",@cdata),"\n";
        close(C);
        &DEBUG("Backup of configfile contains ".($#cdata+1)." entries.");
      }
    }
    
    my $portmask = $in{'allports'}?"1-65535":"0";
    my $cmd = $Autorecheck." auto".$$.".pid -1 '".$ipmask."' ".$portmask." 1";
    &DEBUG("Issuing cmd: $cmd");
    system($cmd);
    &MergeConfigFile() if ($in{'savecurrent'} and (-f "$Configfile.bak"));
    $in{'s'} = '';
    &EDITOR("<hr>Discovery complete - Next, edit the information below or return to the home page.<hr>\n");
    &DEBUG("Exiting");
    exit;
}

sub LOOKUP {
    &DEBUG("Starting");
    use Socket;
    my($name, $addr);
    ($name = $in{'site'}) =~ s/\s//g;
    if ($name =~ /^(?:\d{1,3}\.){3}\d{1,3}$/) {
      $addr = gethostbyaddr(inet_aton($name),AF_INET);
      $name .= " is " . ($addr ? $addr : "???");
    } else {
      $name .= " is " . (($addr = gethostbyname($name)) ? inet_ntoa($addr) : "???");
    }
    $in{'s'} = '';
    &EDITOR("<hr><p style=\"background-color:#999999\">Address: ".$name."</p><hr>\n");
    &DEBUG("Exiting");
    exit;
}

sub MergeConfigFile {
    local(*C);
    &DEBUG("Starting");
    my $back = $Configfile.'.bak';
    open(C, "<$back") || return;
    my @cdata = <C>;
    close(C);
    chomp @cdata;
    unlink $back;

    &DEBUG("Backup file has ".($#cdata+1)." lines");
    my($host,$line,@olddata);
    foreach $line (@cdata) {
      ($host = $line) =~ s/^([\d\.]+) .*$/$1/;
      push(@olddata,&GenerateSiteList($host)) if $host =~ /^[\d\.]+$/;
    }
    &DEBUG("Old hosts:".join("\n",@olddata));
    
    open(C, "<$Configfile");
    my @newdata = <C>;
    close(C);
    chomp @newdata;

    foreach $line (@newdata) {
      ($host = $line) =~ s/^([\d\.]+) .*$/$1/;
      next unless $host =~ /^[\d\.]+$/;
      &DEBUG("Checking host: $host");
      push(@cdata,$line) unless grep(/^\s*$host\s*$/,@olddata);
    }

    open(C, ">$Configfile");
    print C join("\n",@cdata),"\n";
    close(C);
    &DEBUG("Configfile has ".($#cdata+1)." lines");
}

sub HeaderEDITOR {
    print &Header($Myname,"Setup",0);
    print <<EOHE2;
 <table><tr valign="top"><td style="$Fss;border-width:0 1 0 0;border-style:outset"><p style="$Fss">
 From this screen you can either enter or update the information in the large 
 textbox below or ask nPulse to discover the information for you based on the current 
 state of your network. To use the discover mode, enter a range of IP numbers into the 'DISCOVER' 
 box and press &lt;enter&gt;. This may take some time, and your web page may time out. 
 If so, don't worry. The process should still run to completion in the background. Also, the default
 discovery process only discovers 'common' ports/services. To discover all ports check the 'All Ports'
 checkbox (this is <b>much slower</b> and may timeout [see Settings/NMAP/nmap_extra_options]).</p>
 <p style="$Fss">The IP range takes the following form: ip1.ip2.ip3.ip4  where ip<em>x</em> 
 is a single number or a range of numbers of the form ###-### or ###,### or any combination. 
 For example: 192.168.0.1-192 or 192.168.10,11,12.1-254
 </td><td>
 <table><tr><td style="$Fss"><form method=post action=$nPulseurl>
 <input type=hidden name=a value="setup"><input type=hidden name=s value="discover">
 Discover:<input type=text name=ipmask size=18 style="$Fss"><br>
 <input type=checkbox name=savecurrent checked>Add New Hosts Only<br>
 <input type=checkbox name=allports>All Ports
 <input type=submit value="Go" style="$Fxs">
 </form><hr></td></tr>
 <tr><td style="$Fss"><form method=post action="$nPulseurl">
 <input type=hidden name=a value="setup"><input type=hidden name=s value="lookup">
 Lookup&nbsp;Address:<input name=site type=text size=12 style="$Fss" onChange="submit()">
 </form></td></tr></table>
 </td></tr></table>
EOHE2
}

1;