#!/usr/bin/perl --
# Copyright 1998,1999 Ivan E. Moore II
# See /usr/doc/horde/copyright.
$|=1;

### If horde.lib exists ###
### Change permissions so that we can create a new horde.lib file ###

if (! (-s "/usr/share/horde/lib/horde.lib")) {
  system("chmod -R 644 /usr/share/horde/lib/*.lib >> /dev/null 2>&1");
}

if (-s "/usr/sbin/apache-ssl") {
  $srm='/etc/apache-ssl/srm.conf';
  $http='/etc/apache-ssl/httpd.conf';
} else {
  $srm='/etc/apache/srm.conf';
  $http='/etc/apache/httpd.conf';
}

sub loadconf ($$;$)
{
    $parameter = 'DocumentRoot';
    $default = $_[1];

    $file = (defined ($_[2]) ? $_[2] : "$http");
    $in = `egrep "^[:space:]*$parameter " $file 2> /dev/null| head -1`;
    $in =~ s/\s*$parameter //;
    chomp ($in);
    return $in if ($in ne "");
    return $default;
}

sub load_conf ($$;$)
{
    my $parameter = $_[0];
    my $default = $_[1];

    my $file = (defined ($_[2]) ? $_[2] : "$srm");
    my $in = `egrep "^[:space:]*$parameter " $file 2> /dev/null| head -1`;
    $in =~ s/\s*$parameter //;
    chomp ($in);
    return $in if ($in ne "");
    return $default;
}
$install_dir = loadconf ("DocumentRoot", $tmproot, $srm);

$buildopts ='/etc/horde/build.opts';

sub load_config_files ()
{
    $main::srmconf = `cat $srm`;
    $main::dirindex = load_conf ("DirectoryIndex",$tmpdirindex, $srm);
    $main::buildconf = (-f $buildopts ? `cat $buildopts`: '');

}

### build.opts configuration ###

$write_srmconf = 0;
$restart_apache = 0;
$write_buildopts = 0;

if (-s "/usr/bin/psql") {
                $database='postgres';
    } else {
      if (-s "/usr/bin/mysql") {
                 $database='mysql';
      } else {
                 $database='dummy';
      }
    }

sub add_features ()
{
  $dirindexnew='index.html index.php3 index.htm default.html index.cgi';
  print "\n\nNow verifying both Apache and PHP3 Configurations\n";
  if (! ($srmconf =~ m/.*DirectoryIndex\s+$dirindexnew/m))
  {
    $srmconf =~ s/^\s*DirectoryIndex.*/DirectoryIndex $dirindexnew/m;
    print "DirectoryIndex now $dirindexnew.\n";
    $write_srmconf++;
  }
  if (! ($database =~ dummy)) {
    if (! ($buildconf =~ m/.*database=*/m))
    {
        print "Setting $database as your database option in build.opts\n";
        $buildconf =~ s/$/\ndatabase=$database\n/s;
        $write_buildconf++;
    }
  }
    if (! ($srmconf =~ m/.*Alias\s+\/horde\/\s+\/usr\/share\/horde\//m))
    {
        print "Adding alias /horde/ -> /usr/share/horde/ to srm.conf.\n";
        $srmconf =~ s/$/\n# Added for HORDE\n/s;
        $srmconf =~ s/$/Alias \/horde\/ \/usr\/share\/horde\/\n/s;
    if (! ($srmconf =~ m/.*Alias\s+\/horde\s+\/usr\/share\/horde\//m))
    {
        $srmconf =~ s/$/Alias \/horde \/usr\/share\/horde\/\n/s;
    }
        $write_srmconf++;
    }

}

if (-s "/etc/horde/config") {

  print "\nLooks like a previous install, not going to touch your Apache\n
or PHP stuff so if you don't have something set right, don't blame me!\n";
} else {
  load_config_files ();
  add_features ();
  system ("echo 'Base Install\n' > /etc/horde/config");
}

if ($write_buildconf)
{
    system ("savelog -c 100 $buildopts >> /dev/null 2>&1")
        && die ("couldn't rotate $buildopts\n");
    open (CONF, ">$buildopts") || die ("couldn't open $buildopts\n");
    print CONF $buildconf;
    close (CONF);
}

if ($write_srmconf)
{
    system ("savelog -c 100 $srm >> /dev/null 2>&1")
        && die ("couldn't rotate $srm");
    open (CONF, ">$srm") || die ("couldn't open $srm");
    print CONF $srmconf;
    close (CONF);
    $restart_apache++;
}



### Build horde.lib ###

print "Building HORDE Libraries for your specific installation
";

 system("cd /usr/share/horde/lib/src && ./build.pl >> /dev/null 2>&1");

### Change file permissions for security ###

 system("chown -R www-data.root /usr/share/horde >> /dev/null 2>&1");
 system("chmod -R 555 /usr/share/horde/lib/*.lib >> /dev/null 2>&1");
 system("chmod 000 /usr/share/horde/setup.php3 >> /dev/null 2>&1");
 system("chmod 555 /etc/horde/*.php3 >> /dev/null 2>&1");
 system("chmod 555 /etc/horde/*.txt >> /dev/null 2>&1");

### Done ###
