/*
 * tardy - a tar post-processor
 * Copyright (C) 1998-2002, 2004, 2008-2013 Peter Miller
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * some earlier versions of groff don't grok the -c (compatibility) option,
 * but the ones that do, also grok this environment variable.
 */
setenv GROFF_NO_SGR = -c;

version_shorter = [split "." [version_short]] _ _;
version_shorter = [unsplit "." [wordlist 1 2 [version_shorter]]];
project_short = [fromto %.[version_short] % [project]];
project_minus = [project_short]-[version_short];


/*
 * Integration builds also make the shar files for distribution.
 */
if [not [defined baseline]] then
        all += integration-build-targets;

integration-build-targets: [integration-build-targets];

integration-build-targets =
        archive/[project_short]-[version].tar.gz
        archive/[project_minus].spec
        archive/[project_short]-[version].README
        archive/[project_short].png
        [fromto web.src/%0%.roff archive/%0%.html
            [match_mask web.src/%0%.roff [source_files]]
        ]
        archive/changelog.txt
        [fromto web.src/%.uue archive/%
            [match_mask web.src/%.uue [source_files]]
        ]
        ;


Makefile.in: etc/Makefile.head etc/Makefile.sh etc/Makefile.awk [vs_file]
{
    if [quiet] then
        function print Generate [target];
    aemakegen
        --project\=[project]
        --change\=[change]
        --target\=makefile
        --output\=[target]
        ;
}

changes_file = etc/CHANGES.[version_short];

source_file_order =
    README
    [sort
        [stringset
            [changes_file]
            [source_files]
            [project_short].spec
            BUILDING
            LICENSE
            Makefile.in
            libtardy/config.h.in
            libtardy/patchlevel.h
            configure
            etc/version.so
            etc/new.so
            etc/ref-parts.so
            etc/ref-index.so
            debian/watch
            install-sh
            [fromto %0%.1 %0%.h [match_mask man/man1/%.1 [source_files]]]
            [fromto %0%.so %0%.h [match_mask man/man1/%.so [source_files]]]
        -
            README
        ]
    ];

LICENSE: man/cat1/tardy_license.1
{
    cp [resolve man/cat1/tardy_license.1] [target];
}

debian-aemakegen-files =
    debian/changelog
    debian/compat
    debian/control
    debian/rules
    debian/source/format
    debian/tardy.install
    debian/tardy-doc.install
    ;
debian-built-files =
    [debian-aemakegen-files]
    debian/copyright
    debian/watch
    ;

archive/%.tar.gz:
        [source_file_order]
        [arch]/bin/tardy
        [debian-built-files]
        aegis.conf.d/aemakegen /* for contents of control file */
    set shallow
{
    if [quiet] then
        function print Tar [target];
    tar cf - [resolve [source_file_order]]
        [debian-built-files]
    | [resolve [arch]/bin/tardy]
        -una Peter -gna Miller
        -unu 0 -gnu 0
        -ms 0644 -mc 07022
        -now
        --prefix\=[project_minus]
        [prepost "-rp=" "" [search_list]]
        -
        [target]
    ;
}


[changes_file]: etc/CHANGES.sh [vs_file]
{
    if [quiet] then
        function print Generate [target];
    sh [resolve etc/CHANGES.sh] [project] > [target];
}

archive/%.html: web.src/%.roff: [vs_file]
    set shallow
{
    if [quiet] then
        function print Groff [target];
    roffpp [prepost "-I" "/etc" [search_list]] [resolve web.src/%.roff]
        | groff -Tascii -t -P-bhu
        | cat -s
        > [target];
}

archive/[project_short].html: etc/version.so;

/* FIXME: use aemakegen instead */
[project_short].spec: etc/spec.sh
    set shallow
{
    if [quiet] then
        function print Generate [target];
    version\=[version_short]
    sh [resolve etc/spec.sh] [source_files] > [target];
}

archive/%.spec: [project_short].spec
    set shallow
{
    if [quiet] then
        function print Copy [target];
    cat [resolve [need]] > [target];
}

archive/%.README: README
    set shallow
{
    if [quiet] then
        function print Generate [target];
    cat [resolve [need]] >  [target];
}

icon-num = 03;

archive/[project_short].png: archive/icon-[icon-num]-64.png
    set shallow
{
    if [quiet] then
        function print Copy [target];
    cp [resolve [need]] [target];
}

archive/%: web.src/%.uue
{
    if [quiet] then
        function print Generate [target];
    uudecode -o [target] [resolve [need]];
}

%0%1-%2.png: %0%1.png
{
    if [quiet] then
        function print Generate [target];
    convert %0%1.png -scale %2 [target];
}

archive/changelog.txt: [vs_file]
{
    if [quiet] then
        function print Generate [target];
    aereport change-log -p tardy -c 1 -pl\=66 -pw\=80 -tw\=1
        | cat -s
        > [target];
}

if [find_command convert] then
{
    integration-build-targets += archive/[project_short].ico;

    archive/[project_short].ico: archive/icon-[icon-num].ico
        set shallow
    {
        if [quiet] then
            function print Copy [target];
        cp [resolve [need]] [target];
    }

    integration-build-targets +=
        [fromto web.src/%.png.uue archive/%.ico
            [match_mask web.src/%.png.uue [source_files]]
        ];

    %0%1.ico: %0%1-16.png %0%1-32.png %0%1-48.png %0%1-64.png %0%1-96.png
            %0%1-128.png
        set shallow
    {
        if [quiet] then
            function print Generate [target];
        convert [resolve %0%1-16.png %0%1-32.png %0%1-48.png %0%1-64.png
            %0%1-96.png %0%1-128.png] -colors 256 [target];
    }
}

tarball =  archive/[project_short]-[version].tar.gz;

check-tarball: [tarball]
{
    sh [resolve etc/check-tarball.sh]
        [resolve archive/[project_short]-[version].tar.gz];
}

[debian-aemakegen-files]: [source_files]
    set shallow
{
    if [quiet] then
        function print Generate [target];
    aemakegen -project\=[project] -change\=[change] --target\=debian
        ;
}

/* if [in [fromto %1D%2 %2 [version]] 001 002 003 004] then */
{
    integration-build-targets += debian-package;
}

debian/watch:
    set shallow
{
    if [quiet] then
        function print Generate [target];
    echo "'version=3'"
        > [target];
    echo '"http://tardy.sourceforge.net/debian/tardy_(.+).tar.gz"'
        >> [target];
}
debian-aemakegen-files =
    debian/changelog
    debian/compat
    debian/control
    debian/rules
    debian/source/format
    debian/tardy.install
    debian/tardy-doc.install
    ;
debian-built-files =
    [debian-aemakegen-files]
    debian/copyright
    debian/watch
    ;

archive/%.tar.gz:
        [source_file_order]
        [arch]/bin/tardy
        [debian-built-files]
        aegis.conf.d/aemakegen /* for contents of control file */
    set shallow
{
    if [quiet] then
        function print Tar [target];
    tar cf - [resolve [source_file_order]]
        [debian-built-files]
    | [resolve [arch]/bin/tardy]
        -una Peter -gna Miller
        -unu 0 -gnu 0
        -ms 0644 -mc 07022
        -now
        --prefix\=[project_minus]
        [prepost "-rp=" "" [search_list]]
        -
        [target]
    ;
}


[changes_file]: etc/CHANGES.sh [vs_file]
{
    if [quiet] then
        function print Generate [target];
    sh [resolve etc/CHANGES.sh] [project] > [target];
}

archive/%.html: web.src/%.roff: [vs_file]
    set shallow
{
    if [quiet] then
        function print Groff [target];
    roffpp [prepost "-I" "/etc" [search_list]] [resolve web.src/%.roff]
        | groff -Tascii -t -P-bhu
        | cat -s
        > [target];
}

archive/[project_short].html: etc/version.so;

/* FIXME: use aemakegen instead */
[project_short].spec: etc/spec.sh
    set shallow
{
    if [quiet] then
        function print Generate [target];
    version\=[version_short]
    sh [resolve etc/spec.sh] [source_files] > [target];
}

archive/%.spec: [project_short].spec
    set shallow
{
    if [quiet] then
        function print Copy [target];
    cat [resolve [need]] > [target];
}

archive/%.README: README
    set shallow
{
    if [quiet] then
        function print Generate [target];
    cat [resolve [need]] >  [target];
}

icon-num = 03;

archive/[project_short].png: archive/icon-[icon-num]-64.png
    set shallow
{
    if [quiet] then
        function print Copy [target];
    cp [resolve [need]] [target];
}

archive/%: web.src/%.uue
{
    if [quiet] then
        function print Generate [target];
    uudecode -o [target] [resolve [need]];
}

%0%1-%2.png: %0%1.png
{
    if [quiet] then
        function print Generate [target];
    convert %0%1.png -scale %2 [target];
}

archive/changelog.txt: [vs_file]
{
    if [quiet] then
        function print Generate [target];
    aereport change-log -p tardy -c 1 -pl\=66 -pw\=80 -tw\=1
        | cat -s
        > [target];
}

if [find_command convert] then
{
    integration-build-targets += archive/[project_short].ico;

    archive/[project_short].ico: archive/icon-[icon-num].ico
        set shallow
    {
        if [quiet] then
            function print Copy [target];
        cp [resolve [need]] [target];
    }

    integration-build-targets +=
        [fromto web.src/%.png.uue archive/%.ico
            [match_mask web.src/%.png.uue [source_files]]
        ];

    %0%1.ico: %0%1-16.png %0%1-32.png %0%1-48.png %0%1-64.png %0%1-96.png
            %0%1-128.png
        set shallow
    {
        if [quiet] then
            function print Generate [target];
        convert [resolve %0%1-16.png %0%1-32.png %0%1-48.png %0%1-64.png
            %0%1-96.png %0%1-128.png] -colors 256 [target];
    }
}

check-tarball: [tarball]
{
    sh [resolve [need]];
}

[debian-aemakegen-files]: [source_files]
    set shallow
{
    if [quiet] then
        function print Generate [target];
    aemakegen -project\=[project] -change\=[change] --target\=debian
        ;
}

if [in [fromto %1D%2 %2 [version]] 001 002 003 004] then
{
    integration-build-targets += debian-package;
}

debian/watch:
    set shallow
{
    if [quiet] then
        function print Generate [target];
    echo "'version=3'"
        > [target];
    echo '"http://tardy.sourceforge.net/debian/tardy_(.+).tar.gz"'
        >> [target];
}

debian/copyright: [source_files]
          set shallow
{
    function print [target];

    licensecheck "--check='[.]cc$|[.][chy]$'"  "`cat`"
    | sed "'s|^bl\\(bl\\)*/||'"
    | /usr/lib/cdbs/licensecheck2dep5
    | awk -f [resolve etc/deb-cop-fix.awk]
    > [target]
    ;
data
[unsplit "\n" [resolve [need]]]
dataend
}

debian-package: [tarball]
    debian/changelog
{
    function print Generate [target];

    local vsn = [collect head -1 [resolve debian/changelog]
                         | sed "'s|.*(\\([^()]*\\).*|\\1|'" ];
    vsn = [fromto %-1 % [vsn]];
    function print "vsn = '"[vsn]"'";
    if [not [in [count [vsn]] 1]] then
        fail "debian version not right";
    local neutered_tarball = [project_short]_[vsn].orig.tar.gz;

    local user = [collect id -un];
    local key = ;
    if [in [user] archives] then
        key = -k19CEC7D5;
    else
        key = -us -uc;

    /*
     * We fake the .orig.tar.gz file to not have a debian/ directory,
     * by filtering it out with tardy.  This silences a PITA lintian
     * warning, for no real benefit.
     */

    "set" -x &&
    unset GPG_AGENT_INFO &&
    rm -rf archive/debian &&
    mkdir -p archive/debian &&
    cd archive/debian &&
    echo Options Indexes > .htaccess &&
    tardy -exclude "'"[project_short]-[version_short]"/debian/*'"
        ../[notdir [tarball]]
        [neutered_tarball] &&
    tar xzf ../[notdir [tarball]] &&
    cd [project_short]-[version_short] &&
#if 0
    dpkg-buildpackage -sgpg [key] -sa
#else
    debuild [key] -sa
        --changes-option\=-DDistribution\=[collect
            gawk -F\= "'/CODENAME/{print $2}'" /etc/lsb-release]
#endif
    &&
    cd .. &&
    rm -rf [project_short]-[version_short] &&
    lintian -iIv --pedantic *.changes
    ;
}


/* vim: set ts=8 sw=4 et : */
