#!/bin/sh
#
# The yhttpd Project (2003 - 2004)
#

if ! which perl >/dev/null
then
 echo You need to have Perl in your PATH
 exit 1
fi

if ! test -f ../g++.version
then
 echo You need to run ./configure of the top level source dir first
 exit 1
fi 

perl -e '
 use strict;
 $|=1;

 my %libadd;
 my %incadd;
 my $deepness = 500;

 my @headers = (
   "dlfcn.h",
   "pthread.h",
   "netinet/in.h",
   "time.h",
   "ncurses.h"
 );

 my @libs = (
   "libncurses.so"
 );

 sub remove_from_array
 {
  my $elem  = shift;
  my $array = shift;

  for ( my $i = 0; $i <= $#$array; ++$i )
  {
   if ( $$array[$i] eq $elem )
   {
    splice(@$array,$i,1); 
    last;
   }
  }
 } 

 open FILE, "glob.h" or die "glob.h: $!\n";
 while(<FILE>)
 {
  if ( /\/\/#define DATABASE/ )
  {
   remove_from_array("mysql/mysql.h",\@headers);
   remove_from_array("libmysqlclient.so",\@libs);
  }

  elsif ( /\/\/#define NCURSES/ )
  {
   remove_from_array("ncurses.h",\@headers);
   remove_from_array("libncurses.so",\@libs);
  }
 }
 close FILE;

 my @headerpaths = (
   $ENV{HOME}."/include",
   $ENV{HOME}."/usr/include",
   "/include",
   "/usr/include",
   "/usr/local/include",
   "/usr/pkg/include",
   "/opt/include",
   "/opt/local/include"
 );

 my @libpaths = (
   $ENV{HOME}."/lib",
   $ENV{HOME}."/usr/lib",
   "/lib",
   "/usr/lib",
   "/usr/local/lib",
   "/usr/pkg/lib",
   "/opt/lib",
   "/opt/local/lib"
 );

 if ( defined $ENV{YHTTPDHEADERPATHS} ) {
   map { print "Adding $_...\n";
         unshift @headerpaths, $_ } split /:/, $ENV{YHTTPDHEADERPATHS};
 }

 if ( defined $ENV{YHTTPDLIBPATHS} ) {
   map { print "Adding $_...\n";
        unshift @libpaths, $_ } split /:/, $ENV{YHTTPDLIBPATHS};
 }

 sub check {
  my $deep  = shift;

  if ($deep == 0) {
    print "Looking too deep! ($deepness)\n";
    exit(1);
  } 

  my $check = shift;
  my $print = 1;
  if ( $_[-1] eq "subsearch"  ) {
    $print = 0;
    pop(@_);
  }

  print "Checking for $check... " if $print;
  foreach (@_) {
    if ( -f "$_/$check" ) {
       print "OK\n";
       return "" if $_ eq "/usr/lib" or $_ eq "/usr/include"; 
       return $_;
    }
  }

  foreach (@_) {
    next unless -d $_;
    opendir D, $_ or warn "$_: $!\n"; my @dir = readdir(D);
    closedir D;
    foreach my $dir ( @dir ) {
       next if $dir =~ /^\.+$/ or !-d "$_/$dir";
       my $path = &check($deep-1, $check, "$_/$dir", "subsearch");
       return $path if $path ne "";
    }
  }

  if ($print) {
    print "NOT OK\n";
    print "Please make sure that you have the needed software installed!\n";
    print "If you have a special path for your includes then edit src/configure!\n";
    print "Or set the environment variables YHTTPDHEADERPATHS and YHTTPDLIBPATHS.\n";
    print " Example: setenv YHTTPDHEADERPATHS \"/your/header/includes:/a/includes\"\n";
    print "(The environment variables have to be seperated by an :)\n"; 
    exit(1);
  }
  return "";
 }

 map { $incadd{&check($deepness, $_, @headerpaths)}++ } 
  @headers;

 map { $libadd{&check($deepness, $_, @libpaths)}++ } 
  @libs;

 sub makeadd {
  my $flag = shift;
  my $add  = shift;
  my $ret  = "";

  foreach (keys %$add) {
    next unless /.+/;
    $ret .= "$flag$_ ";
  }
  return $ret;
 }

 my $incadd = &makeadd("-I", \%incadd);
 my $libadd = &makeadd("-L", \%libadd);

 foreach ( @libs ) 
 {
  $libadd .= "-l$_ "
   if s/^lib// and s/\.so$//;
 } 

 print "Incadd: $incadd\n";
 print "Libadd: $libadd\n";

 `echo $incadd > includes.add`;
 `echo $libadd > libs.add`;

 print "Creating new base Makefile...\n";
 unlink("Makefile") if -f "Makefile";
 open Fin, "Makefile.in" or die "Makefile.in: $!\n";
 open Fout, ">Makefile" or die "Makefile: $!\n";

 my $cpp = `echo *.cpp */*.cpp contrib/*/*.cpp | sort`;
 my $compiler = `tail -n 1 ../g++.version`;
 my $version  = `tail -n 2 ../g++.version | head -n 1`;
 my $uname    = `uname -srm`;
 chomp $uname;
 print "Configuring for $uname...\n";
 chomp $cpp;
 chomp $version;
 while (<Fin>) {
   s/^(CC=).*\n/$1$compiler/;
   s/^(SRCS=).*/$1$cpp/;
   s/ -frepo// unless $version =~ /3\.4/;
   if ( $uname !~ /Linux/i ) {
     print "Disabling -ldl flag...\n" if s/ -ldl//;
   }
   print Fout;
 }
 close Fin;
 
 my $args = join(" -",@ARGV);
 $args = "-".$args unless $args eq "";

 foreach my $cppfile (split / /, $cpp) {
   my $ofile = $cppfile;
   $ofile =~ s/\.cpp/\.o/;
   print Fout "../obj/$ofile: $cppfile\n";
   print Fout "\t\@if ! test -d `dirname ../obj/$ofile`; then mkdir -p `dirname ../obj/$ofile`; fi\n";
   if ( $ofile =~ /contrib\/.+/ ) {
    my $dirname = `dirname $ofile`;
     print Fout "\t\@echo -n \"Compiling contributed class $ofile \"\n";
   } else {
    print Fout "\t\@echo -n \"Compiling base class $ofile \"\n";	
   }
   print Fout "\t\@\$(CC) \$(CFLAGS) \$(INCLUDES) $args -c -o ../obj/$ofile $cppfile\n";
   print Fout "\t\@du -hc ../obj/$ofile | tail -n 1 | sed s/total// | sed \"s/ //g\"\n";
 }

 close Fout;

 open F, "msgs.h" or die "msgs.h: $!\n";
 my @msgs = <F>;
 close F;
 unlink("msgs.h");
 open F, ">msgs.h" or die "msgs.h: $!\n";
 foreach (@msgs) {
   s/(UNAME)(.+)$/UNAME "$uname"/;
   print F; 
 }
 close F;

 if ( -d "mods" ) {
   chdir("mods");
   my $cflags = "-fno-inline -fno-default-inline";
   $cflags .= " -nostdlib" if $uname !~ /Linux/i;
   system("echo $cflags > cflags.add");
   system("./configure");
   chdir("..");
 }

 exit(0);

' `echo "$*" | sed "s/-//g"` 
