#! /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 Matteo Frigo
Copyright (c) 2003 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-wisdom2c: 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 *);
EOF

tmp=/tmp/fftw-wisdom-to-conf$$
sed 's/ *(//' | cut -d" " -f1 | grep -v -- - | egrep -v '^ *\)*$' > $tmp

cat $tmp | sort | uniq | while read reg_nam; do
    echo "    extern void $reg_nam(void *);"
done

echo "    const struct solvtab_s s[] = {" 
cat $tmp | sort | uniq | while read reg_nam; do
    echo "        { $reg_nam, \"$reg_nam\" },"
done
echo "        { 0, 0 }"
echo "    };"

echo "    ${prefix}solvtab_exec(s, plnr);"
echo "}"
