#! /bin/bash
if [ "`uname`" = "Linux" ]; then enable -n echo; fi
#*******************************************************************************
# E.S.O. - ALMA project
#
# "@(#) $Id: acsMakeXmlErrLibraries,v 1.26 2010/07/09 15:16:06 rtobar Exp $"
#

# the ugly solution taken here is really ugly: it requires one to maintain the list
# of variables which have to be unset in order to run a limited scope make command.
# this is a nightmare, and is only implemented out of hurry
#
# furthermore, there's no error handling
#
xmlName=$1
target=$2
makeDir=$3
MAKE_ONLY=$4
MAKE_VXWORKS=$5
DEBUG=$6

IDL_D="../idl"
FILE=/tmp/XmlErrGeneration_$USER\_$$
#
# I cannot put the following variable under object since 
# make do_idl cleans up ../object
# 
tDir="tmp$$"

function EnvCleanup { 

    unset ACSERRDEF
    unset LIBRARIES 
    unset EXECUTABLES
    unset LIBRARIES_L
    unset EXECUTABLES_L
    unset IDL_FILES
    unset TMP_DEP_IDL; 
    unset LIBRARY_LIST; 
    unset ALL_TARGET; 
    unset CSOURCENAMES
}


if [ "$target" = "all" ]
then
    #
    # first step, we generate an IDL file out of an XML file, 
    acserrGenIDL ../idl/${xmlName}.xml ../idl/${xmlName}.idl || exit -2
    # second step, we generate an C++ and header  files out of an XML file, 
    if [[ "$MAKE_ONLY" = "" || `echo $MAKE_ONLY | grep C++` != "" ]] 
	then
	acserrGenCpp ../idl/${xmlName}.xml ../object/${xmlName}.cpp ../object/${xmlName}.h || exit -2
    fi
    # third step, we write the header in $FILE
    #
    echo "# Dependency file for XMLERR: ${xmlName}" > $FILE
    echo "# DO NOT EDIT THIS FILE" >> $FILE
    echo "" >> $FILE
    echo "MAKEDIR=${makeDir}" >> $FILE
    echo "../lib/python/site-packages/${xmlName}Impl.py: ../object/${xmlName}Impl.py" >> $FILE
    echo "IDL_FILES=${xmlName}" >> $FILE
    echo "${xmlName}_OBJECTS = ${xmlName} ${xmlName}S ${xmlName}C" >> $FILE
    echo "include ${makeDir}/acsMakefile" >> $FILE
    echo "" >> $FILE
    EnvCleanup


    # fourth step, if we're not on VxWorks we generate Java and Python 
    # source files
    #
    if [ xx$MAKE_VXWORKS = xx ]
	then
	if [[ "$MAKE_ONLY" = "" || `echo $MAKE_ONLY | grep Java` != "" ]] 
	    then 
	    mkdir -p ${tDir}/src || exit -34
	    acserrGenJava ../idl/${xmlName}.xml ${tDir}/src || exit -2
	fi
	if [[ "$MAKE_ONLY" = "" || `echo $MAKE_ONLY | grep Python` != "" ]] 
	    then 
	    acserrGenPython ../idl/${xmlName}.xml ../object/${xmlName}Impl.py || exit -2
	    cp ../object/${xmlName}Impl.py ../lib/python/site-packages/${xmlName}Impl.py
	    echo "PY_MODULES=../object/${xmlName}" >> $FILE
	fi

	echo "" >> $FILE
	    
	if [[ "$MAKE_ONLY" = "" || `echo $MAKE_ONLY | grep C++` != "" ]] 
	    then 
	    echo "LIBRARIES=${xmlName}" >> $FILE
	    echo "${xmlName}_LIBS = acserr" >> $FILE
	    make -f $FILE MAKE_ONLY=$MAKE_ONLY do_idl do_indexfiles do_libs || exit -1
	    else 
	    make -f $FILE MAKE_ONLY=$MAKE_ONLY do_idl do_indexfiles || exit -1
	fi
	if [[ "$MAKE_ONLY" = "" || `echo $MAKE_ONLY | grep Java` != "" ]] 
	    then 
	    echo "== Compiling generated Java classes into Stub jarfile"
	    if [ "`uname`" = "$CYGWIN_VER" ]
	    then
	        (cd ${tDir}; mkdir lib; cp ../../lib/*.jar lib; cd src;  export CLASSPATH="`vltMakeJavaClasspath`${PATH_SEP}."; javac -d .. `find . -name \*.java | tr '\n' ' '` ) || exit -13
	    else
	        (cd ${tDir}; ln -s ../../lib lib; cd src;  export CLASSPATH="`vltMakeJavaClasspath`${PATH_SEP}."; javac -d .. `find . -name \*.java | tr '\n' ' '` ) || exit -13
	    fi
	    if [ xx$DEBUG = xxon ]
		then
		(cd ${tDir}; find . \( -name \*.class -o -name \*.java \) -exec jar uf ../../lib/${xmlName}.jar  \{\} \; )
	    else
		(cd ${tDir}; find . -name \*.class -exec jar uf ../../lib/${xmlName}.jar  \{\} \; )	
	    fi
	    rm -fr ${tDir}
#	rm lib
	fi
    else
	if [[ "$MAKE_ONLY" = "" || `echo $MAKE_ONLY | grep C++` != "" ]] 
	    then 
	    echo "EXECUTABLES=${xmlName}" >> $FILE
	    echo "" >> $FILE
	    make -f $FILE MAKE_ONLY=$MAKE_ONLY do_idl do_indexfiles do_exes || exit -1
	fi
    fi
    rm -f $FILE
fi   # end of 'all' target


#
# clean target
#
if [ "$target" = "clean" ]
then
    EnvCleanup
    echo "# Dependency file for XMLERR: ${xmlName}" > $FILE
    echo "# DO NOT EDIT THIS FILE" >> $FILE
    echo "" >> $FILE
    echo "MAKEDIR=${makeDir}" >> $FILE
    echo "IDL_FILES=${xmlName}" >> $FILE
    echo "${xmlName}_LIBS = ${xmlName}Stubs" >> $FILE
    echo "${xmlName}_OBJECTS = ${xmlName}" >> $FILE
    echo "include ${makeDir}/acsMakefile" >> $FILE

    if [ xx$MAKE_VXWORKS = xx ]
	then
	echo "LIBRARIES=${xmlName}" >> $FILE
	rm -f ../lib/lib${xmlName}*.{a,so}
	rm -f ../object/${xmlName}Impl.py
	rm -f ../lib/python/site-packages/${xmlName}Impl.py
    else
	echo "EXECUTABLES=${xmlName}" >> $FILE
	rm -f ../bin/${xmlName}
    fi

    make -s -f $FILE clean_idl || exit -1
    # now we play it dirty
    rm -f ../object/${xmlName}.da ../object/${xmlName}.h 
    rm -f ../object/${xmlName}.cpp
    rm -f ../object/${xmlName}{C,S}.{h,inl,cpp}
#../object/${xmlName}S_T.{h,cpp,inl}
    rm -f ../object/${xmlName}*.o  
    rm -f ../idl/${xmlName}.idl
    rm -f $FILE
fi
#
# install target
#
if [ "$target" = "install" ]
then
    # targets of this installation are the C++ library called 
    # lib${xmlName}.a and so
    # and also the ../object/${xmlName}.h
    # the ../object/${xmlName}C/S.h/i ..
    #  ../lib/${xmlName}.jar 
    #  ../idl/${xmlName}.idl 
    if [ xx$MAKE_VXWORKS = xx ]
    then
	cp ../object/${xmlName}Impl.py ${VLTTOP}/lib/python/site-packages/${xmlName}Impl.py
	cp ../lib/${xmlName}.jar ${VLTTOP}/lib/${xmlName}.jar
	chmod 664 ${VLTTOP}/lib/${xmlName}.jar
	cp ../lib/lib${xmlName}.{a,so} ${VLTTOP}/lib/
	chmod 664  ${VLTTOP}/lib/lib${xmlName}.{a,so}
	cp ../object/${xmlName}.h ${VLTTOP}/include
	cp ../object/${xmlName}{C,S}.{h,inl} ${VLTTOP}/include
#	cp ../object/${xmlName}S_T.{h,cpp,inl} ${VLTTOP}/include


	cp ../idl/${xmlName}.idl ${VLTTOP}/idl
	cp ../idl/${xmlName}.xml ${VLTTOP}/idl
	# forcing user's write permissions
	chmod u+w ${VLTTOP}/idl/${xmlName}.xml
    else
	cp ../bin/${xmlName} ${BIN}/${xmlName}
	chmod 664 ${BIN}/${xmlName}
    fi
fi

