#!/usr/bin/perl -w

=head1 NAME

dh_monoaot - generates AOT images for assemblies

=cut

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

=head1 SYNOPSIS

B<dh_monoaot> [S<I<debhelper options>>] [B<-n>]

=head1 DESCRIPTION

dh_monoaot is a debhelper program that is responsible for
generating AOT images at package install time.

It also automatically generates the postinst and prerm commands needed
to generate AOT images. See L<dh_installdeb(1)> for an
explanation of how this works.

This is based on L<dh_installcligac(1)> in the cli-common package.

=head1 OPTIONS

=over 4

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

Do not modify postinst/prerm scripts.

=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.

=cut

init();

foreach my $package (@{$dh{DOPACKAGES}}) {
  my $tmp = tmpdir($package);
  my $monoaot = pkgfile($package, "installmonoaot");

  if ($monoaot ne '') {
    # sanity check: do all files listed in the installcligac file exist?
    open MONOAOT, "<$monoaot" or
      die "E: Can't open $monoaot\n";
    while (<MONOAOT>)
    {
      chomp;
      if (! -f "$tmp$_") {
        die "E: Can't find file $tmp$_!\n";
      }
    }
    close MONOAOT;

    if (! $dh{NOSCRIPTS}) {
    open MONOAOT, "<$monoaot" or
      die "E: Can't open $monoaot\n";
    while (<MONOAOT>)
    {
      chomp;
      foreach my $file ($_) {
        autoscript($package, "postinst", "postinst-monoaot",
                   "s!#FILE#!$file!g");
        autoscript($package, "prerm", "prerm-monoaot",
                   "s!#FILE#!$file.so!");
      }
    }
    close MONOAOT;

    }

  }
  
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of cli-common-dev.

=head1 AUTHOR

Mirco 'meebey' Bauer <meebey@meebey.net>
Jo Shields <jo.shields@xamarin.com>

=cut