#!/usr/bin/perl
#
# a helper script for gnome-pkgtool that builds a package from source
#
# cmdline arguments:
#	-dir=Build_Dir
#	-source=source.tar.gz
#	-name=Package_Name
#	-version=Package_Version
#	-flags='configure flags'
#	-opt='optimization CFLAGS'
#	-patch='patch' (can be used more than once)

my $build_dir;
my $filename;
my $package;
my $version;
my $configure_flags;
my $optimization;

my $checkinstall = `which checkinstall`;
$checkinstall =~ s/[\s\r\n]*$//g;

$_ = shift;
if($_ eq '') {
	usage();
	exit(1);
}

while(/.+/) {
	print "[$_]\n";
	if(/^\-dir\=(.+)$/) {
		$build_dir = $1;
	}
	elsif(/^\-tarball\=(.+)$/) {
		$filename = $1;
		if ($filename =~ s/^[\'\"]//) {
			$filename =~ s/[\'\"]$//;
		}
	}
	elsif(/^\-name\=?(.+)$/) {
		$package = $1;
		if ($package =~ s/^[\'\"]//) {
			$package =~ s/[\'\"]$//;
		}
		$package =~ s/\s/\_/g;
	}
	elsif(/^\-version\=?(.+)$/) {
		$version = $1;
		if ($version =~ s/^[\'\"]//) {
			$version =~ s/[\'\"]$//;
		}
	}
	elsif(/^\-flags\=?(.+)$/) {
		$configure_flags = $1;
		if ($configure_flags =~ s/^[\'\"]//) {
			$configure_flags =~ s/[\'\"]$//;
		}
	}
	elsif(/^\-opt\=?(.+)$/) {
		$optimization = $1;
		if ($optimization =~ s/^[\'\"]//) {
			$optimization =~ s/[\'\"]$//;
		}
	}
	elsif(/^\-patch\=(.+)$/) {
	    $patch{$1} = $1;
		$_ = $1;
		if ($patch{$_} =~ s/^[\'\"]//) {
			$patch{$_} =~ s/[\'\"]$//;
		}
	}
	else {
		print "Unrecognized command-line option: $_\n";
		usage();
		exit(1);
	}
	$_ = shift;
}

if($build_dir eq '') {
    $build_dir = `pwd`;
    $build_dir =~ s/[\r\n]//g;
}

if($filename eq '') {
	print "Please specify source tarball\n";
	exit(1);
}
elsif($package eq '') {
	print "Please specify package name\n";
	exit(1);
}
elsif($version eq '') {
	print "Please specify package version\n";
	exit(1);
}

########## unpack tarball
my $compression;
if($filename =~ /^.+\.tar\.([gb]z2??)$/) {
	$compression = $1;
}
else {
	print "$filename does not look like a tarball\n";
	exit(1);
}

print "Unpacking $filename\n";
if ($compression eq 'bz2') {
	system "tar jxf \"$filename\" --directory $build_dir/";
}
elsif ($compression eq 'gz') {
	print "tar zxf \"$filename\" --directory $build_dir\n";
	system "tar zxf \"$filename\" --directory $build_dir";
}
else {
	die "$package: I don't know how to unpack '\.$compression' archive.\n";
}
#check for tar errors
if ($? != 0) {
	die "$package: tar returned error status\n";
}

foreach $p (keys %patch) {
    print "Patching with $p\n";
    system "cd $build_dir/$package-$version && patch -i $p -p 1";
}

########## configure
if (!($configure_flags =~ /\-\-prefix\=[^\s]+/)) {
	$configure_flags = "$configure_flags --prefix=/usr";
}
if (!($configure_flags =~ /\-\-sysconfdir\=[^\s]+/)) {
	$configure_flags = "$configure_flags --sysconfdir=/etc";
}
if (!($configure_flags =~ /\-\-localstatedir\=[^\s]+/)) {
	$configure_flags = "$configure_flags --localstatedir=/var";
}

print "Configuring $package version $version\n";
if(!($configure_flags eq '')) {
	print "Configure flags: $configure_flags\n";
}
	system "cd $build_dir/$package-$version && ./configure $configure_flags";
if($? != 0) {
	my $err = $? >> 8;
	print "$package: configure failed ($err)\n";
	exit(1);
}

print "Building $package version $version\n";
my $cmd_make;
if($optimization =~ /./) {
    $cmd_make = "make CFLAGS=\'$optimization\'";
}
else {
    $cmd_make = "make";
}
system "cd $build_dir/$package-$version && $cmd_make";
if($? != 0) {
	my $err = $? >> 8;
	print "$package: make failed ($err)\n";
	exit(1);
}

########## install
print "Checking for previous installation of $package\n";
my $release = 1;
$_ = `ls /var/log/packages/$package-* 2> /dev/null`;
if(($? == 0) && (!($_ eq ''))) {
	foreach $logfile (split(/\n/, $_)) {
		$logfile =~ s/^.*\///g;
		$logfile =~ /^(.*)\-([^\-]+)\-[^\-]+\-([0-9]+)$/;
		if($1 eq $package) {
			print "Removing previous installation of $package ($logfile)\n";
			system "removepkg $logfile";
			if($2 == $version) {
				$release = $3 + 1;
			}
		}
	}
}

#system "spec2desc $build_dir/$package-$version";

my $checkinstall_version = `$checkinstall --version 2>/dev/null|grep checkinstall|tr -s " "|cut -d " " -f 2`;
$checkinstall_version =~ s/[\s\r\n,]//g;
$checkinstall_version = substr($checkinstall_version,0,3);
my $arch = `/usr/bin/uname -m`;
my $install_flag="";
if($checkinstall_version eq "1.6") {
	$install_flag="--install=yes";
}
$arch =~ s/[\s\r\n]//g;
system "cd $build_dir/$package-$version && $checkinstall -S -y --pkgname=$package --pkgversion=$version --arch=$arch --pkgrelease=$release --strip=yes --gzman=no --newslack --autodoinst=yes $install_flag";
if($? != 0) {
	$err = $? >> 8;
	die "$package: checkinstall failed ($err)\n";
}

## clean up
#system("[ -d $build_dir ]");
#if($? == 0) {
#	print("Removing build dir $build_dir\n");
#	system("yes | rm -r $build_dir > /dev/null");
#	if($? != 0) {
#		print "Warning: could not remove build dir $build_dir\n";
#	}
#}

print("Running ldconfig\n");
system("ldconfig");


print "$package: done.\n";



sub usage {
	print "usage:\n";
	print "(later)\n";
}
