<'
#
# Copyright (c) 2010-2014, 2017-2020, 2023, 2025 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 Thu Jan 28 2010
#

# check arguments
if {[llength $argv] != 4} {
  error "expected argument: components input lang terse"
}
lassign $argv components input lang terse

set c [lindex $components 0]
set pkgname [$c name]-[expr {[$c class] eq "interface" ? "lib" : ""}]genom3
if {[catch {$c version} version]} { set version 0 }
if {[catch {$c email} email]} { set email {} }
'>
dnl Autoconf file for building <"[$c name]"> codels library.

<'# generate copyright notice (if any)
if {![catch {dotgen input notice} text]} {
  puts dnl[join [split $text "\n"] "\ndnl"]
}'>
AC_PREREQ(2.59)

AC_INIT([<"$pkgname">],[<"$version">],[<"$email">])
AC_CONFIG_MACRO_DIR([autoconf])
AC_CONFIG_AUX_DIR([autoconf])
AC_CONFIG_HEADERS([autoconf/ac<"[$c name]">.h])
AM_INIT_AUTOMAKE([foreign no-define])

dnl Compilers
dnl
LT_INIT([disable-static])

<' # compute package languages
set lang $lang
foreach c $components { lappend lang [$c language] }

foreach l [lsort -unique $lang] {
  switch -exact -- $l {
    c		{ puts {AC_PROG_CC} }
    c++		{ puts {AC_PROG_CXX} }
    default	{ error "$l language unsupported" }
  }
}
'>

dnl Require GNU make
AC_CACHE_CHECK([for GNU make], [ac_cv_path_MAKE],
  [AC_PATH_PROGS_FEATURE_CHECK([MAKE], [make gmake],
    [case `$ac_path_MAKE --version 2>/dev/null` in
       *GNU*) ac_cv_path_MAKE=$ac_path_MAKE; ac_path_MAKE_found=:;;
     esac],
    [AC_MSG_ERROR([could not find GNU make])])])
AC_SUBST([MAKE], [$ac_cv_path_MAKE])


dnl External packages
<'set requires [list]'>
<'set codels_requires [list]'>
<'foreach c $components {'>
<'  lappend requires {*}[$c require]'>
<'  lappend codels_requires {*}[$c codels-require]'>
<'}'>
<'if {[lsearch $requires "genom3 *"] < 0} { lappend requires genom3 } '>
AC_SUBST(requires_SPEC, ['m4_normalize([
  <"[join $requires ",\n  "]">
])'])
<'if {[llength $codels_requires]} {'>
AC_SUBST(codels_requires_SPEC, ['m4_normalize([
  <"[join $codels_requires ",\n  "]">
])'])
<'} else {'>
AC_SUBST(codels_requires_SPEC)
AC_SUBST(codels_requires_CFLAGS)
AC_SUBST(codels_requires_LIBS)
<'}'>

PKG_CHECK_MODULES(requires, [$requires_SPEC])
<'if {[llength $codels_requires]} {'>
PKG_CHECK_MODULES(codels_requires, [$codels_requires_SPEC])
<'}'>

AC_PATH_PROG(GENOM3, [genom3], [no])
if test "$GENOM3" = "no"; then
  AC_MSG_ERROR([genom3 tool not found], 2)
fi

<'if {"component" in [lmap c $components {$c class}]} {'>
dnl --with-templates option
AG_OPT_TEMPLATES([$GENOM3 <"[dotgen genom cmdline]">],
    [<"$input">])
<'}'>

<'if {!$terse} {'>
dnl Doc
AM_MISSING_PROG([ASCIIDOCTOR], [asciidoctor])
<'}'>

dnl Output
AC_CONFIG_FILES([
<'foreach c $components {'>
<'  switch [$c class] {'>
<'    interface {'>
	<"[$c name]">-libgenom3.pc
<'    }'>
<'    component {'>
	<"[$c name]">-genom3.pc
	<"[$c name]">-genom3-uninstalled.pc
<'    }'>
<'  }'>
<'}'>
	Makefile
	codels/Makefile
])
AC_OUTPUT
<'if {"component" in [lmap $c $components {$c class}]} {'>
AG_OUTPUT_TEMPLATES
<'}'>
