#! /bin/bash
#*******************************************************************************
# E.S.O. - ALMA project
#
# "@(#) $Id: acsMakeRTAIDependencies,v 1.31 2009/12/11 07:25:05 mzampare Exp $"
#
#************************************************************************
#   NAME
#   acsMakeRTAIDependencies - create the makefile to build RTAI Module
# 
#   SYNOPSIS
#
#   acsMakeRTAIDependencies 
# 
#   DESCRIPTION
#   Utility used by acsMakefile to create the makefile to build a RTAI 
#   Module.
#   It is not intended to be used as a standalone command.
#
#
#   (1) see also GNU Make 3.64, 4.3.5 pag 37
#
#
#   The .drtm itself depends from 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    
#
#----------------------------------------------------------------------

modName=$1
objectList=$2
debug=$3
VLTTOP=$4
# INST_SUBFOLDER=`egrep --regexp="define[[:space:]]+UTS_RELEASE" $LINUX_HOME/include/linux/* | cut --delimiter="\"" --fields=2`



destDir=../object/${modName}/
OWNER=root
GROUP=root

VER=`uname -r`
INSTLKM=../bin/installLKM-${modName}
if [ -f load${modName}.cpp -a  -f unload${modName}.cpp  ]
then
   AUXPROG=1
else
   AUXPROG=0
fi

echo "# Dependency file for program: ${modName}"
echo "# Created automatically by acsMakeRTAIDependencies -  `date '+%d.%m.%y %T'`"
echo "# DO NOT EDIT THIS FILE"
echo ""


if [ "${objectList}" != "" ]
then
    echo "../object/${modName}.drtm: Makefile"
    echo ""
    # BROKEN, START
    if [ `echo $modName | sed 's/\ //g'` == `echo $objectList | sed 's/\ //g'` ]
	then
	components=""
	else
	components="${modName}-objs := "
	for member in ${objectList}
	  do
	  components="$components $member.o"
	done
    fi

    # setting the USR_INC according to our paths
    USR_INC="-I\$(RTAI_HOME)/include -I${PWD}/../include -I\$(INTROOT)/include" 
    for intArea in `echo $INTLIST | sed 's/:/ /g'`
      do
      USR_INC="$USR_INC -I$intArea/include"
    done
    USR_INC="$USR_INC -I\$(ACSROOT)/include" 

############# ADDITIONS FOR KERNEL 2.6 ###################
    echo "RTAI_CONFIG := \$(RTAI_HOME)/bin/rtai-config" 
    echo "KDIR := \$(shell \$(RTAI_CONFIG) --linux-dir)" 
# why should the following be needed 
    echo "PWD :=\$(shell pwd)"   
    echo "CC:=\$(shell \$(RTAI_CONFIG) --cc)" 
    echo "USR_INC := $USR_INC"
    echo "EXTRA_CFLAGS := \$(shell \$(RTAI_CONFIG) --module-cflags) -Werror-implicit-function-declaration \$(USR_INC) \$(USER_RTAI_CFLAGS) -DRTAI_HOME" 
##########################################################
    
    echo "$components" >> Kbuild
    echo "obj-m += ${modName}.o" >> Kbuild
    
    echo ""
    echo "xyz_${modName}_SRC = \\"
    for member in ${objectList}
      do
      echo "                 ${member}.c \\"
    done
    echo ""
    
    echo "../rtai/\$(rtai_install_subfold)/${modName}.ko: \$(xyz_${modName}_SRC) ${INSTLKM}"
    echo "	@echo \"== Making RTAI Module: ${modName}\" "
    echo "# here we have to generate the hineous Kbuild file"
    echo "	\$(AT)echo \"obj-m += ${modName}.o\" > Kbuild"
    # this part subject to scrutiny
    echo "	\$(AT)echo \"$components\" >> Kbuild"
    #
    echo "	\$(AT)echo \"USR_INC := $USR_INC\"   >> Kbuild"
    echo "	\$(AT)echo \"EXTRA_CFLAGS := \$(shell \$(RTAI_CONFIG) --module-cflags) -Werror-implicit-function-declaration \$(USR_INC) \$(USER_RTAI_CFLAGS) -DRTAI_HOME\" >> Kbuild"
    echo "ifdef MAKE_VERBOSE"
    echo "	\$(AT)make -C \$(KDIR) CC=\$(CC) ARCH=i386 RTAI_CONFIG=\$(RTAI_CONFIG) M=\$(PWD) V=2 modules"
    echo "else"
    echo "	\$(AT)make -C \$(KDIR) CC=\$(CC) ARCH=i386 RTAI_CONFIG=\$(RTAI_CONFIG) M=\$(PWD) V=0 modules"
    echo "endif"
    echo "	\$(AT)rm Kbuild"
    echo "	\$(AT)mv ${modName}.ko ../rtai/\$(rtai_install_subfold)"
    echo ""
    echo "# LKM Support binaries"
    echo ""
# clean target
    echo "clean${modName}:"
    echo "	\$(AT)make -C \$(KDIR) CC=\$(CC) ARCH=i386 RTAI_CONFIG=\$(RTAI_CONFIG) M=\$(PWD) clean"
    echo "	\$(AT)rm ../rtai/\$(rtai_install_subfold)/${modName}.ko"
    echo ""
    

    if [ $AUXPROG = "1" ] 
	then
	echo "${INSTLKM}: ../bin/load${modName} ../bin/unload${modName}"
	echo "	@echo \"== LKM: ${modName} support scripts built, now proceeding to installLKM\""
	echo ""
    else
	echo "${INSTLKM}: "
    fi

    if [ -d ${VLTTOP}/rtai ]
	then
	OWNER=${USER}
	GROUP=`groups | awk '{print $1}'`
	DEST=${VLTTOP}/rtai/${rtai_install_subfold}
    else
	echo "	@echo \"echo 'WARNING: missing ACSROOT/INTROOT installation dir'\" > ${INSTLKM}"
	DEST=/lib/modules/${VER}/alma
    fi
    echo "	@echo \"echo 'installing ${modName} into ${DEST}..'\" > ${INSTLKM}"

    if [ $AUXPROG = "1" ] 
	then
	echo "	@echo \"cp `pwd`/../bin/load${modName} ${VLTTOP}/bin \" >> ${INSTLKM}"
	echo "	@echo \"cp `pwd`/../bin/unload${modName} ${VLTTOP}/bin \" >> ${INSTLKM}"
	echo "	@echo \"chmod ${P755} ${VLTTOP}/bin/load${modName} ${VLTTOP}/bin/unload${modName}\" >> ${INSTLKM}"
    fi
# create the RTAI directory in the destination area if it doesn' exist yet
#
    echo "	@echo \"if [ ! -d ${DEST} ]; then mkdir ${DEST}; fi\" >> ${INSTLKM}" 
    echo "	@echo \"install -d ${DEST}\" >> ${INSTLKM}" 
    echo "	@echo \"install -m ${P644} -c `pwd`/../rtai/\$(rtai_install_subfold)/${modName}.ko ${DEST}\" >> ${INSTLKM}"
#    echo "	@echo \"echo 'running depmod..'\" >> ${INSTLKM}"
#    echo "	@echo \"/sbin/depmod\" >> ${INSTLKM}"
    if [ $AUXPROG = "1" ] 
	then
	echo "	@echo \"echo 'setting uid permissions and ownership..'\" >> ${INSTLKM}"
	echo "	@echo \"chown root:root ${VLTTOP}/bin/load${modName}\" >> ${INSTLKM}"
	echo "	@echo \"chmod u+s ${VLTTOP}/bin/load${modName}\" >> ${INSTLKM}"
	echo "	@echo \"chown root:root ${VLTTOP}/bin/unload${modName}\" >> ${INSTLKM}"
	echo "	@echo \"chmod u+s ${VLTTOP}/bin/unload${modName}\" >> ${INSTLKM}"
    fi
    echo "	@chmod a+x ${INSTLKM}"
    echo ""
    echo ""
#
#   LOAD binary
    echo "load${modName}_NOSHARED=on"
    echo "../object/load${modName}.dx:" 
    echo "	@vltMakeExecutableDependencies load${modName} \"load${modName}\" \"\" \"LKM C++\" \"\" > ../object/load${modName}.dx" 
    echo "-include ../object/load${modName}.dx" 
#
#   UNLOAD binary
    echo "unload${modName}_NOSHARED=on"
    echo "../object/unload${modName}.dx:" 
    echo "	@vltMakeExecutableDependencies unload${modName} \"unload${modName}\" \"\" \"LKM C++\" \"\" > ../object/unload${modName}.dx" 
    echo "-include ../object/unload${modName}.dx" 
else
    echo "../rtai/\$(rtai_install_subfold)/${modName}.ko: "
    echo "	@echo \"== Making RTAI Module: ${modName}\" "
    echo "	@echo \" ERROR: ${modName}_OBJECTS is not defined. Nothing to do here.\""
    echo "	@echo \" Makefile should define the action for target  '${modName}'\""
fi

# ___oOo___
    