<'
#
# Copyright (c) 2012-2013,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.
#
#                                            Anthony Mallet on Thu Oct 18 2012
#

# check arguments
if {[llength $argv] != 3} { error "expected arguments: component types input" }
lassign $argv component types input

# compute handy shortcuts
set comp [$component name]
'>

ACLOCAL_AMFLAGS=	-I autoconf

# --- ros configuration variables ------------------------------------------

ROS_ENV=

# Store rospack cache locally, since it is architecture dependant
ROS_ENV+= ROS_HOME=${top_builddir}/ros-cache

# ROS_ROOT is normally obsolete, but still required for gendeps
ROS_ENV+= ROS_ROOT=${rospack_prefix}/share/ros

# ROS_PACKAGE_PATH points to the ros hierarchy inside generated sources
ROS_ENV+= \
  ROS_PACKAGE_PATH=${abs_top_srcdir}/<"$comp">_ros:${std_msgs_prefix}/share

# PATH (rospack)
ROS_ENV+= PATH=${PATH}:${rospack_prefix}/bin

# PYTHONPATH (rospkg, roslib) set by configure
ROS_ENV+= PYTHONPATH=${PYTHONPATH}

# LD_LIBRARY_PATH for rospack binary lacking RPATH
ROS_ENV+= LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${rospack_prefix}/lib


# --- pkg-config / cmake / catkin ------------------------------------------

pkgconfigdir=	$(libdir)/pkgconfig
pkgconfig_DATA=	<"$comp">_ros.pc

cmakedir=	$(datadir)/<"$comp">_ros/cmake
cmake_DATA=\
		<"$comp">_rosConfig.cmake		\
		<"$comp">_rosConfig-version.cmake	\
		<"$comp">_ros-msg-paths.cmake

# catkin marker file
install-data-hook:
		test -e $(DESTDIR)$(prefix)/.catkin || \
		  >$(DESTDIR)$(prefix)/.catkin 2>$(builddir)/.catkin

uninstall-hook:
		test -e $(builddir)/.catkin && rm -f $(DESTDIR)$(prefix)/.catkin


# --- msg ------------------------------------------------------------------

compdir=	${datadir}/<"$comp">_ros
msgdir=		${compdir}/msg
srvdir=		${compdir}/srv

comp_DATA=	<"$comp">_ros/package.xml

msg_DATA=
<'foreach t $types {'>
<'  if {[catch {$t masquerade ros}]} {'>
msg_DATA+=	<"$comp">_ros/msg/<"[rosmsg_cname $t]">.msg
<'  }'>
<'}'>
msg_DATA+=	${msg_files}

srv_DATA=
<'foreach s [$component services] {'>
<'  if {[$s kind] != "activity"} {'>
srv_DATA+=	<"$comp">_ros/srv/<"[$s name]">.srv
<'  }'>
<'}'>


# --- C++ headers ----------------------------------------------------------

cppdir=		${includedir}/<"$comp">_ros
cpp_DATA=	${h_files}


# --- python ---------------------------------------------------------------

pythondir=	${libdir}/python${PYTHON_VERSION}/site-packages/<"$comp">_ros
pythonmsgdir=	${pythondir}/msg
pythonsrvdir=	${pythondir}/srv

python_DATA=	py/__init__.py
pythonmsg_DATA=	py/msg/__init__.py ${pymsg_files}
pythonsrv_DATA=	py/srv/__init__.py ${pysrv_files}


# --- regen ----------------------------------------------------------------

.PHONY: regen
$(srcdir)/depend.am regen: <"$input">
	${genom3} <"[dotgen genom cmdline]">		\
	  <"[dotgen template dir]"> -C . <"$input">

include $(srcdir)/depend.am


# --- generated files ------------------------------------------------------

<'set msgdir ${comp}_ros/msg'>
<'set srvdir ${comp}_ros/srv'>
<'set actdir ${comp}_ros/action'>
<'set actpfix {'>
<'  Action ActionFeedback ActionGoal ActionResult Feedback Goal Result'>
<'}'>

BUILT_SOURCES=	${msg_files}
BUILT_SOURCES+= ${cpp_DATA}
BUILT_SOURCES+= ${python_DATA} ${pythonmsg_DATA} ${pythonsrv_DATA}

gensrvcpp_cmd=\
  ${GENCPP} -e ${gencpp_prefix}/share/gencpp				\
    -I '<"$comp">_ros:<"$msgdir">' ${genmsg_path}			\
    -p <"$comp">_ros -o cpp "$$src"
genmsgcpp_cmd=\
  ${GENCPP} -e ${gencpp_prefix}/share/gencpp				\
    -I '<"$comp">_ros:<"$msgdir">' ${genmsg_path}			\
    -p <"$comp">_ros -o cpp "$$src"
gensrvpy_cmd=\
 ${GENSRVPY} -I '<"$comp">_ros:<"$msgdir">' ${genmsg_path}		\
    -p <"$comp">_ros -o py/srv "$$src"
genmsgpy_cmd=\
  ${GENMSGPY} -I '<"$comp">_ros:<"$msgdir">' ${genmsg_path}		\
    -p <"$comp">_ros -o py/msg "$$src"
genaction_cmd=\
  ${GENACTION} -o '<"$msgdir">' "$$src"

gensrvcpp_rule=\
  test -d depend || mkdir -p depend;					\
  ${ROS_ENV} ${MSGMD5} -M --Mf "cpp/$${base}.h py/srv/_$${base}.py"	\
    -o "depend/$${base}.srv.md5" "$$src";				\
  echo ${gensrvcpp_cmd}; ${ROS_ENV} ${gensrvcpp_cmd}
genmsgcpp_rule=\
  test -d depend || mkdir -p depend;					\
  ${ROS_ENV} ${MSGMD5} -M --Mf "cpp/$${base}.h py/msg/_$${base}.py"	\
    -o "depend/$${base}.msg.md5" "$$src";				\
  echo ${genmsgcpp_cmd}; ${ROS_ENV} ${genmsgcpp_cmd}
gensrvpy_rule=\
  echo ${gensrvpy_cmd}; ${ROS_ENV} ${gensrvpy_cmd}
genmsgpy_rule=\
  echo ${genmsgpy_cmd}; ${ROS_ENV} ${genmsgpy_cmd}
genaction_rule=\
  echo ${genaction_cmd}; ${ROS_ENV} ${genaction_cmd}

dep_files=
msg_files=
h_files=
pymsg_files=
pysrv_files=

<'foreach t $types {'>
<'  if {[catch {$t masquerade ros}]} {'>
dep_files+=	depend/<"[rosmsg_cname $t]">.msg.md5.d
h_files+=	cpp/<"[rosmsg_cname $t]">.h
pymsg_files+=	py/msg/_<"[rosmsg_cname $t]">.py

cpp/<"[rosmsg_cname $t]">.h: <"$msgdir">/<"[rosmsg_cname $t]">.msg ${MSGMD5}
	@base='<"[rosmsg_cname $t]">'; src="<"$msgdir">/$${base}.msg"; \
	${genmsgcpp_rule}

py/msg/_<"[rosmsg_cname $t]">.py: <"$msgdir">/<"[rosmsg_cname $t]">.msg
	@src='<"$msgdir">/<"[rosmsg_cname $t]">.msg'; ${genmsgpy_rule}

-include depend/<"[rosmsg_cname $t]">.msg.md5.d

<'  }'>
<'}'>

<'foreach s [$component services] {'>
<'  switch [$s kind] {'>
<'    activity {'>
<'      foreach p $actpfix {'>
dep_files+=	depend/<"[$s name]$p">.msg.md5.d
msg_files+=	<"$msgdir">/<"[$s name]$p">.msg
h_files+=	cpp/<"[$s name]$p">.h
pymsg_files+=	py/msg/_<"[$s name]$p">.py

cpp/<"[$s name]$p">.h: <"$msgdir">/<"[$s name]$p">.msg ${MSGMD5}
	@base='<"[$s name]$p">'; src="<"$msgdir">/$${base}.msg"; \
	${genmsgcpp_rule}

py/msg/_<"[$s name]"><"$p">.py: <"$msgdir">/<"[$s name]"><"$p">.msg
	@src='<"$msgdir">/<"[$s name]"><"$p">.msg'; ${genmsgpy_rule}

-include depend/<"[$s name]$p">.msg.md5.d

<'      }'>
<'foreach p $actpfix {'><"$msgdir">/<"[$s name]$p">.msg <'}'>: \
  <"$actdir">/<"[$s name]">.action
	@src='<"$actdir">/<"[$s name]">.action'; ${genaction_rule}
<'    }'>
<'    default {'>
dep_files+=	depend/<"[$s name]">.srv.md5.d
h_files+=	cpp/<"[$s name]">.h
h_files+=	cpp/<"[$s name]">Request.h
h_files+=	cpp/<"[$s name]">Response.h
pysrv_files+=	py/srv/_<"[$s name]">.py

cpp/<"[$s name]">.h cpp/<"[$s name]">Request.h cpp/<"[$s name]">Response.h: \
  <"$srvdir">/<"[$s name]">.srv ${MSGMD5}
	@base='<"[$s name]">'; src="<"$srvdir">/$${base}.srv"; ${gensrvcpp_rule}

py/srv/_<"[$s name]">.py: <"$srvdir">/<"[$s name]">.srv
	@src='<"$srvdir">/<"[$s name]">.srv'; ${gensrvpy_rule}

-include depend/<"[$s name]">.srv.md5.d

<'    }'>
<'  }'>
<'}'>

py/msg/__init__.py: ${pymsg_files}
	@src='--initpy'; ${genmsgpy_rule}

py/srv/__init__.py: ${pysrv_files}
	@src='--initpy'; ${gensrvpy_rule}

py/__init__.py: py/msg/__init__.py py/srv/__init__.py
	touch $@


# output list of installed files - useful e.g. for dynamic robotpkg PLIST
install-data-hook: plist
plist:
	@exec >$@;							\
	for f in ${pkgconfig_DATA}; do echo "${pkgconfigdir}/$${f##*/}"; done;\
	for f in ${cmake_DATA}; do echo "${cmakedir}/$${f##*/}"; done;	\
	for f in ${comp_DATA}; do echo "${compdir}/$${f##*/}"; done;	\
	for f in ${msg_DATA}; do echo "${msgdir}/$${f##*/}"; done;	\
	for f in ${srv_DATA}; do echo "${srvdir}/$${f##*/}"; done;	\
	for f in ${cpp_DATA}; do echo "${cppdir}/$${f##*/}"; done;	\
	for f in ${python_DATA}; do echo "${pythondir}/$${f##*/}"; done;\
	for f in ${pythonmsg_DATA}; do					\
	  echo "${pythonmsgdir}/$${f##*/}";				\
	done;								\
	for f in ${pythonsrv_DATA}; do					\
	  echo "${pythonsrvdir}/$${f##*/}";				\
	done



# --- cleaning -------------------------------------------------------------

CLEANFILES=	${BUILT_SOURCES} ${dep_files} ${dep_files:.d=}

clean-local:
	-rm -rf ${ROS_HOME}
