#!/usr/bin/perl -w

#
# Debhelper to automatically register Common Lisp components
#
#  Copyright (C) 2005, 2006 by René van Bevern
#

=head1 NAME

dh_lisp - register Common Lisp source and implementations

=head1 SYNOPSIS

B<dh_lisp> [S<I<debhelper options>>] [S<I<implementation>>]

=head1 DESCRIPTION

dh_lisp is a debhelper program that is responsible for registering
Common Lisp source and implementations with the Common Lisp Controller.

For Common Lisp library packages dh_lisp will automatically generate the
postinst and prerm commands needed to interface with the Common Lisp
Controller. It also links the given ASDF system definitions
appropriately. It takes into account all ASDs that you installed below
usr/share/common-lisp/source (for example with L<dh_install>(1)).

If dh_lisp finds precompiled files from CLISP, SBCL or CMUCL, it will
add a dependency on the implementation that can read the FASL version
used by the implementation currently installed. (this is most likely
the one which built the binaries in the package)

If you supply I<implementation>, dh_lisp automatically installs the
implementation-specific script, which is expected to reside in
debian/I<implementation>.sh. dh_lisp automatically generates the
necessary maintainer scripts to register the implementation with
the Common Lisp Controller.

=head1 OPTIONS

=over 4

=item B<-n>, B<--noscripts>

do not add to maintainer scripts

=item B<-d>

do not generate dependencies on implementations for binary files in
the package

=item I<implementation>

Install the debian/I<implementation>.sh script and generate maintainer
scripts to (un)register I<implementation> at the Common Lisp Controller.

=back

=head1 NOTES

Note that this command is not idempotent. "dh_clean -k" should be called
between invocations of this command. Otherwise, it may cause multiple
instances of the same text to be added to maintainer scripts.

=head1 SEE ALSO

L<debhelper(7)>,

The Common Lisp in Debian Manual: S<I<http://cl-debian.alioth.debian.org/clid/>>

=head1 AUTHOR

RenE<eacute> van Bevern <rvb@debian.org>

=cut

use strict;
use File::Find;
use Debian::Debhelper::Dh_Lib;

init();

my $common_lisp_systems = "/usr/share/common-lisp/systems/";
my $common_lisp_sources = "/usr/share/common-lisp/source/";
my $common_lisp_bin = "/usr/lib/common-lisp/bin/";

my @interesting_files = ("fasl", "x86f", "asd", "fas");

my %formats = (
    "# FASL" => "sbcl",
    "FASL FILE output from" => "cmucl",
    '\(SYSTEM::VERSION ' => "clisp",
    "\177ELF" => "ecl");

my %version_info = (
    "sbcl" => '--noinform --noprint --eval "(progn (format t \"~A~&\"
               sb-fasl:+fasl-file-version+) (quit))"',
    "cmucl" => '-quiet -noinit -batch -eval "(progn (format t \"~A~&\"
                (c:backend-fasl-file-version c:*backend*)) (quit))"',
    "clisp" => '-x --quiet "(car (system::version))"',
    "ecl" => '-eval "(progn (format t \"~A~&\" (lisp-implementation-version))
              (quit))"'
);

sub make_substvars {
    addsubstvar(shift, "misc:Depends", "common-lisp-controller", ">= 5.11");
}

sub get_type {
    my $filename = shift;

    if ($filename =~ m/.+\.asd$/) {
	return("asd");
    } else {
	open(FILE, $filename) || error("Could not open " . $filename
				       . " for reading");
	my $line = <FILE>;
	foreach my $magic (keys %formats) {
	    if($line =~ m/^$magic/) {
		close(FILE);
		return($formats{$magic});
	    }
	}
	close(FILE);
	return("unknown");
    }
}

foreach my $package (@{$dh{DOPACKAGES}})
{
    my $tmp = tmpdir ($package);
    my %matches;

    # traverse common-lisp/source directory and record interesing
    # files)

    if ( -d $tmp) {
	find(sub {
	    foreach my $format (@interesting_files) {
		if ($File::Find::name !~ /^$tmp$common_lisp_systems.*/
		    && $File::Find::name =~ m/.+\.$format$/) {
		    push(@{$matches{get_type($_)}}, $File::Find::name);
		}
	    }
	}, $tmp);
    }

    if ($matches{"asd"} && @{$matches{"asd"}}) {
	doit("install", "-d", "$tmp/$common_lisp_systems");
	if(! $dh{NOSCRIPTS}) {
	    make_substvars($package);
	}

	# link asd system definitions approprietly and generate
	# maintainer scripts for the common-lisp-controller

	my @registered_dirs;
	foreach my $asdf (@{$matches{"asd"}}) {
	    $asdf =~ s/^($tmp$common_lisp_sources)//;

	    # only register if asd file is in the right direcotry
	    if ($1 && $asdf =~ m:^/?([^/]+)/.+\.asd$: ) {
		my $dir_to_register = $1;

		doit("ln", "-sf", "../source/" . $asdf,
		     ($tmp . $common_lisp_systems . basename($asdf)));

		if((! $dh{NOSCRIPTS}) &&
		   (!grep(/^$dir_to_register$/, @registered_dirs)))
		{
		    autoscript($package, "postinst", "postinst-clc-source",
			       "s/#SYSTEMDIR#/$dir_to_register/g;");

		    autoscript($package, "prerm", "prerm-clc-source",
			       "s/#SYSTEMDIR#/$dir_to_register/g;");

		    push(@registered_dirs, $dir_to_register);
		}
	    }
	}
    }

    # install implementation control script from
    # debian/$implementation.sh and create maintainer scripts for the
    # common-lisp-controller

    my $implementation = shift;
    if(defined $implementation) {
	if ( -f "debian/" . $implementation . ".sh" ) {
	    doit("install", "-d", $tmp . "/" . $common_lisp_bin);
	    doit("install", "debian/" . $implementation . ".sh",
		 ($tmp . $common_lisp_bin));

	    if(! $dh{NOSCRIPTS}) {
		make_substvars($package);

		autoscript($package, "postinst",
			   "postinst-clc-implementation",
			   "s/#IMPLEMENTATION#/$implementation/g;");

		autoscript($package, "prerm",
			   "prerm-clc-implementation",
			   "s/#IMPLEMENTATION#/$implementation/g;");
	    }
	} else {
	    error("Implementation script debian/" . $implementation
		  . ".sh not found: registration with Common "
		  . "Lisp Controller is not possible.");
	}
    }

    # find out fasl versions for implementations and add dependencies
    # to them

    if(! $dh{D_FLAG}) {
	foreach my $impl (keys %matches) {

	    # only add dependencies on implementation if the
	    # implementation is not the one to be installed

	    if($impl ne "unknown" && $impl ne "asd" &&
	       !($implementation && $impl eq $implementation)) {

		if ( -x "/usr/bin/" . $impl ) {
		    my $fasl_version = `$impl $version_info{$impl}`;

		    if ($impl eq "sbcl" || $impl eq "clisp") {
			addsubstvar($package, "misc:Depends",
				    $impl . "-fasl-loader-" . $fasl_version, "");
		    } elsif ($impl eq "cmucl") {
			addsubstvar($package, "misc:Depends", "$impl",
				    ">= " . sprintf("%X", $fasl_version));
			addsubstvar($package, "misc:Depends", "$impl",
				    "< ". sprintf("%X", $fasl_version + 1));
		    } elsif ($impl eq "ecl") {
			addsubstvar($package, "misc:Depends", "$impl",
				    ">= " . $fasl_version);
		    }
		} else {
		    warning("Implementation " . $impl . " seems not to be "
			    . "installed on this system to build the "
			    . "binary files included in this package. If "
			    . "this package is part of the implementation"
			    . "itself, ignore this warning.");
		}
	    }
	}
    }
}
