#!/usr/bin/perl -w

# easies the pain with libtool libraries and their versioning
# so it atomatically creates the <package_name_with_version>.* files for the
# various dh_commands from plain <package_name>.* files in debian/ or 
# debian/local
#
# possible parameters are:
# clean (which removes any automatically generated files)
# makedeps (
# gendeps (
# --debname lib<name> (should be the prefix for the library package)
#           e.g. when packaging the library libfoo.2.0.0 into the package libfoo
#                then you would use:   --debname libfoo
#                so this parameter also specifies a part of the libraryname.
#                The rest of the libraryname is determined automatically
# --libnames libraryname1 libraryname2 ...
#           this parameter assummes that the rest of the parameters are arguments
#           for this parameter, so specify all options before this.
#           currently this parameter is ingored
#           It should add the additionally specified libraries to the package
#           and it must be specified when there is no library beginning with
#           the name specified with --debname <name> 

use strict;
use debian::scripts::dh_helper;

my $dir       = $debiandir;

if ( !defined($ENV{"BUILD_TREE"}) ) {
    print( '$BUILD_TREE not set, exiting'."\n" );
    exit( 1 );
}
my $buildtree = $ENV{"BUILD_TREE"};
$buildtree =~ s%\/$%%;

if ( -d "$debiandir/local" ) {
    $dir = "$debiandir/local";
}

# default configuration is nothing
my %config = ( 'libdir'=>'src/.libs', 'debug'=>1 );

# parse cmd args
my $temp;
while (@ARGV) {
    $temp = shift;
    if ($temp eq "--clean") { $config{'needclean'} = 1; }
    elsif ($temp eq "--makedeps") { $config{'needlist'} = 1; }
# gendeps is something that won't work since the script depends on files
# whos complete filename is unknown and cannot be guessed
#    elsif ($temp eq "--gendeps") { $config{'needdeps'} = 1; }
    elsif ($temp eq "--debname") { $config{'debname'} = shift; }
    elsif ($temp eq "--libdir") { $config{'libdir'} = shift; }
    elsif ($temp eq "--libnames") {
	while(@ARGV) {
	    push(@{$config{'libs'}}, shift);
	}
    }
}

# print error & exit if not required arguments were specified
die( "--debname wasn't specified" )
    if ( !defined($config{'debname'}) );

# make sure no additional libs specified
if ( defined($config{'libs'}) ) {
    if( ! grep( /^$config{'debname'}$/, @{$config{'libs'}} ) ) {
	push( @{$config{'libs'}}, $config{'debname'} );
    }
} else {
    push( @{$config{'libs'}}, $config{'debname'} );
}

print( "debname: $config{'debname'}\n" ) if ($config{'debug'});

# determine the library major, minor, etc. version
my( $libpkg, $libdevpkg, $mmversion, $ageversion, $agemajor);
if( defined($config{'debname'}) ) {
    my( @FILES );
    if ( ! -d "$buildtree/$config{'libdir'}" ) {
	print( "can't compute library version since ".
	       "$buildtree/$config{'libdir'} is ".
	       "not present\n" );
	exit(0);
    }
    opendir( DIR, "$buildtree/$config{'libdir'}" ) or 
	die( "couldn't open dir $buildtree/$config{'libdir'}" );
    @FILES = grep { !/^\./ && /^$config{'debname'}.*\.so\.[0-9]+\.[0-9]+\.[0-9]+/
} readdir( DIR );
    closedir( DIR );
    if( $FILES[0] =~ m/^$config{'debname'}-*(.*)\.so\.([0-9]+)\.([0-9]+)\.([0-9]+)/ ) {
	$mmversion = $1;                 # version number in library name
	$ageversion = $2.".".$3.".".$4;  # complete version number
	$agemajor = $2;                  # highest number of version
    
	$libpkg="$debiandir/".$config{'debname'}.$mmversion;
	$libdevpkg=$libpkg."-dev";
    } else {
	$libpkg="$debiandir/".$config{'debname'};
	$libdevpkg=$libpkg."-dev";
    }
}

print( "mmv: $mmversion, agev: $ageversion, agem: $agemajor\n" ) 
    if ($config{'debug'});
print( "pkg: $libpkg, devpkg: $libdevpkg\n" )
    if ($config{'debug'});

# write generic library copy information
if ($config{'needclean'}) { # remove files
    system("rm -f ${libpkg}.files ${libpkg}.links");
    system("rm -f ${libdevpkg}.files ${libdevpkg}.links");
} elsif ($config{'needlist'}) { # output dependency list
    print("${libpkg}.files ${libpkg}.links ".
	  "${libdevpkg}.files ${libdevpkg}.links ");
} else {
    open( FILE, ">".$libpkg.".files" );
    print( FILE "usr/lib/".$config{'debname'}."-".$mmversion.".so.".$ageversion);
    close( FILE );

    open( FILE, ">".$libpkg.".links" );
    print( FILE "usr/lib/".$config{'debname'}."-".$mmversion.".so.".$ageversion.
	   " "."usr/lib/".$config{'debname'}."-".$mmversion.".so.".$agemajor );
    close( FILE );

    open( FILE, ">".$libdevpkg.".files" );
    print( FILE "usr/lib/".$config{'debname'}.".a\n" );
    print( FILE "usr/lib/".$config{'debname'}.".la\n" );
    close( FILE );

    open( FILE, ">".$libdevpkg.".links" );
    print( FILE "usr/lib/".$config{'debname'}."-".$mmversion.".so.".$ageversion.
	   " "."usr/lib/".$config{'debname'}."-".$mmversion.".so\n" );
    print( FILE "usr/lib/".$config{'debname'}."-".$mmversion.".so.".$ageversion.
	   " "."usr/lib/".$config{'debname'}.".so\n" );
    close( FILE );
}

# add to package files by using files found in debian/packages.d
opendir( DIR, "$debiandir/packages.d" );
my( $file, $outfile );
my @FILES = 
    grep { /^($config{'debname'}|$config{'debname'}-dev)\.in$/ } readdir(DIR);
closedir( DIR );
print( "found @FILES to split\n" ) if ($config{'debug'});
while( defined($file = shift(@FILES)) ) {
    if ( $file eq $config{'debname'}.".in" ) {
	$outfile = $libpkg;
    } elsif ( $file eq $config{'debname'}."-dev.in" ) {
	$outfile = $libdevpkg;
    }
    print( "outfile: $outfile\n" ) if ($config{'debug'});
    debhelper_split( $file, $outfile, 1, \%config );
}

exit(0);

