#
# Copyright (c) 2012,2017-2018,2020,2023 LAAS/CNRS
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without   fee is hereby granted, provided   that the above  copyright
# notice and this permission notice appear in all copies.
#
# 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.
#
#

# native ros client generation.

template usage {*}{
  "\n"
  "Native ros client for ros-based genom components.\n"
  "\n"
  "This template generates the native ros communication tools to control:\n"
  "genom components generated with the ros/server template. This includes\n"
  "messages, services and action for C++ and python.\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 ros/client/ros
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 }
}
if {![llength $argv]} { puts [template usage]; exit 2 }

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]}]

# component compositing not (yet?) supported
set comp [template arg components]
if {[llength $comp] > 1} {
  template fatal "component compositing not supported"
}

# 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]

# setup build environment
template parse args [list $comp $msg_types $input] \
    file client.Makefile.am file Makefile.am
template parse args [list $comp $msg_types] \
    file client.configure.ac file configure.ac
template parse file ../../common/autoconf/ag_ros.m4 file autoconf/ag_ros.m4
template parse args [list $comp $msg_types] \
    file client.pc.in file [$comp name]_ros.pc.in
template parse args $comp \
    file clientConfig.cmake.in file [$comp name]_rosConfig.cmake.in
template parse args $comp \
    file clientConfig-version.cmake.in \
    file [$comp name]_rosConfig-version.cmake.in
template parse args $comp \
    file client-msg-paths.cmake file [$comp name]_ros-msg-paths.cmake

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
