#! /bin/bash
#*******************************************************************************
# E.S.O. - ALMA project
#
# "@(#) $Id: acsMakeDDSDependencies,v 1.7.2.1 2010/09/22 16:31:26 javarias Exp $"
#
# who       when       what
# --------  --------   ----------------------------------------------
#************************************************************************
#   NAME
#   acsMakeDDSDependencies - create the makefile to build an DDS libraries
# 
#   SYNOPSIS
#
#   acsMakeDDSDependencies [/vw] <jarfile> 
# 
#   DESCRIPTION
#   Utility used by acsMakefile to create the makefile to build a DDS 
#   libraries
#   It is not intended to be used as a standalone command.
#
#
#   (1) see also GNU Make 3.64, 4.3.5 pag 37
#
#
#   The .ddds itself depends to Makefile.
#
#   The rules is written to standard output.
#
#   <jarfile>     The name of the destination jarfile
#                 (Without directory)
#
#
#   FILES
#   $ACSROOT/include/acsMakefile   
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   SEE ALSO 
#   acsMakefile, Makefile, (GNU) make
#
#   BUGS    
#
#----------------------------------------------------------------------

typesFromDDS() {
    alpha=`egrep "^#pragma DCPS_DATA_TYPE" $1 |  sed 's/.*\:\:\([^\"]*\)\"/\1/'`
    echo "$alpha"
}



ddsIdl=$1

echo "# Dependency file for program: ${ddsIdl}"
echo "# Created automatically by acsMakeDDSDependencies -  `date '+%d.%m.%y %T'`"
echo "# DO NOT EDIT THIS FILE"
echo "../object/${ddsIdl}.ddds: Makefile"
echo ""
echo "DEBUG: ${debug}"
echo ""

echo "USER_IDL := -I\$(DDS_ROOT) \$(USER_IDL)"
echo ""

if [ -f ../idl/${ddsIdl}.midl ]; then 
    typesList=`typesFromDDS ../idl/${ddsIdl}.midl`
elif [ -f ../idl/${ddsIdl}.idl ]; then
    typesList=`typesFromDDS ../idl/${ddsIdl}.idl`
else
    echo "\$(AT)@echo \"** NO IDL or mIDL FILE FOUND FOR ${ddsIdl}\""
    exit -1
fi
#
echo ""
extensions="C.cpp S.cpp C.h C.inl S.h S.inl S_T.h S_T.inl S_T.cpp"

#
#
#
echo "\$(addprefix ../object/${ddsIdl}, ${extensions}): ${ddsIdl}.idl Makefile"
echo "	\$(AT)@echo \"== IDL Compiling for DDS TAO (C++): \$(<F)\""
echo "	\$(AT) \$(TAO_IDL) \$(MK_IDL_PATH) \$(TAO_MK_IDL_PATH) -o ../object \$(TAO_IDLFLAGS) $<"

targetList=""
libList=""
for type in ${typesList}
  do
  targetList="../object/${type}TypeSupport.idl ../object/${type}TypeSupportImpl.cpp ../object/${type}TypeSupportImpl.h ${targetList} "
  # to be used for top-level target do_fake_${ddsIdl}
  libList="../lib/lib${type}TypeSupportStubs.a ../lib/lib${type}TypeSupportStubs.so ${libList}"

  echo "# ${type}"
  echo ""
  echo "vpath ${type}TypeSupport%.h ../object"
  echo "vpath ${type}TypeSupport%.inl ../object"
  echo ""
  echo "../object/${type}TypeSupportStubs.da:"
  echo "	\$(AT)echo \"== (Dependencies) == \$@ \" "
  echo "	\$(AT)vltMakeLibraryDependencies ${type}TypeSupportStubs \"${type}TypeSupportS ${type}TypeSupportImpl ${type}TypeSupportC\" \"\" \"${_NOSHARED}\" \"${_NOSTATIC}\" \"${ddsIdl}Stubs\" > ../object/${type}TypeSupportStubs.da"
  echo ""
  echo "include ../object/${type}TypeSupportStubs.da"
  echo ""
  echo ""
  installList="../object/${type}TypeSupportC.h ../object/${type}TypeSupportS.h ../object/${type}TypeSupportImpl.h ${installList}"

  echo "CSOURCENAMES:= ${type}TypeSupportC ${type}TypeSupportS ${type}TypeSupportImpl \$(CSOURCENAMES)"
  echo ""
  generatedStubFiles=""
  aaa=""
  for ext in ${extensions}
    do
       generatedStubFiles="${type}TypeSupport${ext} ${generatedStubFiles}"
       aaa="../object/${type}TypeSupport${ext} ${aaa}"
    done
  clean_list="${aaa} ../object/${type}TypeSupportS.o ../object/${type}TypeSupportC.o ../object/${type}TypeSupportImpl.o ../object/${type}TypeSupportStubs.da ../lib/lib${type}TypeSupportStubs.a ../lib/lib${type}TypeSupportStubs.so ${clean_list}" 
# note: there' s no particular reason for chosing ../object/${ddsIdl}S.cpp as first prerequisite
#
  echo "${aaa}: ../object/${ddsIdl}S.cpp ../object/${type}TypeSupport.idl Makefile"
  echo "	\$(AT)@echo \"== IDL Compiling for DDS TAO (C++): ${type}TypeSupport.idl \""
  echo "	\$(AT) \$(TAO_IDL) \$(MK_IDL_PATH) \$(TAO_MK_IDL_PATH) -o ../object \$(TAO_IDLFLAGS) ../object/${type}TypeSupport.idl"
  echo ""
  done

#
# this part is independent from the types contained in the IDL file
#
#
# TODO
for ext in ${extensions}
  do
  clean_list="${clean_list} ../object/${ddsIdl}${ext}"
done



echo "${targetList}: ../idl/${ddsIdl}.idl";
echo "	\$(AT)\$(ECHO) \"== Generating DDS support for IDL-specified types (\$<)\""
echo "	\$(AT)(cd ../object && cp ../idl/${ddsIdl}.idl .  && \$(DDS_ROOT)/bin/dcps_ts.pl ${ddsIdl}.idl && \$(RM) ${ddsIdl}.idl)"
echo ""
echo ".PHONY: do_fake_${ddsIdl}"
echo "do_fake_${ddsIdl}: ${libList}"
echo ""

#
# clean area
#

echo ""
echo "#"
echo "# CLEAN"
echo "#"
echo ".PHONY : clean_${ddsIdl}"
echo ""
echo "clean_${ddsIdl}:"
echo "	\$(AT)\$(RM) ../object/${ddsIdl}.ddds"
echo "	\$(AT)\$(RM) ${targetList} ${clean_list}"
# the cleaning of the library should actually be handled by a separate target
#       generated by the corresponding dependencies script...
# in an ideal world, that is.
echo ""
#
# install area
#
echo ""
echo "#"
echo "# INSTALL"
echo "#"
echo ".PHONY : install_dds_${ddsIdl}"
echo ""
echo "install_dds_${ddsIdl}:"
echo "	\$(AT)cp ${libList} \$(VLTTOP)/lib"
echo "	\$(AT)cp ${installList} \$(VLTTOP)/include"
echo ""


exit;

# ___oOo___
