use inc::Module::Install;

name 'LATMOS-Accounts-Web';
all_from 'lib/LATMOS/Accounts/Web.pm';

#requires 'Catalyst' => '5.7015';
requires 'Catalyst::Plugin::ConfigLoader';
requires 'Catalyst::Plugin::Static::Simple';
requires 'Catalyst::Action::RenderView';
requires 'YAML'; # This should reflect the config file format you've chosen
                 # See Catalyst::Plugin::ConfigLoader for supported formats
requires 'Catalyst::Plugin::Authentication' => '0';
requires 'Catalyst::Plugin::Session' => '0';
requires 'Catalyst::Plugin::Session::Store::File' => '0';
requires 'Catalyst::Plugin::Session::State::Cookie' => '0';
requires 'Catalyst::Plugin::Prototype' => '0';
requires 'Catalyst::Plugin::I18N' => '0';
requires 'Catalyst::View::TT' => '0';
requires 'LATMOS::Accounts' => '0';
requires 'Email::Send';
requires 'Email::MIME::Creator';
catalyst;

install_script glob('script/*.pl');
#auto_install;
WriteMakefile(
    macro => { 
        DESTRPMDIR => '$(shell pwd)',
    },
);

package MY;

sub postamble {
    my @po = map { m:lib/LATMOS/Accounts/Web/I18N/([^\.]+)\.po$:; $1 }
            glob('lib/LATMOS/Accounts/Web/I18N/*.po');

    my $makepo = join("\n\n", map {
        "
lib/LATMOS/Accounts/Web/I18N/$_.po: lib/LATMOS/Accounts/Web/I18N/messages.pot
\t[ -f lib/LATMOS/Accounts/Web/I18N/$_.po ] || msginit --no-translator --input=lib/LATMOS/Accounts/Web/I18N/messages.pot --output= --locale=$_
\tmsgmerge -U lib/LATMOS/Accounts/Web/I18N/$_.po lib/LATMOS/Accounts/Web/I18N/messages.pot

        "
        } @po
    );

    $makepo .= "\n\npo: " . join(' ', map { "lib/LATMOS/Accounts/Web/I18N/$_.po" } @po) . "\n";

    <<EOF;
.PHONY: lib/LATMOS/Accounts/Web/I18N/messages.pot

pot: lib/LATMOS/Accounts/Web/I18N/messages.pot

lib/LATMOS/Accounts/Web/I18N/messages.pot:
\tcat MANIFEST | egrep '^(lib|root/html)' | xargs xgettext.pl -o lib/LATMOS/Accounts/Web/I18N/messages.pot

$makepo

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

ChangeLog:
\tsvn log > ChangeLog

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

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

svnrpm: \$(DISTVNAME).tar.gz latmos-accounts-web.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-web.spec

EOF
}

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

    $section =~ s/(^install ::.*)/$1 install_config/m;

    $section .= q[
install_config ::
	install -d $(DESTDIR)/etc
	install -d $(DESTDIR)/etc/latmos-accounts
	install etc/latmos-accounts-web.yml $(DESTDIR)/etc/latmos-accounts/latmos-accounts-web.yml
];

    return $section;
}

