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

use subs qw(run);
use vars qw($Libdir);

require "build.config";
print "Do you want to install PerlQt now? [n] ";
chomp($install = <STDIN>);
$install = ($install =~ /^y/i);

unless($install) {
    print <<END;

Alright, I will not install PerlQt.
I will be installing libperlqt into $Libdir, though.
END
}

print "\nBuilding PerlQt...\n";
print "> cd libperlqt\n";
chdir "libperlqt" or die "Can't cd to libperlqt: $!";
run "$Perl Makefile.PL" unless -e "Makefile" and
    -M "Makefile.PL" > -M "Makefile" and -M "Qt.config" > -M "Makefile";
run "$Make";
run "$Make install";
print "> cd ..\n";
chdir ".." or die "Can't cd to ..: $!";
run "$Perl Makefile.PL" unless -e "Makefile" and
    -M "Makefile.PL" > -M "Makefile" and -M "Qt.config" > -M "Makefile";
run "$Make";
if($install) {
    run "$Make install";
} else {
    print "Please run '$Make install' to install PerlQt\n";
}

sub run {
    print "> $_[0]\n";
    my $sysret = system "QTDIR=\'$QtDir\' $_[0]";
    if($sysret == 0xff00) {
	die "system \"QTDIR=\'$QtDir\' $_[0]\" failed: $!";
    } elsif($sysret & 0xff) {
	$sysret &= ~0xff80;
	die "Fatal signal $sysret recieved during \"$_[0]\"";
    } elsif($sysret > 0x80) {
	die "\"$_[0]\" failed miserably";
    }
}
