/*
 * aegis - project change supervisor
 * Copyright (C) 2002-2008 Peter Miller
 * Copyright (C) 2006, 2007, 2009 Walter Franzini
 *
 * 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/>.
 */

if [not [defined autoconf]] then
    autoconf = autoconf;
if [not [autoconf]] then
    autoconf = autoconf;

if [not [defined autoheader]] then
    autoheader = autoheader;
if [not [autoheader]] then
    autoheader = autoheader;

[arch]/bin/%: script/%.in: [arch]/config.status
        set shallow
        single-thread conftest.subs
{
        if [not [exists script]] then
                mkdir script
                        set clearstat;
        /* these 2 env vars control what is generated */
        CONFIG_FILES\=[target]\:[resolve script/%.in] CONFIG_HEADERS\=
                [resolve [arch]/config.status];
        chmod 755 [target];
}

/*
 * The configured files are generated using the config.status script
 * output by the configure script.  Not all of them are essential to a
 * build, or are already taken care of, and some are not meaningful.
 */
configured_files1 =
        [match_mask script/%.in [source_files]]
        ;
configured_files2 =
        [stringset
                [match_mask %0%.in [source_files]]
        -
                [configured_files1]
        ];
configured_files =
        [fromto script/%.in [arch]/bin/% [configured_files1]]
        [fromto %0%.in [arch]/%0% [configured_files2]]
        ;
configured_files =
        [stringset
                [configured_files]
        -
                [arch]/Makefile /* only for integration builds */
                [arch]/etc/Howto.conf /* make cook trigger this itself */
                [arch]/etc/config.h
                [arch]/etc/configure
        ];
configured_files: [configured_files];

configure: aegis.conf.d/site.conf etc/configure.ac
        install-sh config.guess config.sub
{
        /*
         * unfortunately,
         * install-sh must be in the current directory
         */
        if [not [exists install-sh]] then
                ln -s [resolve install-sh] install-sh
                        set clearstat;
        if [not [exists config.guess]] then
                ln -s [resolve config.guess] config.guess
                        set clearstat;
        if [not [exists config.sub]] then
                ln -s [resolve config.sub] config.sub
                        set clearstat;
        [autoconf] [resolve etc/configure.ac] > [target];
        chmod 755 [target];
}

/*
 * The install-sh script can come from a huge range of places, depending
 * on what distribution you have installed (or whether you built it from
 * the tarball).
 *
 * Setting the install-sh variable make cook print the intended error
 * message instead of a syntax error when the install-sh script does
 * not exists.
 */
install-sh = ;
loop install-sh-maybe =
    /usr/share/autoconf/install-sh
    [glob /usr/share/automake*/install-sh]
    [glob /usr/local/share/autoconf*/install-sh]
    [glob /usr/local/share/automake*/install-sh]
{
    if [exists [install-sh-maybe]] then
    {
        install-sh = [install-sh-maybe];
        loopstop;
    }
}
if [not [install-sh]] then
    fail "Can't locate the \"install-sh\" script from GNU automake";

install-sh: [install-sh]
{
        cp [need] [target];
        chmod 755 [target];
}

config.sub = ;
loop config.sub-maybe =
     /usr/share/autoconf/config.sub
     /usr/share/automake/config.sub
     [glob /usr/share/automake*/config.sub]
     [glob /usr/local/share/autoconf*/config.sub]
     [glob /usr/local/share/automake*/config.sub]
{
    if [exists [config.sub-maybe]] then
    {
        config.sub = [config.sub-maybe];
        loopstop;
    }
}
if [not [config.sub]] then
    fail "Can't locate the \"config.sub\" script from GNU autoconf";

config.sub: [config.sub]
{
    cp [need] [target];
    chmod 755 [target];
}

config.guess = ;
loop config.guess-maybe =
     /usr/share/autoconf/config.guess
     /usr/share/automake/config.guess
     [glob /usr/share/automake*/config.guess]
     [glob /usr/local/share/autoconf*/config.guess]
     [glob /usr/local/share/automake*/config.guess]
{
    if [exists [config.guess-maybe]] then
    {
        config.guess = [config.guess-maybe];
        loopstop;
    }
}
if [not [config.guess]] then
    fail "Can't locate the \"config.guess\" script from GNU autoconf";

config.guess : [config.guess]
{
    cp [need] [target];
    chmod 755 [target];
}

/*
 * Warn the user (a wannabe aegis developer) that a local-only
 * configuration file is missing, since otherwise the build will fail
 * with a misterious error message.
 *
 * This will happen only when importing for the first time the entire
 * source of Aegis using aedist.
 */
site.conf = [resolve aegis.conf.d/site.conf];
if [not [exists [site.conf]]] then
{
    echo "ERROR!" set silent;
    echo set silent;
    echo "Can\\'t locate the \"aegis.conf.d/site.conf\" configuration file."
         set silent;
    echo "You need to create the configuration file with:\n" set silent;
    echo set silent;
    echo "$ aegis -nf -config aegis.conf.d/site.conf" set silent;
    echo "$ aegis -file-attr entire-source-hide=true "
         "aegis.conf.d/site.conf" set silent;
    echo set silent;
    echo "Then you may want to edit the \"aegis.conf.d/site.conf\" "
         "to customize it for your site" set silent;
    echo set silent;
    fail;
}
[arch]/config.status: configure install-sh config.sub config.guess
                      aegis.conf.d/site.conf
{
        if [not [exists install-sh]] then
                ln -s [resolve install-sh] install-sh
                        set clearstat;

        if [not [exists config.sub]] then
                ln -s [resolve config.sub] config.sub
                        set clearstat;

        if [not [exists config.guess]] then
                ln -s [resolve config.guess] config.guess
                        set clearstat;

        local flags =;
        if [defined [arch]_c++_flags] then
            flags += CXXFLAGS\=[quote
                  [unsplit " " [filter_out "-Werror"
                                       [split "%" [[arch]_c++_flags]]]]];
        else if [defined c++_flags] then
            flags += CXXFLAGS\=[quote
                  [unsplit " " [filter_out "-Werror"
                                       [split "%" [c++_flags]]]]];

        if [defined [arch]_ld_flags] then
            flags += LDFLAGS\=[quote [unsplit " "
                                       [split "%" [[arch]_ld_flags]]]];

        else if [defined ld_flags] then
            flags += LDFLAGS\=[quote [unsplit " " [split "%" [ld_flags]]]];

        if [defined [arch]_libs] then
            flags += LIBS\=[quote [unsplit " " [split "%" [[arch]_libs]]]];
        else if [defined libs] then
            flags += LIBS\=[quote [unsplit " " [split "%" [libs]]]];

        [unsplit " "
            CXX\=[quote [unsplit " " [c++]]]
            [unsplit " " [flags]]

            [pathname [resolve configure]] --cache-file\=/dev/null --verbose
            --no-create --srcdir\=. --sysconfdir\=/etc
            [configure_args]

            && mv config.status [arch]/config.status
            && mv config.log [arch]/config.log
        ];
}

common/config.h.in: etc/configure.ac
{
        [autoheader] [resolve etc/configure.ac] > [target];
}


[arch]/%0%.h: %0%.h.in [arch]/config.status
        single-thread conftest.subs
{
        if [not [exists [dirname %0%.h]]] then
                mkdir [dirname %0%.h]
                        set clearstat;
        /* these 2 env vars control what is generated */
        CONFIG_FILES\= CONFIG_HEADERS\=[target]\:[resolve %0%.h.in]
                [resolve [arch]/config.status];
}

[arch]/%0%: %0%.in [arch]/config.status
        set ['if' [not [defined baseline]] 'then' shallow]
        single-thread conftest.subs
{
        if [not [exists [dirname %0%]]] then
                mkdir [dirname %0%]
                        set clearstat;
        /* these 2 env vars control what is generated */
        CONFIG_FILES\=[target]\:[resolve %0%.in] CONFIG_HEADERS\=
                [resolve [arch]/config.status];

        /*
         * Make shell scripts and CGI scripts executable.
         */
        if [or [matches %%.cgi %] [matches %%.sh %]] then
                chmod a+rx [target];
}
