#! /bin/bash
#*******************************************************************************
# E.S.O. - ALMA project
#
# "@(#) $Id: acsMakeJavaDependencies,v 1.84 2009/05/14 07:13:04 mzampare Exp $"
#
# who       when       what
# --------  --------   ----------------------------------------------
# mzampare  2002-11-27 generalized, no longer src
# mzampare  2002-11-19 fixed concept bug. Compiled classes were not included in path
# mzampare  2002-11-12 VLTSW20020559 and VLTSW20020518 implemented
# mzampare  2002-08-07 xargs replaced by tr
# mzampare  2002-06-26 modified to allow more comfortable directory selection
# mzampare  2002-06-20 created 

#************************************************************************
#   NAME
#   acsMakeJavaDependencies - create the makefile to build an executable
# 
#   SYNOPSIS
#
#   acsMakeJavaDependencies [/vw] <jarfile> 
# 
#   DESCRIPTION
#   Utility used by acsMakefile to create the makefile to build a Java 
#   Jarfile, based upon the java compiler and the jar archival tool.
#   It is not intended to be used as a standalone command.
#
#
#   (1) see also GNU Make 3.64, 4.3.5 pag 37
#
#
#   The .djar 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    
#
#----------------------------------------------------------------------

if [ "$1" = "/vw" ]
then
    MAKE_VXWORKS="yes"
    shift
else
    MAKE_VXWORKS="no"
fi

jarName=$1
depExt=$2
tgtDir=$3
dirList=$4
extraList=$5
userCompilerFlags=$6
jarCompilerFlags=$7
endorsed=$8
debug=$9

tmpDestDir=../object/${jarName}/
fileListFile="/tmp/acsMakeJavac_${jarName}_${UNIQUE_NUMBER}_${USER_NAME}"

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

if [ "${endorsed}" == "on" ] || [ "${endorsed}" == "1" ]
then
    CLASSMAKER="vltMakeJavaClasspath -endorsed"
else
    CLASSMAKER=vltMakeJavaClasspath
fi
CompilerFlags="${userCompilerFlags} ${jarCompilerFlags}"

if [ "${JAVAC}" == "" ]
then
    JAVAC="javac -J-Xmx1g"
fi

    if [ "${debug}" != "" ]
	then
	# all possible debugging flags are deleted to
	# be later replaced with a global one
	#
	CompilerFlags=`echo $CompilerFlags | sed -e 's/-g //;s/-g[:a-z,]*//'`
	CompilerFlags="$CompilerFlags -g"
    fi

if [ "${MAKE_PURIFY}" != "" -o "${MAKE_PURE}" != "" ]
then
    if [ "${RATIONAL_DIR}" != "" ]
    then
	PURIFY_ADDITION="export ATLTGT=${RATIONAL_DIR}/targets/jdk1.4.0 ;  export PATH=\$\$ATLTGT/cmd:\${PATH} ; "
	mkdir -p ${tmpDestDir}/javi.jir
	cp -r ${RATIONAL_DIR}/targets/jdk1.4.0/lib/com ${tmpDestDir}/javi.jir/
	cp -r ${RATIONAL_DIR}/targets/xml/java/lib/com ${tmpDestDir}/javi.jir/
    fi
fi

if [ "${dirList}" != "" ]
then
    currentLocation=`\pwd | sed "s/\/.*\///"`
    #
    # prepare the list of all objects (full filename)
    for member in ${dirList}
    do
        # prepare the list with the 'src/test' at the front, for DEBUG=on
        sList="${sList} $currentLocation/${member} "

	if [ ${member} = "." ]
	then
	    cList="${cList} *.class "
	else
	    cList="${cList} ${member} "
	fi
    done
    #
    echo "${jarName}_source_file_list:= \$(shell find ${dirList} -name \\*.java | grep -v 'CVS/' | tr '\\n' ' ')"
    echo "${jarName}_class_file_list:= \$(subst .java,.class,\$(foreach src,\$src, \$(${jarName}_source_file_list) ) )"
    echo ""
    #
#
# the usage of .INTERMEDIATE does not seem possible
# for the file with the list of java source files.
#
#
# define the dependency file dependent to the Makefile
    echo "../${tgtDir}/${jarName}.jar: \$(${jarName}_source_file_list) ../object/${jarName}.${depExt}"
    echo "	@echo \"== Making ${jarName}.jar\""
    echo "	\$(AT)\$(shell \$(RM) ${fileListFile})"
    echo "	\$(AT)mkdir -p ${tmpDestDir}"
    echo "	\$(AT)mkdir -p ../${tgtDir}"
    echo "	\$(AT)\$(foreach element,\$?,\$(shell echo -n \"\$(element) \" | grep -v .${depExt}  >> ${fileListFile}))"
    echo "	\$(AT)CLASSPATH=\`${CLASSMAKER}\`:.; export CLASSPATH ; ${JAVAC} ${CompilerFlags} -d ${tmpDestDir} @${fileListFile}"
    echo "	\$(AT)(cd ${tmpDestDir}; jar cf ../../${tgtDir}/${jarName}.jar ${cList} )"
    echo "	\$(AT)\$(RM) ${fileListFile}"

if [  "${MAKE_PURE}" != "" -o "${MAKE_PURIFY}" != "" ]
then 
    if [  "${RATIONAL_DIR}" != "" ]
	then
	echo "	\$(AT)(cd ${tmpDestDir}; jar uf ../../${tgtDir}/${jarName}.jar com/rational )"
    fi
fi

    if [ "${debug}" != "" ]
	then
	echo "	\$(AT)cd ..; jar uf ${tgtDir}/${jarName}.jar ${sList}"
    fi

    if [ "${extraList}" != "" ]
	then
        echo "	\$(AT)jar uf ../${tgtDir}/${jarName}.jar ${extraList} "
    fi
   

else
    echo "../${tgtDir}/${jarName}.jar: "
    echo "	@echo \"== Making ${jarName}.jar\""
    echo "	@echo \" ${jarName}_DIRS is not defined. Nothing to do here.\""
    echo "	@echo \" Makefile should define the action for target  '${jarName}:'\""
fi

# ___oOo___
