use 5.010000;
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
    NAME              => 'LATMOS::Accounts',
    VERSION           => '2.0.6', # finds $VERSION
    PREREQ_PM         => {
        'Net::LDAP' => undef,
        'Unicode::Map8' => undef,
        'DBD::Pg' => undef,
        'Crypt::RSA' => undef,
        'MIME::Base64' => undef,
        'Term::ReadKey' => undef,
        'Config::IniFiles' => undef,
        'Mail::Sendmail' => undef,
        'Net::DNS' => undef,
        'Template' => undef,
        'Sys::Syslog' => undef,
        'Pod::Usage' => undef,
        'Unicode::String' => undef,
        'Crypt::Cracklib' => undef,
        'Net::IP' => undef,
        'Net::IPv4Addr' => undef,
        'Net::IPv6Addr' => undef,
    }, # e.g., Module::Name => 1.1
    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
      (ABSTRACT_FROM  => 'lib/LATMOS/Accounts.pm', # retrieve abstract from module
       AUTHOR         => 'Thauvin Olivier <olivier.thauvin@latmos.ipsl.fr>') : ()),
    EXE_FILES => [ qw(
        bin/la-acls
        bin/la-qacls
        bin/la-attributes
        bin/la-cli
        bin/la-config
        bin/la-create
        bin/la-crypt-passwd
        bin/la-delete
        bin/la-edit
        bin/la-find-expired
        bin/la-group
        bin/la-guser
        bin/la-passwd
        bin/la-query
        bin/la-rename
        bin/la-rename-host
        bin/la-exchange-ip
        bin/la-rev
        bin/la-search
        bin/la-sync
        bin/la-sync-list
        bin/la-sync-manager
        bin/la-warn-expire
        bin/la-freeip
        bin/la-buildnet
        bin/la-expired-reminder
        bin/la-sql-regatt
        bin/la-sql-loadatt
        bin/la-sql-upgrade
        bin/la-sql-edit-form
        ) ],
        macro => {
            INSTALLMAN5DIR      => '$(PERLPREFIX)/share/man/man5',
            DESTINSTALLMAN5DIR  => '$(DESTDIR)$(INSTALLMAN5DIR)',
            INST_MAN5DIR        => 'blib/man5',
            DESTRPMDIR          => '$(shell pwd)',
            DATADIR             => '/usr/share/latmos-accounts',
            DESTDATADIR         => '$(DESTDIR)$(DATADIR)',
        },
        MAN1PODS        => {
            map {
                my $targ = $_;
                $targ =~ s{^man/}{};
                $targ =~ s{^bin/}{};
                $targ =~ s/\.(\d)\.pod$//;
                my $section = $1 || 1;
                ( $_ =>
                    "\$(INST_MAN${section}DIR)/$targ.$section" );
            } <man/*.pod>, (grep { ! /~$/ } <bin/*>)
        },

);

package MY;

sub postamble {
    <<EOF;
#.PHONY .= svnmanifest

bin/la-sql-upgrade: bin/la-sql-upgrade.in
\tcat \$< \| sed 's:\@DATADIR\@:\$(DATADIR):' > \$@
\tchmod +x \$@

svnmanifest:
\tsvn ls -R| grep -v "/\$\$"  > MANIFEST

ChangeLog:
\tsvn log > ChangeLog

latmos-accounts.spec: latmos-accounts.spec.in Makefile
\tsed -e 's/\@VERSION@/\$(VERSION)/' < \$< > \$@

rpm: \$(DISTVNAME).tar.gz latmos-accounts.spec
\tmkdir ./noarch || :
\trpmbuild -ba --clean\\
\t --define "_sourcedir `pwd`" \\
\t --define "_specdir `pwd`" \\
\t --define "_srcrpmdir \$(DESTRPMDIR)" \\
\t --define "_rpmdir \$(DESTRPMDIR)" \\
\t latmos-accounts.spec

svnrpm: \$(DISTVNAME).tar.gz latmos-accounts.spec
\tmkdir \$(DESTRPMDIR)/noarch || :
\trpmbuild -ba --clean\\
\t --define "_sourcedir `pwd`" \\
\t --define "_specdir `pwd`" \\
\t --define "_srcrpmdir \$(DESTRPMDIR)" \\
\t --define "_rpmdir \$(DESTRPMDIR)" \\
\t --define "svnrelease `LC_ALL=C svn info | grep '^Revision:' | sed 's/Revision: //'`" \\
\t latmos-accounts.spec

EOF
}

sub installbin {
    my $self = shift;
    my $inherited = $self->SUPER::installbin(@_);
    my $s = join '|', map quotemeta, @sbin_scripts;
    # how to create needed directories under blib
    $inherited .= $self->dir_target("\$(INST_$_)") for qw(MAN5DIR);
    $inherited;
}

sub top_targets {
    my $inherited = shift->SUPER::top_targets(@_);
    $inherited =~ s/^config ::/$& \$(INST_MAN5DIR)\$(DIRFILESEP).exists/m;
    $inherited;
}

sub install {
    my ($self) = @_;
    my $section = $self->SUPER::install();

    $section =~ s/(^install ::.*)/$1 install_config install_templates install_data/m;
    $section =~ s/\$\(INST_BIN\) \$\(DESTINSTALL(\w*)BIN\)/$& \$(INST_MAN5DIR) \$(DESTINSTALLMAN5DIR)/g;

    $section .= q[
install_config ::
	install -d $(DESTDIR)/etc
	install -d $(DESTDIR)/etc/latmos-accounts
	install sample/latmos-accounts.ini $(DESTDIR)/etc/latmos-accounts/latmos-accounts.ini
	install sample/la-sync-list.ini $(DESTDIR)/etc/latmos-accounts/la-sync-list.ini
	install sample/la-allowed-values.ini $(DESTDIR)/etc/latmos-accounts/la-allowed-values.ini
	install sample/la-sync-manager.ini $(DESTDIR)/etc/latmos-accounts/la-sync-manager.ini

install_templates ::
	install -d $(DESTDIR)$(DATADIR)/templates
	$(ABSPERLRUN) -MExtUtils::Install -e 'install({ templates => @ARGV })' \\
		$(DESTDIR)$(DATADIR)/templates

install_data ::
	install -d $(DESTDIR)$(DATADIR)
	$(ABSPERLRUN) -MExtUtils::Install -e 'install({ sqldata => @ARGV })' \\
	    $(DESTDIR)$(DATADIR)
];
  
    return $section;
}
