# Copyright (C) 2006-2016  CEA/DEN, EDF R&D
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#
generate_corba_module_source()
{
   cd ${tmp_dir}
   if [ ${BE_QUIET} -lt 1 ] ; then
       echo -e "\n\n\tCorba wrapper generation"
   fi
#
# ------------------------- Corba generation ------------------------------------------------------
   if [ ${BE_QUIET} -lt 1 ] ; then
       echo -e "\n\n-> Generate Salome2 Corba files\n"
   fi
   cat ${CLASS_NAME}_public_functions |\
   awk -v class_name=${CLASS_NAME} -f ${gene_dir}/parse3.awk

   if [ `grep -c MEDMEM:: ${CLASS_NAME}_public_functions` -gt 0 ]
   then
       USE_MED=1
    else
       USE_MED=0
    fi

# outputs

   cat parse_result >> hxx2salome_journal
   echo -e "\n----------------- IDL file ------------------\n" >> hxx2salome_journal
   cat code_idl >> hxx2salome_journal
   echo -e "\n----------------- hxx file ------------------\n" >> hxx2salome_journal
   cat code_hxx >> hxx2salome_journal
   echo -e "\n----------------- cxx file ------------------\n" >> hxx2salome_journal
   cat code_cxx >> hxx2salome_journal

   if [ ${BE_QUIET} -lt 1 ] ; then
       echo -e "\n  IDL file:"
       cat code_idl
       echo
   fi

   if [ ${BE_QUIET} -lt 1 ] ; then
       echo -e "\n-> Substitute generated code in idl file"
   fi
   echo "// this idl file was generated by hxx2salome" > tmpfile
   cat ${CLASS_NAME}_SRC/idl/${CLASS_NAME}_Gen.idl |awk \
       '$0 ~ "HXX2SALOME_IDL_CODE" {system("cat code_idl >> tmpfile")} 
        $0 != "HXX2SALOME_IDL_CODE" { print $0 >> "tmpfile" }'
   mv tmpfile ${CLASS_NAME}_SRC/idl/${CLASS_NAME}_Gen.idl
#
   if [ ${BE_QUIET} -lt 1 ] ; then
       echo -e "\n-> Substitute generated code in hxx file"
   fi
   echo "// this hxx file was generated by hxx2salome" > tmpfile
   cat ${CLASS_NAME}_SRC/src/${CLASS_NAME}_I/${CLASS_NAME}_i.hxx |awk '
       $0 ~ "HXX2SALOME_HXX_CODE" {system("cat code_hxx >> tmpfile")}
       $0 !~ "HXX2SALOME" { print $0 >> "tmpfile" }'
   mv tmpfile ${CLASS_NAME}_SRC/src/${CLASS_NAME}_I/${CLASS_NAME}_i.hxx
#
   if [ ${BE_QUIET} -lt 1 ] ; then
       echo -e "\n-> Substitute generated code in cxx file"
   fi
   echo "// this cxx file was generated by hxx2salome" > tmpfile
   cat ${CLASS_NAME}_SRC/src/${CLASS_NAME}_I/${CLASS_NAME}_i.cxx |awk -v cpp_include=$hxx '
       $0 ~ "HXX2SALOME_CXX_CODE" {system("cat code_cxx >> tmpfile")}
       $0 ~ "HXX2SALOME_CPP_INCLUDE" { printf "#include \"%s\"\n",cpp_include >> "tmpfile" }
       $0 !~ "HXX2SALOME" { print $0 >> "tmpfile" }'
   mv tmpfile ${CLASS_NAME}_SRC/src/${CLASS_NAME}_I/${CLASS_NAME}_i.cxx

   get_info_makefile
   
   if [ ${BE_QUIET} -lt 1 ] ; then
       echo -e "\n-> Substitute flags in Makefile.am files"
   fi

#    Root Makefile.am
   sed -e "s/SUBDIRS = /SUBDIRS =  ${CLASS_NAME}_I/" \
        < ${CLASS_NAME}_SRC/src/Makefile.am \
        > ${CLASS_NAME}_SRC/src/Makefile.am_new
   \mv -f  ${CLASS_NAME}_SRC/src/Makefile.am_new \
        ${CLASS_NAME}_SRC/src/Makefile.am

#    IDL Makefile.am
   if [ ${USE_MED} == 1 ]
       then
       sed "s?USE_MED=0?USE_MED=1\nIDL_FILES += MED.idl\nIDL_SRC += MEDSK.cc?" ${CLASS_NAME}_SRC/idl/Makefile.am > tmpfile
       sed "s?XXX?XXX -DUSE_MED -I\${MED_ROOT_DIR}/idl/salome?" tmpfile > tmp2file
       mv tmp2file ${CLASS_NAME}_SRC/idl/Makefile.am
   fi

#    C++ implementation Makefile.am
   sed "s?HXX2SALOME_INCLUDE?${makefile_include}?g
        s?HXX2SALOME_PYTHON_FILE?${python_test_file}?g
        s?HXX2SALOME_LIB?${makefile_lib}?g
       " ${CLASS_NAME}_SRC/src/${CLASS_NAME}_I/Makefile.am > tmpfile
   sed "s?XXX?XXX -I$hxx_dir?" tmpfile > tmp2file
   if [ ${BE_QUIET} -lt 1 ] ; then
       cat ${CLASS_NAME}_public_functions
   fi
   if [ ${USE_MED} == 1 ]
   then
       sed "/AM_CXXFLAGS/s?\$? -DUSE_MED \${MED2_INCLUDES} -I\${MED_ROOT_DIR}/include/salome?" tmp2file > tmpfile
       sed "s?USE_MED=0?USE_MED=1?" tmpfile > tmp2file
   fi 
   mv tmp2file ${CLASS_NAME}_SRC/src/${CLASS_NAME}_I/Makefile.am
   \rm -f tmpfile tmp2file
#
#    generate component catalog
   if [ ${BE_QUIET} -lt 1 ] ; then
       echo -e "\n-> Generate component catalog" | tee hxx2salome_journal
   fi
   if [ -f ${KERNEL_ROOT_DIR}/bin/salome/runIDLparser ]
   then
       idlparser=${KERNEL_ROOT_DIR}/bin/salome/runIDLparser
   else
       idlparser=${gene_dir}/runIDLparser
   fi
   if [ ${USE_MED} == 1 ]
   then
       idlparser="${idlparser} -DUSE_MED=1 -I${MED_ROOT_DIR}/idl/salome"
   fi

   cd ${CLASS_NAME}_SRC/resources
   VER=`cat ${KERNEL_ROOT_DIR}/bin/salome/VERSION | awk ' { print $NF }'` # extract number of version 
   ${idlparser} -Wbcatalog=tmp.xml,icon=${CLASS_NAME}.png,version=${VER} -I${KERNEL_ROOT_DIR}/idl/salome ../idl/${CLASS_NAME}_Gen.idl >& /dev/null | tee  hxx2salome_journal
   cat tmp.xml | sed 's/_Gen//g' > ${CLASS_NAME}Catalog.xml
   \rm -f tmp.xml

}

clean_corba_module_source()
{
   \rm -rf ${CLASS_NAME}_SRC/src/${CLASS_NAME}_I
   \rm -rf ${CLASS_NAME}_SRC/idl/*
   touch ${CLASS_NAME}_SRC/idl/Makefile.am
   sed -e "s/^CHECK_OMNIORB/dnl CHECK_OMNIORB/" -e "/CORBA/s/^/dnl /" ${CLASS_NAME}_SRC/configure.in.base > ${CLASS_NAME}_SRC/configure.in.temp
   mv ${CLASS_NAME}_SRC/configure.in.temp ${CLASS_NAME}_SRC/configure.in.base
}
