#!/bin/sh

# libctl: flexible Guile-based control files for scientific software 
# Copyright (C) 1998, 1999, 2000, 2001, 2002, Steven G. Johnson
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA  02111-1307, USA.
#
# Steven G. Johnson can be contacted at stevenj@alum.mit.edu.

output_dir="`dirname $1`"

if test "$#" = "2"; then
    libctl_dir="$2"
else
    libctl_dir="@prefix@/share/libctl"
fi
if test ! -r $libctl_dir/utils/ctl-io.scm; then
    echo "couldn't find $libctl_dir/utils/ctl-io.scm"
    exit 1
fi

rm -f $output_dir/ctl-io.c $output_dir/ctl-io.h

cat > $output_dir/ctl-io.h <<EOF
/* THIS FILE WAS AUTOMATICALLY GENERATED.  DO NOT MODIFY! */
/* generated from the file: $1 */

#ifndef CTL_IO_H
#define CTL_IO_H

#include <guile/gh.h>
#include <ctl.h>

#ifdef __cplusplus
extern "C" {
#endif                          /* __cplusplus */

EOF

cat > $output_dir/ctl-io.c <<EOF
/* THIS FILE WAS AUTOMATICALLY GENERATED.  DO NOT MODIFY! */
/* generated from the file: $1 */

#include <stdlib.h>
#include <stdio.h>
#include <ctl-io.h>

EOF

guile -l $libctl_dir/base/include.scm \
      -c "(include "'"'"$libctl_dir/base/ctl.scm"'"'") (include "'"'"$libctl_dir/utils/ctl-io.scm"'"'") (include "'"'"$1"'"'") (output-header)" >> $output_dir/ctl-io.h

guile -l $libctl_dir/base/include.scm \
      -c "(include "'"'"$libctl_dir/base/ctl.scm"'"'") (include "'"'"$libctl_dir/utils/ctl-io.scm"'"'") (include "'"'"$1"'"'") (output-source)" >> $output_dir/ctl-io.c

cat >> $output_dir/ctl-io.h <<EOF

#ifdef __cplusplus
}                               /* extern "C" */
#endif                          /* __cplusplus */

#endif                          /* CTL_IO_H */

EOF

@INDENT@ $output_dir/ctl-io.c > /dev/null 2>&1
@INDENT@ $output_dir/ctl-io.h > /dev/null 2>&1
rm -f $output_dir/ctl-io.c~ $output_dir/ctl-io.h~
rm -f $output_dir/ctl-io.c.BAK $output_dir/ctl-io.h.BAK

