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.

my @po;
foreach (glob('po/*.po')) {
    my ($po) = $_ =~  m:([^/\.]+)\.po$:;
    push (@po, $po);
}

WriteMakefile(
    NAME              => 'LATMOS::Accounts',
    VERSION_FROM      => 'lib/LATMOS/Accounts.pm',
    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,
        'Crypt::Blowfish' => undef,
        'Net::IP' => undef,
        'Net::IPv4Addr' => undef,
        'Net::IPv6Addr' => undef,
        'DateTime' => undef,
        'SOAP::Lite' => undef,
        'HTTP::Cookies' => undef,
        'XML::XPath' => undef,
        'Crypt::SmbHash' => undef,
        'Date::Calc' => undef,
        'Spreadsheet::WriteExcel' => undef,
        'Text::CSV' => 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-sql-crypt-passwd
        bin/la-delete
        bin/la-edit
        bin/la-sql-find-expired
        bin/la-group
        bin/la-guser
        bin/la-passwd
        bin/la-gen-passwd
        bin/la-query
        bin/la-rename
        bin/la-sql-rename-host
        bin/la-sql-exchange-ip
        bin/la-sql-exchange-hostname
        bin/la-sql-switch-cname
        bin/la-sql-rev
        bin/la-search
        bin/la-sync
        bin/la-sync-manager
        bin/la-sync-ctl
        bin/la-warn-expire
        bin/la-sql-freeip
        bin/la-expired-reminder
        bin/la-sql-upgrade
        bin/la-sql-recompute
        bin/la-sql-edit-form
        bin/la-sql-updsshfp
        bin/la-test-mail
        bin/la-sql-log
        bin/la-sql-runstat
        bin/la-sql-properties
        bin/la-sql-alias
        bin/la-sql-stat2csv
        bin/la2xls
        bin/la-ban-passwd
        bin/la-renewEmp
        bin/la-load-csv
        ) ],
        macro => {
            INSTALLMAN5DIR      => '$(PERLPREFIX)/share/man/man5',
            INSTALLMAN8DIR      => '$(PERLPREFIX)/share/man/man8',
            DESTINSTALLMAN5DIR  => '$(DESTDIR)$(INSTALLMAN5DIR)',
            DESTINSTALLMAN8DIR  => '$(DESTDIR)$(INSTALLMAN8DIR)',
            LOCALEDIR           => '/usr/share/locale',
            INST_MAN5DIR        => 'blib/man5',
            INST_MAN8DIR        => 'blib/man8',
            DESTRPMDIR          => '$(shell pwd)',
            DATADIR             => '/usr/share/latmos-accounts',
            DESTDATADIR         => '$(DESTDIR)$(DATADIR)',
            MAN1PODSBIN         => join(' ', <bin/*>),
            MAN5PODS            => join(' ', <man/man5/*.pod>),
            MAN8PODS            => join(' ', <man/man8/*.pod>),
        },
        MAN1PODS        => {
            map {
                my $targ = $_;
                $targ =~ s{^man/man(\d)}{};
                my $section = $1 || 1;
                $targ =~ s{^bin/}{};
                $targ =~ s/.pod$//;
                ( $_ =>
                    "\$(INST_MAN${section}DIR)/$targ.$section" );
            } <man/man?/*.pod>, (grep { ! /~$/ } <bin/*>)
        },
);

package MY;

sub postamble {

    my $makepo = '';

    foreach my $po (@po) {
        $makepo .=
"
po/$po.po: po/messages.pot
\t[ -f po/$po.po ] || msginit --no-translator --input=po/messages.pot --output= --locale=$_
\tmsgmerge -U po/$po.po po/messages.pot

po/$po/LC_MESSAGES/linkaccounts.mo: po/$po.po
\tinstall -d po/$po/LC_MESSAGES
\tmsgfmt -o \$@ \$<
\n"
    }

    $makepo .= "\n\npo: " . join(' ', map { "po/$_.po" } @po) . "\n";
    $makepo .= "\n\nmo: " . join(' ', map { "po/$_/LC_MESSAGES/linkaccounts.mo" } @po) . "\n";

    <<EOF;
.PHONY: po/messages.pot

pot: po/messages.pot

po/messages.pot:
\tcat MANIFEST | egrep '^(lib|bin)' | xargs xgettext.pl -o po/messages.pot

$makepo

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

podhtml: \$(MAN5PODS) \$(MAN8PODS) \$(MAN3PODS) \$(MAN1PODSBIN)
\tmkdir html || :
\trm -fr html/*
\tperl scripts/pod2xhtml.pl man1 \$(MAN1PODSBIN)
\tperl scripts/pod2xhtml.pl man3 \$(MAN3PODS)
\tperl scripts/pod2xhtml.pl man5 \$(MAN5PODS)
\tperl scripts/pod2xhtml.pl man8 \$(MAN8PODS)

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 MAN8DIR);
    $inherited;
}

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

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

    $section =~ s/(^install ::.*)/$1 install_config install_templates install_data install_mo/m;
    $section =~ s/\$\(INST_BIN\) \$\(DESTINSTALL(\w*)BIN\)/$& \$(INST_MAN5DIR) \$(DESTINSTALLMAN5DIR) \$(INST_MAN8DIR) \$(DESTINSTALLMAN8DIR)/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)

install_mo: mo
	install -d $(DESTDIR)$(LOCALEDIR)
];

    foreach (@po) {
        $section .= "\tinstall -d \$(DESTDIR)\$(LOCALEDIR)/$_/LC_MESSAGES\n";
        $section .= "\tinstall po/$_/LC_MESSAGES/linkaccounts.mo \$(DESTDIR)\$(LOCALEDIR)/$_/LC_MESSAGES/linkaccounts.mo\n";
    }
    $section .= "\n";
  
    return $section;
}

sub test {
    my $inherited = shift->SUPER::test(@_);

    $inherited =~ s:^(test_dynamic .*):$1 test_scripts:m;

    $inherited .= <<EOF;

TESTLIVE_FILES = live-test/*.t
TESTSCRIPTS_FILE = t/scripts.pl

test_live :: pure_all
\tPERL_DL_NONLAZY=1 \$(FULLPERLRUN) "-MExtUtils::Command::MM" "-e" "test_harness(\$(TEST_VERBOSE), '\$(INST_LIB)', '\$(INST_ARCHLIB)')" \$(TESTLIVE_FILES)

test_scripts :: pure_all
\tPERL_DL_NONLAZY=1 \$(FULLPERLRUN) "-I\$(INST_LIB)" "-I\$(INST_ARCHLIB)" \$(TESTSCRIPTS_FILE) \$(EXE_FILES)
EOF
    return $inherited;
}
