#
# Copyright (c) 2012-2015, 2017-2018, 2020, 2023, 2026 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 Aug  2 2012
#

# Ros C client library generation.

template usage {*}{
    "\n"
    "C client library for ros-based components.\n"
    "\n"
    "This template generates a C client library for GenoM component built\n"
    "with the ros middleware.\n"
    "\n"
    "Supported options:\n"
    "  -C, --directory=dir\toutput files in dir\n"
    "  -p, --preserve\tdo not overwrite existing files\n"
    "  -h. --help\t\tprint usage summary (this text)"
}

# defaults
set odir client/c
engine mode +overwrite +move-if-change

# parse options
template options {
    -C - --directory	{ set odir [template arg] }
    -p - --preserve	{ engine mode -overwrite }
    -h - --help		{ puts [template usage]; exit 0 }
}
engine chdir $odir

# parse common services specification and input
set input [template arg files]
dotgen parse file \
    [file join [dotgen template builtindir] common/genom.gen]	\
    {*}$input

# normalized list of input
set path [dotgen input path]
set input [lmap f $input {file normalize [file join $path $f]}]

# only one component is allowed
set comp [template arg components]
if {[llength $comp] > 1} {
  template fatal "component compositing not supported"
}

# require utility procs
template require common/docutil.tcl
set types [$comp types public]

# common header for all files (copyright info from .gen file)
set header {/* <"[--- Generated by [dotgen genom version]. Do not edit - ]"> */

<'if {![catch {dotgen input notice} notice]} {
  puts [lang c; comment $notice]
}'>
}

# generate ros .msg/.srv and .action for interface types
template require ../../common/genrosmsg.tcl
set msg_types [genrossrv $comp]
set msg_types [genrosmsg $comp $msg_types]

# client source files
set client_files {
  common/client-info.c
  common/client-json.c
  common/client-plugin.c

  init.cc
  client.cc
  msglib.h
  msglib.cc
  portlib.cc
}

template parse args [list $comp $types] string $header			\
    file [file join [dotgen template builtindir] common/client.h]	\
    file src/[$comp name]/c/client.h
foreach f $client_files {
  template parse args $comp string $header \
        file $f file src/[$comp name]_[file tail $f]
}

template parse file ../../common/genom-serialize.h file src/genom-serialize.h
template parse file private.h file src/private.h
template parse args [list $types] \
    file common/typeops-c.h file src/typeops-c.h
template parse args [list $types] \
    file common/jsonops-c.h file src/jsonops-c.h
template parse args [list $comp $types] \
    file ../../common/serialize.h file src/[$comp name]_serialize.h
template parse args $comp \
    file ../../common/portlib.h file src/[$comp name]_portlib.h
template parse args [list $comp [$comp services]] \
    file ../../common/service.h file src/[$comp name]_service.h
template parse args [list $comp [$comp services]] \
    file ../../common/action.h file src/[$comp name]_action.h

# setup build environment
template parse args [list $comp $msg_types $input] \
    file client.Makefile.am file Makefile.am
template parse args $comp \
    file client.configure.ac file configure.ac
template parse file \
    ../../common/autoconf/ax_pthread.m4 file autoconf/ax_pthread.m4
template parse file ../../common/autoconf/ax_boost_base.m4 \
    file autoconf/ax_boost_base.m4
template parse file ../../common/autoconf/ag_ros.m4 file autoconf/ag_ros.m4
template parse args [list ros $comp] \
    file [file join [dotgen template builtindir] common/c-client.pc.in] \
    file [$comp name]-c-client.pc.in
template parse args [list ros $comp] \
    file [file join \
              [dotgen template builtindir] common/c-client-uninstalled.pc.in] \
    file [$comp name]-c-client-uninstalled.pc.in

template parse args [list $comp $msg_types] \
    file ../../common/ros.package.xml file [$comp name]_ros/package.xml

set deps [list]
foreach d [concat [dotgen input deps] [template deps]] {
  lappend deps "\$(srcdir)/depend.am: $d"
  lappend deps "$d:" {}
}
engine mode +overwrite -move-if-change
template parse raw [join $deps "\n"]\n file depend.am
