#! /bin/sh

# Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE).
#
# This file is part of Olena.
#
# Olena 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, version 2 of the License.
#
# Olena 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 Olena.  If not, see <http://www.gnu.org/licenses/>.

# To be used from milena/doc/'s source directory.

me=`basename $0`

test $# -ne 0 || { echo "usage: $me SOURCES..."; exit 1; }

cat<<EOF
## Generated by $me.  Do not edit by hand.
EOF

for source; do
  base=`basename "$source" .cc`
  source_dir=`dirname "$source"`
  timestamp="\$(srcdir)/$source_dir/$base.stamp"
  # FIXME: Programs of which sources are located in a subdirectory of
  # examples/ take the path as prefix of their name.  This is a pain.
  # It'd be much simpler to have all sources in the same directory.
  canonical_name=`echo "$source"		\
                    | sed -e 's,examples/,,'	\
                          -e 'y,/,_,'		\
                          -e 's,\.cc$,,'`
  program="$canonical_name\$(EXEEXT)"
  output="\$(srcdir)/outputs/$canonical_name.txt"

  # Programs generate a textual output, and possibly one or several
  # images (as a ``side effect'').  Use a timestamp to keep track of
  # these multiple outputs.
  #
  # Also, have outputs depend on sources rather than programs, and
  # recompile the latter to regen the former only when needed.  This
  # way, we can ship generated outputs without forcing the user of the
  # distribution to recompile the programs.
  cat<<EOF


$timestamp: $source
	@rm -f \$@.tmp
	@touch \$@.tmp
	\$(MAKE) \$(AM_MAKEFLAGS) $program
	./$program >$output.tmp
	mv $output.tmp $output
	@mv -f \$@.tmp \$@
EXTRA_DIST += $timestamp
MAINTAINERCLEANFILES += $timestamp

$output: $timestamp
## Recover from the removal of \$@
	@if test -f \$@; then :; else \\
	  rm -f \$<; \\
	  \$(MAKE) \$(AM_MAKEFLAGS) \$<; \\
	fi
EOF
done
