/*
 * tardy - a tar post-processor
 * Copyright (C) 1998-2002, 2004, 2008-2011 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 = [all] integration-build-targets;

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

integration-build-targets =
        archive/[project_minus].tar.gz
        archive/[project_minus].spec
        archive/[project_minus].README
        archive/[project_minus].lsm
        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]
{
    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
            install-sh
            /* debian/changelog */
            [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];
}

archive/Part01.gz: [source_file_order]
    set shallow
{
    parts = [glob "archive/Part*"];
    if [parts] then
            rm [parts]
                    set clearstat;
    /* assumes shar.3.49 available */
    shar -n[project] -a -c -oarchive/Part -L50
            [resolve [source_file_order]];
    gzip -9 -v archive/Part*;
}

archive/[project_minus].tar.gz:
        [source_file_order]
        [arch]/bin/tardy
        debian/control /* et al */
        aegis.conf.d/aemakegen /* for contents of crontrol file */
    set shallow
{
    if [exists archive/[project_minus].tar] then
        rm archive/[project_minus].tar
            set clearstat;
    tar cf - [resolve [source_file_order]]
        debian/changelog
        debian/compat
        debian/control
        debian/rules
        'debian/*.install'
    | [resolve [arch]/bin/tardy]
        -una Peter -gna Miller
        -unu 0 -gnu 0
        -ms 0644 -mc 07022
        -now
        --prefix\=[project_minus]
        [prepost "-rp=" "" [search_list]]
    | gzip -9
    > [target];
}

patch_files = [fromto %0% %0%,P [source_files] etc/CHANGES.[version_short]];
patch_files = [vs_file],P [stringset [patch_files] - [vs_file],P];

archive/[project_minus].patch.gz: [patch_files]
    set shallow
{
    if [exists archive/[project_minus].patch] then
        rm archive/[project_minus].patch
            set clearstat;
    sh [resolve etc/patches.sh] [resolve [patch_files]]
        > archive/[project_minus].patch;
    gzip -9 -v archive/[project_minus].patch;
}

[vs_file],P: [vs_file]
{
    echo "Index:" [vs_file] > [target];
    prev =
        [collect
            aegis -list version -p [project] -c [change]
            | [awk] -F"'\"'" "'/previous/{print $2}'"
        ];
    echo "'Prereq: \""[prev]"\"'" >> [target];
    echo "'#define PATCHLEVEL \""[prev]"\"'" |
    diff -c - [vs_file] | sed '1,2d' >> [target];
}

%0%,P: %0%
    if [exists [resolve %0%]]
{
    aegis -cp %0% -delta 1 -output patch.orig -p [project] -c [change]
        || cp /dev/null patch.orig;
    sh [resolve etc/patch.file.sh] patch.orig [resolve %0%] %0% > [target];
    rm patch.orig;
}

[changes_file]: etc/CHANGES.sh [vs_file]
{
    sh [resolve etc/CHANGES.sh] [project] > [target];
}

archive/%.html: web.src/%.roff: etc/ssp.awk [vs_file]
    set shallow
{
    roffpp [prepost "-I" "/etc" [search_list]] [resolve web.src/%.roff]
        | groff -Tascii -t -P-bhu
        | [awk] -f [resolve etc/ssp.awk]
        > [target];
}

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

[project_short].spec: etc/spec.sh
    set shallow
{
    version\=[version_short]
    sh [resolve etc/spec.sh] [source_files] > [target];
}

archive/[project_minus].spec: [project_short].spec
    set shallow
{
        cat [resolve [need]] > [target];
}

archive/[project_minus].README: README
    set shallow
{
    cat [resolve [need]] >  [target];
}

icon-num = 03;

archive/[project_short].png: archive/icon-[icon-num]-64.png
    set shallow
{
    cp [resolve [need]] [target];
}

archive/%: web.src/%.uue
{
    uudecode -o [target] [resolve [need]];
}

%0%1-%2.png: %0%1.png
{
    convert %0%1.png -scale %2 [target];
}

archive/changelog.txt: [vs_file] etc/ssp.awk
{
    aereport change-log -p tardy -c 1 -pl\=66 -pw\=80 -tw\=1
        | awk -f [resolve etc/ssp.awk]
        > [target];
}

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

    archive/[project_short].ico: archive/icon-[icon-num].ico
        set shallow
    {
        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
    {
        png2ico [target] [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];
    }
}

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

debian/control
debian/tardy.install
debian/changelog: [source_files]
    set shallow
{
    /* This actually makes more files than just debian/control */
    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-package: archive/[project_minus].tar.gz
    debian/changelog
    [arch]/bin/tardy
{
    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 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.
     */

    unset GPG_AGENT_INFO &&
    rm -rf archive/debian &&
    mkdir -p archive/debian &&
    cd archive/debian &&
    echo Options Indexes > .htaccess &&
    gunzip < ../[project_minus].tar.gz
        | ../../[arch]/bin/tardy -exclude "'"[project_minus]"/debian/*'"
        | gzip -9 > [tarball] &&
    tar xzf ../[project_minus].tar.gz &&
    cd [project_minus] &&
#if 0
    dpkg-buildpackage -sgpg [key] -sa &&
#else
    debuild [key] -sa &&
#endif
    cd .. &&
    rm -rf [project_minus] &&
    lintian -iIv --pedantic *.changes
    ;
}
