#!/usr/bin/perl -w
$VERSION = '1.02';

use strict;
use Symbol;
use Config;
#use Fatal qw(open);
use vars qw(%SavedConfig %PigConfig %C $VERSION);

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

$| = 1;

%C = (
  qtdir => $ENV{'QTDIR'} ? $ENV{'QTDIR'} : '/usr/lib/qt',
  make => $Config{'make'},
  perl => $^X,
  cc => 'g++',
  ld => 'g++',
  opts => $Config{'optimize'},
  cppflags => $Config{'cppflags'},
  include => '-I$(QTDIR)/include',
  libpqt => "$Config{prefix}/lib",
  argcount => 3,
);

if (-e "./perlqt.conf") {
  # can overwrite %C
  eval { require  "./perlqt.conf" }; # ok if fail
  if (!$@) {
      while (my ($k,$v) = each %SavedConfig) {
         # maybe only accept some keys? XXX
         $C{$k} = $v;
      }
  }
}

$C{libs} = "-L$C{qtdir}/lib -lqt" unless $C{libs};

# should try architechure specific defaults XXX

$C{qtdir} = get('What is your Qt dir?', $C{qtdir});
$C{make} = get("What is your 'make' program called?", $C{'make'});
$C{perl} = get("How shall I invoke perl?", $C{perl});
$C{cc} = get('What C++ compiler shall I use?', $C{cc});
$C{ld} = get('What C++ linker shall I use?', $C{ld});
$C{opts} = get('What C++ optimization flags shall I use?', $C{'opts'});
$C{cppflags} = get('What C++ compiler flags shall I use?', $C{'cppflags'});
$C{include} = get('What include directives shall I use?', $C{include});
$C{libs} = get('What libraries should I link in?', $C{libs});
$C{libpqt} = get('Where should I install libpqt?', $C{libpqt});

$C{argcount} = get('How many signal/slot arguments shall I support?',
                  $C{argcount});

do {
    my $verbose = get('How about some verbosity while you wait (Yes, No, Silent)?', 'Y');
    my $vflag = '';
    if($verbose =~ /^y/i) { $vflag = '-v' }
    elsif($verbose =~ /^s/i) { $vflag = '-s' }

    print "Generating signal/slot stubs..." if $verbose !~ /^s/;
    my $binclude = $C{include};
    $binclude =~ s/\$\((.*?)\)/\$$1/g;
    system "$C{cc} $binclude -o types types.c";
    system "$C{perl} gensigslot -n $C{argcount} int long short bool float double 'void*'";
    print "\n" if $verbose !~ /^s/;

    system "$C{perl} pig Qt '$C{include}' $C{libs} -include pqt.h -lpqt $vflag";
};
print "Generated source-code.\n";
print "Building supplemental perlqt.conf...";

for my $cnf ('src/Qt/perlqt.conf') {
    my $fh = gensym;
    open $fh, ">$cnf";
    print $fh "# uncomment the following line to regenerate:\n#return 0;\n\n";
    print $fh qq[
%PigConfig = (
    'CC' => "\Q$C{cc}\E",
    'CCFLAGS' => "\Q$C{cppflags}\E",
    'LD' => "\Q$C{ld}\E",
    'INC' => "\Q-I../../include -I.. -I. $C{include}\E",
    'LIBS' => "\Q-L$C{libpqt} -lpqt $C{libs}\E",
    'OPTIMIZE' => "\Q$C{opts}\E"
);

\$VERSION = "\Q$VERSION\E";
1;
];
    close $fh;
}

for my $cnf (qw(libpqt/perlqt.conf perlqt.conf)) {
    my $fh = gensym;
    open $fh, ">$cnf";
    print $fh "# uncomment the following line to regenerate:\n#return 0;\n\n";
    print $fh qq[
%PigConfig = (
    'CC' => "\Q$C{cc}\E",
    'CCFLAGS' => "\Q$C{cppflags}\E",
    'LD' => "\Q$C{ld}\E",
    'INC' => "\Q-I../include -I../src -I../src/Qt $C{include}\E",
    'LIBS' => "\Q$C{libs}\E",
    'OPTIMIZE' => "\Q$C{opts}\E"
);

\$VERSION = "\Q$VERSION\E";
\$LibDest = "\Q$C{libpqt}\E";

];
    for my $k (qw(qtdir make perl cc ld opts cppflags include
                  libs libpqt argcount)) {
       print $fh qq[\$SavedConfig{$k} = "\Q$C{$k}\E";\n];
    }
    print $fh "\n1;\n";
    close $fh;
}

print "\n";

do {
    my $compile = get('PerlQt is ready. Shall I compile for you?', 'N');
    exit if $compile !~ /^y/i;
};
print "Alright, first I'll build and install libpqt.\n";
system "cd libpqt; $C{perl} Makefile.PL";
system "cd libpqt; $C{make} install";
system "cd src/Qt; $C{perl} Makefile.PL";
system "cd src/Qt; $C{make}";

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;
}
