#!/usr/bin/perl

use strict;
use vars qw(%data %conf $cfgfile $root $relroot $sec $ldap);
my $inp;

# Parse some %ENV-values.
($relroot) = $ENV{'SCRIPT_NAME'} =~ /^(.*)\/.+/;
$root = $ENV{'DOCUMENT_ROOT'} . $relroot;

# Global variables:
%data = (title      => "Welcome to LDAP DNS Admin",
	 secversion => '???',
	 error	    => [],
	 notify     => []);
$cfgfile = "$root/etc/admin.conf";
push @INC, "$root/include/";

# Include necessary files.
require "basic.pl.inc";
require "pagefunc.pl.inc";

# If there is input or arguments, parse and modify %data
$data{'domain'} = "N/A";
$data{'section'} = 'home';
%data = proc_input($inp, %data) if ($inp = <STDIN>);
%data = proc_input($ENV{'QUERY_STRING'}, %data) if ($ENV{'QUERY_STRING'});

# Include code for current section.
%conf = get_cfg(%conf);
require "sec_$data{section}.pl.inc"
  if -r "$root/include/sec_$data{section}.pl.inc";

# Generate the page!
print "Content-Type: text/html\n\n";
pagestart(%data);

# Print error message if $data{error} is set.
if (($conf{'showerr'} =~ /yes|y|1/i || ! -r $cfgfile) && @{$data{'error'}}) {
  my $error;
  
  foreach $error (@{$data{'error'}}) {
    $data{'errorstr'} .= 
      "  <font color=\"red\"><b>* An error occurred: $error</b></font><br />\n"
  }
  $data{'errorstr'} .= "  <hr />\n";
}

# Print notification message if $data{notify} is set.
if (($conf{'shownotify'} =~ /yes|y|1/i || ! -r $cfgfile) && @{$data{'notify'}} > 0) {
  my $notify;
  
  foreach $notify (@{$data{'notify'}}) {
    $data{'notifystr'} .= 
      "  <font color=\"green\"><b>* $notify</b></font><br />\n";
  }
  $data{'notifystr'} .= "  <hr />\n";
}

pagebody(%data);
print "  <hr />\n";

# Debug output
if ($conf{'showdebug'} =~ /yes|y|1/i) {
  print "  <b>Debug output:</b> <br />\n";
  print "  \$inp = $inp<br>\n" if $inp;
  foreach (sort keys %data) {
    print "  \%data\% $_ => $data{$_}<br />\n";
  }
  foreach (sort keys %conf) {
    print "  \%conf\% $_ => $conf{$_}<br />\n";
  }
  print "  <hr />\n"
}

pageend(%data);
