# -*- perl -*-
# $Id: Configure,v 1.3 2006/05/19 21:32:02 lile Exp $

use Getopt::Long;
use Config;
use strict;

our ($opt_h, $opt_prefix, $opt_sysconfdir, $opt_generic, $opt_rfc2307, $opt_iplanet,
     $opt_shadow, $opt_authen_sasl_cyrus);

$opt_prefix = "/usr/local";
$opt_generic = 1;
$opt_rfc2307 = 1;
$opt_iplanet = 1;
$opt_shadow = 0;
$opt_authen_sasl_cyrus = 1;

#--- Configure ---

if (!GetOptions(
		'h' => \$opt_h,
		"prefix=s" => \$opt_prefix,
		"sysconfdir=s" => \$opt_sysconfdir,
		"generic!" => \$opt_generic,
		"rfc2307!" => \$opt_rfc2307,
		"iPlanet!" => \$opt_iplanet,
		"shadow!"  => \$opt_shadow,
		) || $opt_h) {
	die << "EDQ";
Usage: perl $0 [-h]
	-h		  Display this message
	--prefix	  Install prefix
	--sysconfdir	  Read-only single-machine data [PREFIX/etc]
	--generic	* Include generic ldap maps (rootDSE, aci)
	--rfc2307	* Include rfc2307 maps (passwd, group, ...)
	--shadow	  Include support for shadow passwords
	--iPlanet	* Include iPlanet Directory Server maps (schema, config, ...)

	* Included by default
EDQ
}

#--- Bake out config values

my $prefix = $opt_prefix;
my $bindir = join("/", $prefix, "/bin");
my $sysconfdir = $opt_sysconfdir || join("/", $prefix, "/etc");

$bindir =~ s!/+!/!g;
$sysconfdir =~ s!/+!/!g;

#--- Check for required modules ---

my $missing = 0;
$| = 1;

sub check_module {
  my($module,$version) = @_;
  print substr("$module ............................",0,30);
  my $ok = eval {
    my $file = $module;
    $file =~ s#::#/#g;
    require $file . ".pm";
    $module->VERSION($version) if defined $version;
    1;
  };
  $missing++ unless $ok;
  $@ =~ s/^(Can't locate \S* in \@INC).*/$1/s;
  print $ok ? "ok\n" : "** FAILED **\n$@\n";
  $ok;
}

print "\nChecking for REQUIRED modules\n\n";

check_module('POSIX') or print <<"EDQ", "\n";
The POSIX module is required for led
EDQ

check_module('Net::LDAP') or print <<"EDQ", "\n";
The Net::LDAP module is required for led
EDQ

check_module('Digest::MD5') or print <<"EDQ", "\n";
The Digest::MD5 module is required for led
EDQ

check_module('URI',1.08) or print <<"EDQ", "\n";
The URI module is required for led
EDQ

check_module('URI::ldap',1.10) or print <<"EDQ", "\n";
The URI::ldap module is required for led
EDQ

die "\n",<<"EDQ", "\n" if $missing;
****************************************************************************
You are missing some modules that are required for led.  Read the above
messages and download any required modules from http://www.perl.com/CPAN
****************************************************************************
EDQ

print "\nChecking for OPTIONAL modules\n\n";
$missing = 0;

check_module('Authen::SASL') or print <<"EDQ", "\n";
The Authen::SASL module is missing, you will be unable to use SASL
EDQ

(!$missing and ($opt_authen_sasl_cyrus = (check_module('Authen::SASL::Cyrus') ? 1 : 0 )))
    or print <<"EDQ", "\n";
The Authen::SASL::Cyrus module is missing, you will be unable to use GSSAPI
EDQ

warn "\n",<<"EDQ", "\n" if $missing;
****************************************************************************
You are missing some optional modules for led.  Read the above
messages and download any required modules from http://www.perl.com/CPAN
****************************************************************************
EDQ

print"\n";
print "Writing Makefile\n";
open(MAKEFILE, ">Makefile") or die "Unable to write Makefile";

print MAKEFILE <<"EDQ", "\n";

INSTALL = /usr/bin/install -C
RM = /bin/rm
PERL = $Config{installbin}/perl
PREFIX = $prefix
BINDIR = $bindir
SYSCONFDIR = $sysconfdir
GENERIC = $opt_generic
RFC2307 = $opt_rfc2307
IPLANET = $opt_iplanet
SHADOW = $opt_shadow
CYRUS = $opt_authen_sasl_cyrus

EDQ

print MAKEFILE <<'EDQ', "\n";
all: build

build: led.pl

led.pl: led Makefile
	${PERL} -p -e 's#^\#!/usr/bin/perl#\#!${PERL}#' led > led.pl
	${PERL} -p -e 's!/etc/led.ldif!${SYSCONFDIR}/led.ldif!' -i led.pl
	${PERL} -p -e 's/load_generic_maps\((.*)?\);/load_generic_maps(${GENERIC});/' -i led.pl
	${PERL} -p -e 's/load_rfc2307_maps\((.*)?\);/load_rfc2307_maps(${RFC2307}, ${SHADOW});/' -i led.pl
	${PERL} -p -e 's/load_iPlanet_maps\((.*)?\);/load_iPlanet_maps(${IPLANET});/' -i led.pl
	${PERL} -p -e 's/Authen_SASL_Cyrus\s*=\s*.*?;/Authen_SASL_Cyrus = ${CYRUS};/' -i led.pl
	chmod 755 led.pl

install:
	${INSTALL} led.pl ${BINDIR}/led

clean:
	${RM} -f led.pl

distclean: clean
	${RM} -f Makefile
EDQ

close MAKEFILE or warn "Error writing to Makefile";

print "\n";
