#! /bin/sh

if test "x$1" = "x--help" || test "x$1" = "x-h"; then
    cat <<EOF
Usage: fftw-wisdom-to-conf [OPTIONS] [< INPUT] [> OUTPUT]
Convert wisdom (stdin) to C configuration routine (stdout).

Options:
        -h, --help: print this help
     -V, --version: print version/copyright info
EOF
    exit 0
fi

if test "x$1" = "x--version" || test "x$1" = "x-V"; then
    cat <<EOF
fftw-wisdom-to-conf from FFTW version @VERSION@

Copyright (c) 2003, 2006 Matteo Frigo
Copyright (c) 2003, 2006 Massachusetts Institute of Technology

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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    
EOF
    exit 0
fi

read preamble fftw_wisdom

case "$preamble $fftw_wisdom" in
	\(@PACKAGE@-@VERSION@\ *_wisdom)
		prefix=`echo $fftw_wisdom | cut -d_ -f1`_
		;;
	*)
		echo "fftw-wisdom-to-conf: invalid wisdom" 1>&2
		exit 1
		;;
esac

cat <<EOF
/* Automatically generated by fftw-wisdom-to-conf from @PACKAGE@ @VERSION@.
   DO NOT EDIT!  (Unless you really, really want to.  Then it's okay.) */
void ${prefix}configure_planner(void *plnr)
{
    struct solvtab_s { void (*reg)(void *); const char *reg_nam; };
    extern void ${prefix}solvtab_exec(const struct solvtab_s s[], void *);

#define DECLARE(name) extern void name(void *);
#define STRINGIZEx(x) #x
#define STRINGIZE(x) STRINGIZEx(x)
#define SOLVTAB(s) { s, STRINGIZE(s) },
#define DO(X) \\
EOF

sed 's/ *(//' | cut -d" " -f1 | grep -v -- - | egrep -v '^ *\)*$' | sort | uniq | while read reg_nam; do
    echo "    X($reg_nam)\\"
done

cat <<EOF
    /* end DO(X) */

    DO(DECLARE)

    const struct solvtab_s s[] = {
        DO(SOLVTAB)
        { 0, 0 }
    };

    ${prefix}solvtab_exec(s, plnr);
}
EOF
