/*
 *      fhist - file history and comparison tools
 *      Copyright (C) 1998, 2001, 2003, 2005, 2006, 2008-2010, 2012 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/>.
 */

integration-build-targets =
    archive/[project_short]-[version].README
    archive/[project_minus].lsm
    archive/[project_short]-[version].pdf
    archive/[project_minus].spec
    archive/[project_short]-[version].tar.gz
    archive/[project_short].png
    archive/index.html
    archive/.htaccess
    archive/ChangeLog
    ;

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

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


source_file_order =
    README
    [sort
        [stringset
            [source_files]
            BUILDING
            LICENSE

            /*
             * These two are for time stamping the distribution,
             * and cranking on the version.
             */
            common/patchlevel.h

            /*
             * These four are for building the distribution.
             */
            Makefile.in
            common/config.h.in
            install-sh
            configure

            /*
             * This is for building the RPM package
             */
            [project_minus].spec
        -
            README
        ]
    ]
    ;


/*
 * These recipes build a Makefile for a user.  They do not build a
 * Makefile useful for a development build, because no arch
 * information is included in the Makefile.
 */

Makefile.in: [source_files]
{
    function if_quiet Generate;
    aemakegen -p [project] -c [change]
    > [target];
}

archive/%.tar.gz: [source_file_order]
        debian/control /* et al */
    set shallow
{
    function if_quiet Tar;
    tar cf - [resolve [source_file_order]]
        debian/changelog
        debian/compat
        debian/control
        debian/rules
        debian/fhist.install
    | tardy
        -una Peter -gna Miller
        -unu 0 -gnu 0
        -ms 0644 -mc 07022
        -now
        -prefix\=[project_minus]
        [prepost "-rp=" "" [search_list]]
        -
        [target]
    ;
}

archive/[project_short]-[version].%: %
    set shallow
{
    function if_quiet Copy;
    cat [resolve [need]] > [target];
}

archive/[project_minus].%: %
    set shallow
{
    function if_quiet Copy;
    cat [resolve [need]] > [target];
}

#include-cooked [addsuffix ".d" [match_mask etc/%.man [source_files]]]


/*
 * This is not in the integration-build-targets because it takes far
 * too long to do.
 */

RPM: archive/[project_minus]-1.i386.rpm;

archive/[project_minus]-1.i386.rpm archive/[project_minus]-1.src.rpm:
        archive/[project_minus].tar.gz etc/rpm-build.sh
    set shallow
{
    function if_quiet Generate;
    sh [resolve etc/rpm-build.sh]
        RPM-temporary
        [resolve archive/[project_short]-[version].tar.gz]
        ;
    mv RPM-temporary/RPMS/i386/[project_minus]-1.i386.rpm
        archive/[project_minus]-1.i386.rpm
        ;
    mv RPM-temporary/SRPMS/[project_minus]-1.src.rpm
        archive/[project_minus]-1.src.rpm
        ;
    rm -r RPM-temporary;
}

[project_minus].spec: etc/spec.sh [source_files]
{
    function if_quiet Generate;
    version\=[version_short]
    sh [resolve etc/spec.sh] [source_files]
        > [target];
}

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

archive/%: etc/%.uue
    set shallow
{
    function if_quiet Uudecode;
    uudecode -o [target] [resolve [need]];
}


archive/%.html: etc/%.html
    set shallow
{
    function if_quiet Groff;
    roffpp [cc_include_flags] [resolve etc/%.html]
    | groff -Tascii -t -P-hub -man
    | cat -s
    > [target];
}


etc/[project_short].lsm.d: etc/[project_short].lsm
    set no-cascade
{
    function if_quiet Depends;
    c_incl -nc -ns -eia -r -nrec
        [cc_include_flags]
        -prefix "'cascade etc/"[project_short]".lsm ='"
        -suffix "';'"
        [resolve etc/[project_short].lsm]
        [prepost "-rlp=" "" [search_list]] /* remove leading path */
        -slp [arch] "'[arch]'" /* substitute leading path */
        -o [target];
}
#include-cooked-nowarn etc/[project_short].lsm.d

archive/[project_minus].lsm: etc/[project_short].lsm
    set shallow
{
    function if_quiet Generate;
    roffpp
        [cc_include_flags]
        [resolve etc/[project_short].lsm]
    | groff -Tascii -t -P-hub -man
    | cat -s
    > [target];
}

LICENSE: lib/en/cat1/fhist_lic.1
{
    function if_quiet Copy;
    cp [resolve [need]] [target];
}

archive/%.pdf: lib/en/reference.pdf
    set shallow
{
    function if_quiet Copy;
    cp [resolve [need]] [target];
}

archive/.htaccess: etc/htaccess
    set shallow
{
    function if_quiet Generate;
    sed '/^#/d' < [resolve etc/htaccess] > [target];
}

archive/ChangeLog: [source_files]
{
    function if_quiet Generate;
    aereport change_log -p fhist -c 1
        -o [target] -pw\=80 -pl\=66;
}

/*
 * Catch 22:  You need to build these to be able to automatically discover
 * that you need to build them.  Sigh.
 */
cascade etc/index.html = etc/version.so;
cascade etc/fhist.lsm = etc/version.so;


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

archive/%1-%2.png: archive/%1.png
    set shallow
{
    function if_quiet Generate;
    convert [resolve archive/%1.png] -scale %2 [target];
}

archive/%.ico: archive/%-16.png archive/%-32.png archive/%-64.png
        archive/%-128.png
    set shallow
{
    function if_quiet Generate;
    convert [resolve [need]] -colors 256 [target];
}


/*
 * if [or
 *     [in [fromto %1D%2 %2 [version]] 001 002 003 004]
 *     [collect "set +e; on_ac_power; expr 1 - $?; exit 0" ]
 * ] then
 */
if [in [fromto %1D%2 %2 [version]] 001 002 003 004] then
{
    integration-build-targets += debian-package;
}

debian-package: archive/[project_short]-[version].tar.gz
    debian/changelog
{
    function if_quiet "";
    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 &&
    tardy -exclude "'"[project_minus]"/debian/*'"
        ../[project_short]-[version].tar.gz
        [tarball] &&
    tar xzf ../[project_short]-[version].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
    ;
}


debian/control
debian/srecord.install
debian/changelog: [source_files]
    set shallow
{
    function if_quiet Generate;
    /* This actually makes more files than just debian/control */
    aemakegen -project\=[project] -change\=[change] --target\=debian ;
}

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