#!/usr/bin/perl -w
###############################################################################
# configure -- a wrapper to cmake and its options                             #
# Copyright (C) 2006 by Allen Winter <winter@kde.org>                         #
#                                                                             #
# This program is free software; you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation; either version 2 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program; if not, write to the Free Software                 #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#                                                                             #
###############################################################################
#

## TODO:
## ??--enable-tests?? => -DKDE4_BUILD_TESTS=ON
## specify the name of the build subdir??
## WITH_FOO: there are several options, e.g. WITH_CUPS or WITH_Jasper.
##   If you disable them, cmake will not even try to find this package.
##   If it is enabled, cmake will try to find it. If it fails with this,
##   you can still adjust it manually as described above. 

use strict;
use Getopt::Long;
use Env qw (PATH CC);
use File::Path;

my($Prog) = 'configure';
my
$VERSION = '0.1'; #split line so MakeMaker can find the version here

my($help) = '';
my($version) = '';
my($prefix) = '';
my($debug) = '';
my($final) = '';
my($rpath) = '';
my($qtdir) = '';

exit 1
if(!GetOptions('help' => \$help,       # print help and exit
	       'version' => \$version, # print version and exit
	       'prefix=s' => \$prefix,
	       'enable-debug=s' => \$debug,
	       'enable-final' => \$final,
	       'disable-rpath' => \$rpath,
	       'with-qt-dir=s' => \$qtdir,
	      ));

&Help() if ($help);
&Version() if ($version);

# Step1:  find a cmake with minimum required version in $PATH

# Step2:  process command line args
my($opts) = " .. ";
my($envs) = "";

if ($prefix)
  {$opts .= " -DCMAKE_INSTALL_PREFIX=$prefix";}
else
  {$opts .= " -DCMAKE_INSTALL_PREFIX=/usr";}


if ($rpath) {$opts .= " -DCMAKE_SKIP_RPATH=1";}

if (! $final) {
  if ($debug) {
    if ($debug eq "yes") {$opts .= " -DCMAKE_BUILD_TYPE=debug";}
    if ($debug eq "no")  {$opts .= " -DCMAKE_BUILD_TYPE=release";}
    if ($debug eq "full"){$opts .= " -DCMAKE_BUILD_TYPE=debugfull";}
  } else {
    {$opts .= " -DCMAKE_BUILD_TYPE=relwithdebinfo";}
  }
} else {
  {$opts .= " -DCMAKE_BUILD_TYPE=release";}
}

if ($qtdir) {$envs .= " PATH=$qtdir/bin:$PATH";}

# Step 3: create build directory, if necessary
if (! defined($CC)) {
  $CC="gcc";
}
my($d)="build-$CC";
if (! -d "$d") {
  mkpath("$d") || die "$d: $!\n";
}

system("(cd $d; $envs cmake$opts)");

exit 0;

#==============================================================================
# Help function: print help message and exit.
sub Help {
  print "\n";
  print "`$Prog' configures this package to adapt to many kinds of systems.\n";
  print "\n";
  print "Usage: $Prog [OPTION]... [VAR=VALUE]...\n";
  print "\n";
  print "To assign environment variables (e.g., CC, CFLAGS...), specify them as\n";
  print "VAR=VALUE.  See below for descriptions of some of the useful variables.\n";
  print "\n";
  print "Defaults for the options are specified in brackets.\n";
  print "\n";
  print "Configuration:\n";
  print "  --help              display this help and exit\n";
  print "  --version           display version information and exit\n";
  print "\n";
  print "Installation directories:\n";
  print "  --prefix=PREFIX         install architecture-independent files in PREFIX\n";
  print "\n";
  print "By default, `make install' will install all the files in\n";
  print "`/usr/bin', `/usr/lib' etc.  You can specify\n";
  print "an installation prefix other than `/usr' using `--prefix',\n";
  print "for instance `--prefix=\$HOME'.\n";
  print "\n";
  print "For better control, use the options below.\n";
  print "\n";
  print "Fine tuning of the installation directories:\n";
  print "  N/A\n";
  print "\n";
  print "Program names:\n";
  print "  N/A\n";
  print "\n";
  print "System types:\n";
  print "  N/A\n";
  print "\n";
  print "Optional Features:\n";
  print "  --enable-debug=ARG      enables debug symbols (yes|no|full) default=no\n";
  print "  --disable-debug         disables debug output and debug symbols default=no\n";
  print "  --enable-final          build size optimized apps\n";
  print "  --disable-rpath         do not use the rpath feature of ld\n";
  print "\n";
  print "Optional Packages:\n";
  print "  --with-qt-dir=DIR       where the root of Qt is installed\n";
  print "\n";
  print "Some influential environment variables:\n";
  print "  CC          C compiler command\n";
  print "  CFLAGS      C compiler flags\n";
  print "  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a\n";
  print "              nonstandard directory <lib dir>\n";
  print "  CPPFLAGS    C/C++ preprocessor flags, e.g. -I<include dir> if you have\n";
  print "              headers in a nonstandard directory <include dir>\n";
  print "  CPP         C preprocessor\n";
  print "  CXX         C++ compiler command\n";
  print "  CXXFLAGS    C++ compiler flags\n";
  print "  CXXCPP      C++ preprocessor\n";
  print "  F77         Fortran 77 compiler command\n";
  print "  FFLAGS      Fortran 77 compiler flags\n";
  print "\n";
  print "Use these variables to override the choices made by `configure' or to help\n";
  print "it to find libraries and programs with nonstandard names/locations.\n";
  print "\n";

  exit 0 if $help;
}

# Version function: print the version number and exit.
sub Version {
  print "\n$Prog, version $VERSION\n\n";
  exit 0 if $version;
}
