#!/usr/bin/perl -w
#
# Registration with defoma.

use Debian::Debhelper::Dh_Lib;
init();

use FileHandle;

my $fh = new FileHandle("debian/control", "r");
my @l = $fh->getlines();
$fh->close();

my ($pkg, %dep);
foreach my $i (@l) {
    chomp($i);
    if ($i =~ /^Package:[ \t]*([^ \t]+)[ \t]*$/) {
	$pkg = $1;
	$dep{$pkg} = {};
    } elsif (defined($pkg) && $i =~ /^Depends:[ \t]*(.*)$/) {
	my @deps = split(/\,[ \t]*/, $1);
	foreach my $d (@deps) {
	    my @dd = split(' ', $d);
	    my $p = shift(@dd);
	    $dep{$pkg}->{$p} = (@dd > 0) ? join(' ', @dd) : '';
	}
	undef $pkg;
    }
}

foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
        $TMP=tmpdir($PACKAGE);
	my $defoma = pkgfile($PACKAGE, "defoma");
	my $udefoma = pkgfile($PACKAGE, "udefoma"); 
        my $hints = pkgfile($PACKAGE, "defoma-hints");
	my $version = undef;
	my $uversion = undef;

        if ( $defoma ne '' ) {
	    $version = defomaversion($defoma);
	    checkdepends($PACKAGE, $defoma, $version);
	    
            if ( ! -d "$TMP/usr/share/defoma/scripts/") {
              doit("install","-d","$TMP/usr/share/defoma/scripts/");
            }
            doit("install","-m0644","$defoma","$TMP/usr/share/defoma/scripts/$PACKAGE.defoma");
        }
	if ( $udefoma ne '' ) {
	    $uversion = defomaversion($udefoma);
	    checkdepends($PACKAGE, $udefoma, $uversion);
	    
            if ( ! -d "$TMP/usr/share/defoma/scripts/") {
              doit("install","-d","$TMP/usr/share/defoma/scripts/");
            }
            doit("install","-m0644","$udefoma","$TMP/usr/share/defoma/scripts/$PACKAGE.udefoma");
        }

        if ( $hints ne '' ) {
	    checkdepends($PACKAGE, undef, undef);
            if ( ! -d "$TMP/etc/defoma/hints/") {
              doit("install","-d","$TMP/etc/defoma/hints/");
            }
            doit("install","-m0644","$hints","$TMP/etc/defoma/hints/$PACKAGE.hints");
        }
        

	if ($defoma) {
	    if (! $dh{NOSCRIPTS}) {
		autoscript($PACKAGE,"postinst","postinst-defoma",
			   "s/#PACKAGE#/$PACKAGE/;");
		autoscript($PACKAGE,"prerm","prerm-defoma",
			   "s/#PACKAGE#/$PACKAGE/;");
		autoscript($PACKAGE,"postrm","postrm-defoma",
			   "s/#PACKAGE#/$PACKAGE/;");
	    }
	}

	if ($hints) {
	    if (! $dh{NOSCRIPTS}) {
		autoscript($PACKAGE,"postinst","postinst-defoma-hints",
			   "s/#PACKAGE#/$PACKAGE/;");

            	autoscript($PACKAGE,"prerm","prerm-defoma-hints",
			   "s/#PACKAGE#/$PACKAGE/;");
	    }
	}

}

sub defomaversion {
    my $file = shift;
    my $l = `/bin/grep '^\$APPINFO->{require}[ \t]*=[ \t]*' $file`;
    if ($l) {
	chomp($l);
	$l =~ s/^.*=[ \t]*//;
	$l =~ s/[ \t]*;[ \t]*$//;
	$l =~ /^[^0-9.]*([0-9.]+)[^0-9.]*$/;
	return $1;
    }
    return undef;
}

sub checkdepends {
    my $pkg = shift;
    my $file = shift;
    my $version = shift;

    if (exists($dep{$pkg}) && exists($dep{$pkg}->{defoma})) {
	return unless (defined($version));
	if ($dep{$pkg}->{defoma} ne "(>= $version)") {
	    warning("Different version dependency against defoma between ".
		    "$file and debian/control.");
	    return;
	}
	
    }

    my $str = "defoma";
    $str .= " (>= $version)" if (defined($version));
    
    error("Please add Depends: $str for $pkg.");
}
