#!/usr/bin/perl -w
#
# This is the PerlQt configuration script
#

use Config;
use subs qw(write_config);

$| = 1;

print <<INTRO;

So, you have chosen to build PerlQt...

This script will help with your compiling configuration of PerlQt and
will optionally run the 'build' script to compile PerlQt unattended.

You will be asked several questions. The default answers will be surrounded
with [], and just pressing enter without any input will use the default
setting. If you don't want the default, but rather a blank entry, just
enter 'none'.

If you have any problems, send a bug-report to jql\@accessone.com.

INTRO

$qtdir = get('What is your Qt dir?',
	     $ENV{'QTDIR'} ? $ENV{'QTDIR'} : '/usr/lib/qt');
$make = get("What is your 'make' program called?", $Config{'make'});
$perl = get("How shall I invoke perl?", $^X);
$cc = get('What C++ compiler shall I use?', 'g++');
$opts = get('What C++ optimization flags shall I use?', $Config{'optimize'});
$cppflags = get('What C++ compiler flags shall I use?', $Config{'cppflags'});
$include = get('What include directives shall I use? (excluding libperlqt)',
	       '-I$(QTDIR)/include');

{
    use ExtUtils::MakeMaker;
    require ExtUtils::Liblist;

    $libqt = '-lqt';
    open(QSAVEOUT, ">&STDOUT") || die "failed to save STDOUT: $!";
    open(STDOUT, ">qcheck") || die "could not redirect output to qcheck: $!";
    unless(ExtUtils::Liblist::ext('ExtUtils::MM_Unix', $libqt)) {
	$libqt = "-L$qtdir/lib $libqt" if
	    ExtUtils::Liblist::ext('ExtUtils::MM_Unix', "-L$qtdir/lib $libqt");
    }
    open(STDOUT, ">&QSAVEOUT") || die "failed to restore STDOUT: $!";
    close QSAVEOUT;
    unlink 'qcheck';
}

$libs = get('What library flags shall I add? (excluding libperlqt)',
	    $libqt);
$libdir = get('What directory should libperlqt be installed to?',
	      '/usr/local/lib');

write_config <<CONFIG, 'Qt.config';
# This file is required by each PerlQt module's Makefile.PL, and
# a sub-ref placed in WriteMakefile()'s CONFIGURE parameter, consisting
# of the code 'sub { return \\%QtConfig }', passes the configuration
# parameters in %QtConfig to MakeMaker. All MakeMaker parameters are
# valid %QtConfig elements. See ExtUtils::MakeMaker(3) for cool info.
#
# All pathnames are relative to the module's Makefile.PL, not the main
# one.

%QtConfig = (
    CC => '$cc',
    CCFLAGS => '$cppflags',
    INC => '$include -I../libperlqt/include',
    LIBS => '-lperlqt $libs',
    OPTIMIZE => '$opts'
);
CONFIG

write_config <<CONFIG, 'libperlqt/Qt.config';
# This file is required into libperlqt's Makefile.PL, and a sub-ref
# placed in WriteMakefile()'s CONFIGURE parameter, consisting of the
# code 'sub { return \\%QtConfig }', passes the configuration parameters
# in %QtConfig to MakeMaker. All MakeMaker parameters are valid %QtConfig
# elements. See ExtUtils::MakeMaker(3) for cool info.
#
# All pathnames are relative to the libperlqt build dir, not the main one.

\$InstallDir = '$libdir';

%QtConfig = (
    CC => '$cc',
    CCFLAGS => '$cppflags',
    INC => '$include -Iinclude',
    OPTIMIZE => '$opts'
);
CONFIG

write_config <<CONFIG, 'build.config';
# This file contains the information the build script needs to run.

\$Make = '$make';
\$Perl = '$perl';
\$QtDir = '$qtdir';
\$Libdir = '$libdir';
CONFIG

$build = get("Do you want me to build PerlQt now?", "n");
exec $^X, 'build' if $build =~ /^y/i;

sub get {
    my $question = shift;
    my $default = shift;

    print "$question [$default] ";
    my $input = <STDIN>;
    chomp($input);
    return $default unless $input =~ /\S/;
    return '' if $input =~ /^\s*none\s*$/i;
    return $input;
}

sub write_config {
    my $config = shift;
    my $qtconfig = shift;

    print "Writing $qtconfig...";
    open QT_CONFIG, ">$qtconfig" or
	die "Could not open $qtconfig for writing: $!";
    my $date = scalar localtime;

    print QT_CONFIG <<CONFIG;
# This file was automatically created by Configure on $date
#
CONFIG

    print QT_CONFIG $config;
    close QT_CONFIG;
    print "\n";
}
