#!/usr/bin/perl -w

use warnings;
use Getopt::Long;
use Cwd 'abs_path';
use File::Basename;

print "\nConfiguring Makefiles for your system...\n";

# Set a bunch of variables

my @invoke = @ARGV;

my $ret = 0;

my %options = (
	help => \$help,
	cc => \$cc,
	debug => \$debug,
	cflags => \$cflags,
	extracflags => \$extracflags,
	ldflags => \$ldflags,
	extraldflags => \$extraldflags,
	objdir => \$objdir,
        incdir => \$incdir,
	libdir => \$libdir,
        ccsincdir => \$ccsincdir,
	ccslibdir => \$ccslibdir,
	cmanincdir => \$cmanincdir,
	cmanlibdir => \$cmanlibdir,
	logtincdir => \$logtincdir,
	logtlibdir => \$logtlibdir,
	openaisincdir => \$openaisincdir,
	openaislibdir => \$openaislibdir,
	virtincdir => \$virtincdir,
	virtlibdir => \$virtlibdir,
	nssincdir => \$nssincdir,
	nsslibdir => \$nsslibdir,
	nsprincdir => \$nsprincdir,
	nsprlibdir => \$nsprlibdir,
	mandir  => \$mandir,
	prefix => \$prefix,
	sbindir => \$sbindir,
	initddir => \$initddir,
	docdir => \$docdir,
	logdir => \$logdir,
	syslogfacility => \$syslogfacility,
	sysloglevel => \$sysloglevel,
	snmpbin => \$snmpbin,
	confdir => \$confdir,
	fence_agents => \$fence_agents,
	fenceagentslibdir => \$fenceagentslibdir,
	enable_crack_of_the_day => \$enable_crack_of_the_day,
	enable_legacy_code => \$enable_legacy_code,
	enable_virt => \$enable_virt,
	release_version => \$release_version,
);

my $err = &GetOptions (\%options,
		    'help',
		    'cc=s',
		    'debug',
		    'cflags=s',
		    'extracflags=s',
		    'ldflags=s',
		    'extraldflags=s',
		    'objdir=s',
		    'incdir=s',
		    'libdir=s',
		    'ccsincdir=s',
		    'ccslibdir=s',
		    'cmanincdir=s',
		    'cmanlibdir=s',
		    'logtincdir=s',
		    'logtlibdir=s',
		    'openaisincdir=s',
		    'openaislibdir=s',
		    'virtincdir=s',
		    'virtlibdir=s',
		    'nssincdir=s',
		    'nsslibdir=s',
		    'nsprincdir=s',
		    'nsprlibdir=s',
		    'mandir=s',
		    'prefix=s',
		    'sbindir=s',
		    'initddir=s',
		    'docdir=s',
		    'logdir=s',
		    'syslogfacility=s',
		    'sysloglevel=s',
		    'snmpbin=s',
		    'confdir=s',
		    'release_version=s',
		    'fence_agents=s',
		    'fenceagentslibdir=s',
		    'enable_crack_of_the_day',
		    'enable_legacy_code',
		    'enable_virt');

if(!$err) {
  $ret = 1;
  print "*** ERROR: Invalid option detected ***\n";
}

# Check for the --help flag
if ($help || !$err) {
  $_ = $0;
  s/.*\.\/(.*)/$1/;
  print "Usage: $_ [flags]\n";
  print "--help\t\tPrints this usage information\n\n";
  print "install flags:\n";
  print "--prefix=\tthe base directory to install into.  (Default: /usr)\n";
  print "--sbindir=\tthe base directory for system binaries.  (Default: {prefix}/sbin)\n";
  print "--initddir=\tthe base directory for init.d scripts.  (Default: /etc/init.d)\n";
  print "--libdir=\tthe base directory for libraries.  (Default: {prefix}/lib)\n";
  print "--docdir=\tthe base directory for misc cluster documentation files.  (Default: {prefix}/share/doc/cluster)\n";
  print "--logdir=\tthe base directory for cluster logging files.  (Default: /var/log/cluster/)\n";
  print "--syslogfacility=\tset the default syslog facility.  (Default: LOG_LOCAL4)\n";
  print "--sysloglevel=\tset the default syslog level.  (Default: LOG_INFO)\n";
  print "--snmpbin=\tthe base directory for snmp binaries (Ex: /usr/bin/snmpwalk).  (Default: {prefix}/bin)\n";
  print "--confdir=\tthe cluster config directory.  (Default: /etc/cluster)\n";
  print "--mandir=\tthe base directory for man pages.  (Default: {prefix}/share/man)\n";
  print "\nbuild flags:\n";
  print "--cc=\t\tcompiler to use.  (Default: gcc)\n";
  print "--debug\t\tEnable debugging build. Changes default CFLAGS to -Wall -O0 -DDEBUG -g.\n";
  print "--cflags=\toverride default CFLAGS settings.  (Default: -Wall -O2 -g)\n";
  print "--extracflags=\tadd extra compiler options to default CFLAGS setting.  (Default: none)\n";
  print "--ldflags=\toverride default LDFLAGS settings.  (Default: none)\n";
  print "--extraldflags=\tadd extra linking options to default LDFLAGS settings.  (Default: none)\n";
  print "--objdir=\tspecify directory where to store object files.  (Defaults: current build dir)\n";
  print "--incdir=\tthe base directory for include files.  (Default: {prefix}/include)\n";
  print "--ccsincdir=\tthe base directory for ccs include files.  (Default: ./config/libs/libccsconfdb)\n";
  print "--ccslibdir=\tthe base directory for ccs libraries.  (Default: ./config/libs/libccsconfdb)\n";
  print "--cmanincdir=\tthe base directory for cman include files.  (Default: ./cman/lib)\n";
  print "--cmanlibdir=\tthe base directory for cman libraries.  (Default: ./cman/lib)\n";
  print "--logtincdir=\tthe base directory for logthread include files.  (Default: ./common/liblogthread)\n";
  print "--logtlibdir=\tthe base directory for logthread libraries.  (Default: ./common/liblogthread)\n";
  print "--openaisincdir=\tthe base directory for openais include files.  (Default: {incdir})\n";
  print "--openaislibdir=\tthe base directory for openais libraries.  (Default: {libdir}/openais)\n";
  print "--nssincdir=\tthe base directory for libnss include files.  (Default: {incdir}/nss3)\n";
  print "--nsslibdir=\tthe base directory for libnss libraries.  (Default: {libdir})\n";
  print "--nsprincdir=\tthe base directory for libnspr include files.  (Default: {incdir}/nspr4)\n";
  print "--nsprlibdir=\tthe base directory for libnspr libraries.  (Default: {libdir})\n";
  print "--virtincdir=\tthe base directory for libvirt include files.  (Default: {incdir}/libvirt)\n";
  print "--virtlibdir=\tthe base directory for libvirt libraries.  (Default: {libdir})\n";
  print "--fence_agents=\tlist of fence agents to configure.  (Default: all)\n";
  print "\t\tUse --fence_agents=help for a list\n";
  print "--fenceagentslibdir=\tspecify directory where to install common fence python lib.  (Default: /usr/share/fence)\n";
  print "--enable_crack_of_the_day\tEnable build of highly experimental features that rely on code that is not yet available for general use. (Default: no)\n";
  print "--enable_legacy_code\tEnable build of old/obsolete/unsupported code/tools.  (Default: no)\n";
  print "--enable_virt\tEnable building of virtualization code (Default: disabled)\n";
  exit $ret;
}

sub symlinks {
  my $dir = shift;
  my $pattern = shift;
  @args = "find $dir -type f -name $pattern";
  open (IFILE, "@args |");
  while (<IFILE>) {
    chomp;
    s|\./||g;
    s|.*make\/defines.mk||g;
    $dirname = dirname($_);
    $filename = basename($_);
    system("mkdir -p $objdir/$dirname");
    symlink("${cdir}/$_","$objdir/$dirname/$filename");
  }
  close IFILE;
  return 0;
}

$pwd = `pwd`;
chomp($pwd);

if (!$cc) {
  $cc="gcc";
}
if (!$cflags) {
  $cflags="-Wall -Wformat=2 -MMD";
  if (!$debug) {
    $cflags="${cflags} -O2";
  } else {
    $cflags="${cflags} -O0 -DDEBUG";
  }
  $cflags="${cflags} -g";
}
if ($extracflags) {
  $cflags="${cflags} ${extracflags}";
}
if (!$ldflags) {
  $ldflags="";
}
if ($extraldflags) {
  $ldflags="${ldflags} ${extraldflags}";
}
if (!$prefix) {
  $prefix="/usr";
}

print "\nChecking tree: ";
if (!$objdir) {
  $objdir="${pwd}";
}
$objdir = abs_path( $objdir );
$cdir = dirname ( abs_path( $0 ) );
unless ("$cdir" eq "$objdir") {
  chdir "$cdir";
}

if (! -d "$objdir/make") {
  print "setting up $objdir\n";
  mkdir "$objdir";
  symlinks(".","Makefile");
  symlinks(".","*.mk");
} else {
  print "nothing to do\n";
}

if (!$incdir) {
  $incdir="${prefix}/include";
}
if (!$libdir) {
  $libdir="${prefix}/lib";
}
if (!$ccsincdir) {
  $ccsincdir="${incdir}";
}
if (!$ccslibdir) {
  $ccslibdir="${libdir}";
}
if (!$cmanincdir) {
  $cmanincdir="${incdir}";
}
if (!$cmanlibdir) {
  $cmanlibdir="${libdir}";
}
if (!$logtincdir) {
  $logtincdir="${incdir}";
}
if (!$logtlibdir) {
  $logtlibdir="${libdir}";
}
if (!$openaisincdir) {
  $openaisincdir="${incdir}";
}
if (!$openaislibdir) {
  $openaislibdir="${libdir}/openais";
}
if (!$nssincdir) {
  $nssincdir="${incdir}/nss3";
}
if (!$nsslibdir) {
  $nsslibdir="${libdir}";
}
if (!$nsprincdir) {
  $nsprincdir="${incdir}/nspr4";
}
if (!$nsprlibdir) {
  $nsprlibdir="${libdir}";
}
if (!$virtincdir) {
  $virtincdir="${incdir}/libvirt";
}
if (!$virtlibdir) {
  $virtlibdir="${libdir}";
}
if (!$mandir) {
  $mandir="${prefix}/share/man";
}
if (!$sbindir) {
  $sbindir="${prefix}/sbin";
}
if (!$initddir) {
  $initddir="/etc/init.d";
}
if (!$docdir) {
  $docdir="${prefix}/share/doc/cluster";
}
if (!$logdir) {
  $logdir="/var/log/cluster";
}
if (!$syslogfacility) {
  $syslogfacility="LOG_LOCAL4";
}
if (!$sysloglevel) {
  $sysloglevel="LOG_INFO";
}
if (!$snmpbin) {
  $snmpbin="${prefix}/bin";
}
if (!$confdir) {
  $confdir="/etc/cluster";
}
if (!$fence_agents) {
  $fence_agents="all";
}
if (!$fenceagentslibdir) {
  $fenceagentslibdir="/usr/share/fence";
}
if (!$enable_crack_of_the_day) {
  $enable_crack_of_the_day="";
} else {
  print "\n********************************************************************************************\n";
  print "WARNING: *you* have explicitly enabled all possible experimental features under development!\n";
  print "         This code will have no mercy for your data and your machines. You have been warned!\n";
  print "\n********************************************************************************************\n";
  $cflags="${cflags} -DEXPERIMENTAL_BUILD";
}
if (!$enable_legacy_code) {
  $enable_legacy_code="";
} else {
  print "\n********************************************************************************************\n";
  print "WARNING: *you* have explicitly enabled the build of old/obsoleted/unsupported code/tools\n";
  print "         This code is provided *only* for backward compatibility\n";
  print "\n********************************************************************************************\n";
  $cflags="${cflags} -DLEGACY_CODE";
}
if (!$enable_virt) {
  $enable_virt="";
}
if (defined($release_version) && not length $release_version) {
  $release_version="";
}

if ((!$fence_agents) || ($fence_agents eq "all") || ($fence_agents eq "help")) {
  @args = "find fence/agents -mindepth 2 -maxdepth 2 -name Makefile -printf '%h'";
  open (IFILE, "@args |");
  while (<IFILE>) {
    chomp;
    s|fence\/agents\/| |g;
    s|lib ||g;
    $all_fence = $_;
  }
  close IFILE;
}

if ($fence_agents eq "help") {
  print "Available fence agents:\n$all_fence\n";
  exit 0;
}

if ((!$fence_agents) || ($fence_agents eq "all")) {
  print "\nThe following fence agents will be build on this system:\n$all_fence\n";
  print "\nNOTE: xvm will build only if --enable_virt has been specified\n\n";
  $fence_agents = $all_fence
}

open IFILE, "<make/defines.mk.input" or die "Can't redirect stdin";
open OFILE, ">${objdir}/make/defines.mk" or die "Can't redirect stdout";

print OFILE "# This file was generated by configure from defines.mk.input\n";

while (<IFILE>) {
  chomp;
  $_ =~ s/\@SRCDIR\@/$cdir/;
  $_ =~ s/\@CC\@/$cc/;
  $_ =~ s/\@CFLAGS\@/$cflags/;
  $_ =~ s/\@LDFLAGS\@/$ldflags/;
  $_ =~ s/\@OBJDIR\@/$objdir/;
  $_ =~ s/\@INCDIR\@/$incdir/;
  $_ =~ s/\@LIBDIR\@/$libdir/;
  $_ =~ s/\@CCSINCDIR\@/$ccsincdir/;
  $_ =~ s/\@CCSLIBDIR\@/$ccslibdir/;
  $_ =~ s/\@CMANINCDIR\@/$cmanincdir/;
  $_ =~ s/\@CMANLIBDIR\@/$cmanlibdir/;
  $_ =~ s/\@LOGTINCDIR\@/$logtincdir/;
  $_ =~ s/\@LOGTLIBDIR\@/$logtlibdir/;
  $_ =~ s/\@OPENAISINCDIR\@/$openaisincdir/;
  $_ =~ s/\@OPENAISLIBDIR\@/$openaislibdir/;
  $_ =~ s/\@NSSINCDIR\@/$nssincdir/;
  $_ =~ s/\@NSSLIBDIR\@/$nsslibdir/;
  $_ =~ s/\@NSPRINCDIR\@/$nsprincdir/;
  $_ =~ s/\@NSPRLIBDIR\@/$nsprlibdir/;
  $_ =~ s/\@VIRTINCDIR\@/$virtincdir/;
  $_ =~ s/\@VIRTLIBDIR\@/$virtlibdir/;
  $_ =~ s/\@MANDIR\@/$mandir/;
  $_ =~ s/\@SBINDIR\@/$sbindir/;
  $_ =~ s/\@INITDDIR\@/$initddir/;
  $_ =~ s/\@CONFDIR\@/$confdir/;
  $_ =~ s/\@DOCDIR\@/$docdir/;
  $_ =~ s/\@LOGDIR\@/$logdir/;
  $_ =~ s/\@SYSLOGFACILITY\@/$syslogfacility/;
  $_ =~ s/\@SYSLOGLEVEL\@/$sysloglevel/;
  $_ =~ s/\@SNMPBIN\@/$snmpbin/;
  $_ =~ s/\@FENCE_AGENTS\@/$fence_agents/;
  $_ =~ s/\@FENCEAGENTSLIBDIR\@/$fenceagentslibdir/;
  $_ =~ s/\@ENABLE_CRACK_OF_THE_DAY\@/$enable_crack_of_the_day/;
  $_ =~ s/\@ENABLE_LEGACY_CODE\@/$enable_legacy_code/;
  $_ =~ s/\@ENABLE_VIRT\@/$enable_virt/;

  print OFILE "$_\n";
}

close IFILE;

if (not defined($release_version)) {

  my $current_version;
  if ( -f 'make/official_release_version' ) {
    open OFFICIAL_VERSION, '<', "make/official_release_version";
    while (<OFFICIAL_VERSION>) {
      if ($_ =~ /VERSION/) {
	$current_version = $_;
      }
    }
    close OFFICIAL_VERSION;
  }

  if (not defined($release_version)) {
    if (not defined($current_version)) {
      $release_version = `date +%s`;
      chomp $release_version;
    } else {
      $release_version = $current_version;
      $release_version =~ s/.*"(.*)"\n/$1/;
    }
  }
}

print OFILE "RELEASE_VERSION = $release_version\n";
print OFILE "CFLAGS += -DRELEASE_VERSION=\\\"$release_version\\\"\n";

close OFILE;

open OFILE, ">${objdir}/.configure.sh.tmp" or die "Can't redirect stdout";
print OFILE "#!/bin/bash\n";
print OFILE "$0 @invoke \$@\n";
print OFILE "exit \$?\n";
close OFILE;

system("mv ${objdir}/.configure.sh.tmp ${objdir}/.configure.sh");

print "Completed Makefile configuration\n\n";
