/*
 * fhist - file history and comparison tools
 * Copyright (C) 1998, 2001, 2004, 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/>.
 */

source_files +=
    etc/version.so
    [fromto
        lib/%1/readme/main.%2 lib/%1/readme/new.so
        [match_mask lib/%1/readme/main.%2 [source_files]]
    ]
    [fromto
        lib/%1/reference/main.%2 lib/%1/reference/ref-index.so
        [match_mask lib/%1/reference/main.%2 [source_files]]
    ]
    [fromto
        lib/%1/reference/main.%2 lib/%1/reference/ref-parts.so
        [match_mask lib/%1/reference/main.%2 [source_files]]
    ];

etc/version.so:
    set ['if' [or [not [defined baseline] ] [not [exists bl/etc/version.so
        ] ] ] 'then' shallow]
{
    function if_quiet Generate;
    echo \".ds V) [version]\" > [target];
    echo \".ds v) [version_short]\" >> etc/version.so;
    aegis -list version -p [project] -c [change]
        | gawk -F'\'"\'' "'/previous/{print $2}'"
        | gawk -F. "'{print \".ds w) \"$1\".\"$2}'"
        >> [target];
    echo \".ds Y) [copyright_years]\" >> [target];
}

lib/%/readme/new.so: : etc/so.sh
    [match_mask lib/%/readme/new.%%.so [source_files]]
    set ['if' [not [defined baseline]] 'then' shallow]
{
    function if_quiet Generate;
    sh [resolve etc/so.sh] -r
        [match_mask lib/%/readme/new.%%.so [source_files]]
        > [target];
}


/* Sigh.  Must be before the next recipe, and before the generic .d's. */
cascade lib/en/reference/main.man =
    lib/en/reference/ref-index.so lib/en/reference/ref-parts.so;

lib/%/reference/ref-index.so: [match_mask lib/%/man%%1/%%2.%%1 [source_files]]
        etc/ptx1.awk etc/ptx.ignore etc/ptx2.awk
    set no-cascade
{
    function if_quiet Generate;
    [awk] -f [resolve etc/ptx1.awk]
        [resolve [match_mask lib/%/man%%1/%%2.%%1 [source_files]]]
    | ptx -O -r -w1000 -i[resolve etc/ptx.ignore]
    | sort -t'\'"\'' -f -d +5 -6 +3 -4
    | [awk] -F'\'"\'' -f [resolve etc/ptx2.awk]
    > [target];
}

lib/%/reference/ref-parts.so: [match_mask lib/%/man%%1/%%2.%%1 [source_files]]
        etc/ptx.sh
    set no-cascade
{
    function if_quiet Generate;
    sh -f [resolve etc/ptx.sh]
        [match_mask lib/%/man%%1/%%2.%%1 [source_files]]
    > [target];
}


/*
 * formatting documents and manual entries
 */

doc.targets: cat special;

/*
 * manual entries
 */
cat =
    [fromto lib/%3/man%1/%2.%1 lib/%3/cat%1/%2.%1
        [match_mask lib/%3/man%1/%2.%1 [source_files]]
    ];
cat: [cat];

lib/%3/cat%1/%2.%1: lib/%3/man%1/%2.%1
    set mkdir unlink
{
    function if_quiet Groff;
    roffpp [cc_include_flags] [resolve lib/%3/man%1/%2.%1]
    | groff -Tascii -P-ch -t -man > [target];
}

lib/%3/man%1/%2.%1.d: lib/%3/man%1/%2.%1
    set nocascade
{
    function if_quiet Depends;
    c_incl -nc -ns -eia -r -nrec
        [cc_include_flags]
        -prefix "'cascade lib/%3/man%1/%2.%1 ='"
        -suffix "';'" [resolve lib/%3/man%1/%2.%1]
        [prepost "-rlp=" "" [search_list]]
        -o [target];
}

%0%.so.d: %0%.so
    set nocascade
{
    function if_quiet Depends;
    c_incl -nc -ns -eia -r -nrec
        [cc_include_flags]
        -prefix "'cascade %0%.so ='"
        -suffix "';'"
        [resolve %0%.so]
        [prepost "-rlp=" "" [search_list]]
        -o [target];
}

man-dep-files =
    [addsuffix .d [match_mask lib/%3/man%1/%2.%1 [source_files]]]
    [addsuffix .d [match_mask %0%.so [source_files]]]
    ;

#include-cooked-nowarn [man-dep-files]


/*
 * specials
 */

special =
    [fromto lib/%1/%2/main.man lib/%1/%2.pdf
        [match_mask lib/%1/%2/main.man [source_files]]
    ];
special: [special];

lib/%1/%2.pdf: lib/%1/%2/main.man
{
    function if_quiet Groff;
    roffpp
        [cc_include_flags]
        [resolve lib/%1/%2/main.man]
        > lib/%1/%2.groff;
    groff -t -p -man lib/%1/%2.groff > lib/%1/%2.ps;
    rm lib/%1/%2.groff
        set silent;
    ps2pdf lib/%1/%2.ps [target];
    rm lib/%1/%2.ps
        set silent;
}

lib/%1/%2.txt: lib/%1/%2/main.man
{
    function if_quiet Groff;
    roffpp
        [cc_include_flags]
        [resolve lib/%1/%2/main.man]
        > lib/%1/%2.groff;
    groff -Tascii -P-bchu -t -p -man lib/%1/%2.groff > [target];
    rm lib/%1/%2.groff
        set silent;
}

lib/%1/%2/main.man.d: lib/%1/%2/main.man
    set nocascade
{
    function if_quiet Depends;
    c_incl -nc -ns -eia -r -nrec
        [cc_include_flags]
        -prefix "'cascade lib/%1/%2/main.man ='"
        -suffix "';'"
        [resolve lib/%1/%2/main.man]
        [prepost "-rlp=" "" [search_list]]
        -o [target];
}

etc-dep-files =
    [addsuffix .d [match_mask %0%.man [source_files]]]
    ;

#include-cooked-nowarn [etc-dep-files]

README: lib/en/readme.txt
{
    function if_quiet Copy;
    cat -s [resolve lib/en/readme.txt] > [target];
}

BUILDING: lib/en/building.txt
{
    function if_quiet Copy;
    cp [resolve [need]] [target];
}

etc/%.txt: etc/%.man
{
    function if_quiet Groff;
    roffpp [cc_include_flags] [resolve etc/%.man]
        > etc/%.groff;
    groff -Tascii -P-bcu -t -p -man etc/%.groff
        | cat -s
        > [target];
    rm etc/%.groff
        set silent;
}

etc/%.man.d: etc/%.man
    set nocascade
{
    function if_quiet Depends;
    c_incl -nc -ns -eia -r -nrec
        [cc_include_flags]
        -prefix "'cascade etc/%.man ='"
        -suffix "';'"
        [resolve etc/%.man]
        [prepost "-rlp=" "" [search_list]]
        -o [target];
}

all += etc/release.txt;


special += etc/freecode.txt;

etc/%.txt: etc/%.roff: etc/version.so etc/groff.sh
    set shallow
{
    if [quiet] then
        function print GRO [target];
    sh [resolve etc/groff.sh] -Tascii -t -P-bcu
        [addprefix "-I" [search_list]]
        [resolve etc/%.roff]
    | cat -s
    > [target];
}

/*
 * Catch 22: you need to build these includes, in order to discover that
 * you need to build these includes.  Sigh.
 */
cascade lib/en/man1/z_cr.so = etc/version.so;
cascade lib/en/readme/main.man = lib/en/readme/new.so;
cascade etc/release.man = etc/version.so;


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