####################################################
# Edit the preferences file
####################################################
sub PREFS {
    my($subaction) = shift || "edit";
    my($section) = $in{'section'} || "COMMANDS";
    my($line, $arg, $val, %prefs, @args, $s, @data);
    my($output) = "";
    my($result) = "";
    local(*F);

# Read Preferences file

    open(F, "<$Preferencefile") || &Error("Unable to open settings file: $Preferencefile");
    @data = <F>;
    close(F);

# Make updates and save preferences

    if ($subaction =~ /save/i) {
      open(F, ">$Preferencefile") || &Error("Unable to update settings file: $Preferencefile");
      local $inauto = 1;
      foreach $line (@data) {
        chomp $line;
        $inauto = 0 if $line =~ /# -- END AUTO PREFERENCES/;
        if ($inauto && ($line =~ /^\$/)) {
          ($arg, $val) = $line =~ /^.([^ =]+)\s*=\s*(.+)\s*;$/;
          $val = (defined $in{$arg})?$in{$arg}:eval($val);
          $val =~ s/\@/\\\@/g;
          print F "\$".$arg." = \"".$val."\";\n";
        } else {
          print F $line,"\n";
        }
      }
      close(F);

      if (open(F, "<$Configdir/autorecheck.pid")) {
        local $autopid = <F>;
        chomp $autopid;
        close(F);
        if ($autopid) {
          if ($Autointerval > 0) {
            kill 'HUP', $autopid;
          } else {
            kill 'TERM', $autopid;
          }
        }
      }

      $output = "<p><strong>Settings have been updated.</strong></p>";
      @data = ();   # Reread file
      open(F, "<$Preferencefile") || &Error("Unable to open settings file: $Preferencefile");
      @data = <F>;
      close(F);
      #eval(@data);  # Update values
    }

# Normal Entry

    $result = &Header($Myname,"","").$output;

    my $instr = "";
    my $ss = 0;

    foreach (@data) {
      chomp;
      last if $_ =~ /# -- END AUTO PREFERENCES/;
      next if (!$_ or ($_ =~ /^1/) or ($_ =~ /^# -/));
      if( $_ =~ /^#\s*SECTION/ ) {
        ($s = $_) =~ s/^#\s*SECTION\s*(.*)$/$1/;
        $ss = 0;
        next;
      }
      if ($_ =~ /^#/) {
        ($line = $_) =~ s/^#//;
        $instr .= $line . "\n";
      } else {
        ($arg, $val) = $_ =~ /^.([^ =]+)\s*=\s*(.+)$/;
        if ($instr) {
          $ss++;
          $prefs{$s}{$ss}{'instr'} = $instr;
          $instr = "";
        }
        $prefs{$s}{$ss}{'args'} .= $arg.":".eval($val)."\n";
      }
    }

    my $sections = "";
    foreach $s (sort keys %prefs) {
      $src = ($s eq $section) ? "item_on.gif" : "item_off.gif";
      $sections .= "<a href=\"".$nPULSEurl."?a=pref&s=edit&section=".$s."\">".
                   "<img src=\"/images/".$src."\" border=0 width=24 height=16>".
                   $s."</a><br>";
    }

    my $out = "<table>\n<tr>\n<td colspan=5><p style=\"".$Fsn."\">".$section."</p></td>\n</tr>\n";
      
    foreach $ss (sort {$a <=> $b} keys %{$prefs{$section}}) {
      @args = split(/\n/, $prefs{$section}{$ss}{'args'});
      ($arg, $val) = split(/:/,shift(@args));
      $out .= "<tr valign=\"top\"><td rowspan=\"".($#args+2)."\">&nbsp;</td>".
              "<td colspan=2 rowspan=\"".($#args+2)."\"><span style=\"".$Fss."\">".
              $prefs{$section}{$ss}{'instr'}."</span></td>".
              "<td style=\"".$Fxs.";text-align:right\" align=\"right\"><span style=\"".$Fxs.
              ";text-align:right;color:#bdbdbd\">".$arg.":</span></td>\n".
              "<td><input style=\"".$Fxs."\" type=text name=\"".$arg."\" size=25 value=\"".$val."\">".
              "</td>\n</tr>\n";
      foreach (@args) {
        ($arg, $val) = split /:/;
        $out .= "<tr valign=\"top\">\n".
          "<td style=\"".$Fxs.";text-align:right\" align=\"right\"><span style=\"".$Fxs.
          ";text-align:right;color:#bdbdbd\">".$arg.":</span></td>\n".
          "<td><input style=\"".$Fxs."\" type=text name=\"".$arg."\" size=25 value=\"".$val."\">".
          "</td>\n</tr>\n";
      }
      $out .= "<tr><td colspan=5><hr noshade></td></tr>\n";
    }

    $out .= "</table>\n";

     $result .= <<EOS;
<p>You can change the following values to alter how nPULSE operates.</p>
<form method=post action="$nPULSEurl"><input type=hidden name=a value="pref">
 <input type=hidden name=s value="save">
 <table border=3>
  <tr valign="top">
   <td><div style="$Fsn">SETTINGS</div><div class="$Fss">$sections</div></td>
   <td>$out<br>
    <input type=submit name=submit style="$Fss" value="Save">
   </td>
  </tr>
 </table>
</form>
EOS
    $result .= &Footer();
    return $result;
}

1;
