#
# Copyright (c) 2010 LAAS/CNRS
# All rights reserved.
#
# Redistribution  and  use  in  source  and binary  forms,  with  or  without
# modification, are permitted provided that the following conditions are met:
#
#   1. Redistributions of  source  code must retain the  above copyright
#      notice and this list of conditions.
#   2. Redistributions in binary form must reproduce the above copyright
#      notice and  this list of  conditions in the  documentation and/or
#      other materials provided with the distribution.
#
# THE SOFTWARE  IS PROVIDED "AS IS"  AND THE AUTHOR  DISCLAIMS ALL WARRANTIES
# WITH  REGARD   TO  THIS  SOFTWARE  INCLUDING  ALL   IMPLIED  WARRANTIES  OF
# MERCHANTABILITY AND  FITNESS.  IN NO EVENT  SHALL THE AUTHOR  BE LIABLE FOR
# ANY  SPECIAL, DIRECT,  INDIRECT, OR  CONSEQUENTIAL DAMAGES  OR  ANY DAMAGES
# WHATSOEVER  RESULTING FROM  LOSS OF  USE, DATA  OR PROFITS,  WHETHER  IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR  OTHER TORTIOUS ACTION, ARISING OUT OF OR
# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#                                           Anthony Mallet on Tue Jun  8 2010
#

# Example template which illustrates all the procedures of the genom3 Tcl
# generator engine.

template usage {*}{
    "\n"
    "Example template which illustrates the genom3 Tcl generator engine\n"
    "\n"
    "This template generates a simple C program by using all the procedures\n"
    "available in the GenoM3 Tcl generator engine. It is mostly interesting\n"
    "as a tutorial for template developers.\n"
    "\n"
    "Supported options:\n"
    "  -C, --directory=dir\toutput files in dir instead of current directory\n"
    "  -f, --force\toverwrite existing files (use with caution)\n"
    "  -h. --help\t\tprint usage summary (this text)"
}

# Default engine behaviour (before parsing options)
variable dir	{example}
engine mode -overwrite +move-if-change

# Parse options. The last argument (between braces) is a regular Tcl switch
# body.
template options {
    -C - --directory	{ engine chdir [template arg] }
    -f - --force	{ engine mode +overwrite }
    -h - --help		{ engine mode +verbose; puts [template usage]; exit 0 }
}

# Require additional procedures. Since the file name is relative, it will be
# searched in the template's directory. Absolute file names are accepted as
# well.
template require example.tcl

# Get the main component object(s). Since this template can only handle one
# component at a time, we check that we have only one component object in the
# returned list.
set c [dotgen components]
if {[llength $c] > 1} {
    template fatal "This template can only handle one component at a time."
}

# The variable "c" now contains a component object: this is a reference to a
# Tcl procedure that represents the actual component object. For instance, we
# can get (and print) the component's name like this:
template message "now processing component [$c name]"

# Define a header *program* in a variable. Because of the use of braces {}
# around the variable definition, no interpretation is done yet.
# This program will be used later to output a standard header for each
# generated file.
set header {/* <"[language hfill \
   " --- Generated by [dotgen genom version]. Do not edit " - 73]"> */

<'catch {puts [language comment c [dotgen input notice]]}'>
}

# Now parse our example template source file with the "template parse"
# procedure. The last arguments pairs are the output file name, in this case
# this will be "src/example.c". Other arguments (before the last pair) are
# the source files. In this case, the only source is the file example.c
template parse		\
    file example.c	\
    file src/example.c

# This will be detailed later
template parse file example.Makefile.am file src/Makefile.am
template parse file example.configure.ac file src/configure.ac

buildenv autoconf subdir src
buildenv autoconf create
