#! /bin/sh
# $Id: g++dep 80826 2008-03-04 14:51:23Z wotte $

# This utility is a lightly editted version of the freed Berkeley
# script `mkdep'.  The current script is intended to work for GNU G++.

# Here is the original BSD header:
#       @(#)mkdep.sh    1.7     (Berkeley)      10/13/87
#

if [ $# = 0 ] ; then
  echo 'usage: g++dep [-p] [-f makefile] [flags] file ...'
  exit 1
fi

DO_ACE_MAKE_DEPEND=0
MAKE=GNUmakefile
STOPNOW=0
REL=""

while [ $STOPNOW -eq 0 ]
do
case $1 in
  # -e for compatibility with depgen.pl
  -e) shift; shift ;;

  # -f allows you to select a makefile name
  -f) MAKE=$2
      shift; shift ;;

  # the -p flag produces "program: program.c" style dependencies
  # so .o's don't get produced
  -p) SED='s;\.o;;'
      shift ;;

  # -A implies -r and fixes the .obj line, hate 
  -A) REL="ACE_ROOT TAO_ROOT "$REL
      DO_ACE_MAKE_DEPEND=1
      shift ;;

  # -r allows the use of relative pathnames...
  -r) REL="ACE_ROOT TAO_ROOT "$REL
      shift ;;

  # -R VARNAME allows you to specify a variable which should be used
  #    to generate relative paths if it's defined.  You can use multiple
  #    -R options, but be careful if one of the values is a proper
  #    subset of a subsequent value, because I suspect that sed will
  #    substitute for the first value properly, but not for the
  #    second.  You might be able to get around this by reordering and
  #    having the more specific values lead the less specific values.
  -R) REL=$2" "$REL
      shift; shift;;
  *) STOPNOW=1
esac
done

if [ ! -w $MAKE ]; then
  echo "g++dep: no writeable file \"$MAKE\""
  exit 1
fi

TMP=/tmp/g++dep$$
SCRIPT=${TMP}_script

trap 'rm -f $TMP $SCRIPT; exit 1' 1 2 3 13 15

cp $MAKE ${MAKE}.bak

sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP

cat << _EOF_ >> $TMP
# DO NOT DELETE THIS LINE -- g++dep uses it.
# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.

_EOF_

# Local files may appear as './foo' change that to 'foo'
echo 's; \./; ;g' >$SCRIPT

# If the -p flag is set we want to change 'foo.o' to simply 'foo'
echo $SED >>$SCRIPT

# Dependencies on local files are better expressed like that, instead
# of using $(TAO_ROOT) or $(ACE_ROOT).  This is specially important
# for IDL generated files.
echo "s;`pwd`/;;g" >>$SCRIPT

if [ -z "$TAO_ROOT" ]; then
  TAO_ROOT=${ACE_ROOT}/TAO
fi

# This is a long series of commands to change the actual value of
# $ACE_ROOT to '$(ACE_ROOT)', similar changes are done for TAO_ROOT
# and any number of "variables" defined via the -R option.
for varname in $REL; do
  varvalue=$(eval echo \$${varname})
  echo "s;"$varvalue";$""("$varname");g" >>$SCRIPT
done

if [ $DO_ACE_MAKE_DEPEND -eq 1 ]; then
  # Append a series of commands to the sed script that help with the
  # ACE build style (.obj subdirectories, plaform indenpendent
  # dependencies, etc.)

  # To avoid interpolation we build this string in pieces, the idea is
  # to generate a rule that will convert
  #    foo.o:
  # into
  #    .obj/foo.o .shobj/foo.o .obj/foo.so .shobj/foo.so:
  # 
  # will be foo.o foo.
  LONG_TARGET="$""(sort "
  for i in VDIR VSHDIR; do
    for j in OBJEXT SOEXT; do
      LONG_TARGET=${LONG_TARGET}"$""("${i}")\1.$""("${j}") "
    done
  done
  LONG_TARGET=${LONG_TARGET}")"

  cat >>$SCRIPT <<EOF
#
# Change the actual plaform config.h file to a MAKE macro...
s;${ACE_PLATFORM_CONFIG};\$(ACE_PLATFORM_CONFIG);g
#
# Append a 'x' character to the config-all and config-lite names,
# because we are going to remove all the config-* names..
s/config-all/configx-all/
s/config-lite/configx-lite/
#
# Remove the config-* names
/config-.*\.h/d
#
# Restore configx-all and configx-lite to their original names
s/configx-all/config-all/
s/configx-lite/config-lite/
#
# Remove any absolute dependencies
s; /[-a-zA-Z0-9_./+]*\.h;;g
#
# Remove blanks followed by a backslash
s;[ \\t][ \\t]*\\\\; \\\\;g
#
# g++ generate dependencies for foo.o in the current directory, but we
# we need dependencies for foo.o and foo.so in the .obj and .shobj
# subdirectories.  Actually .obj and .shobj are, respectively, the
# expansions of VDIR and VSHDIR, therefore it is better *NOT* to use
# the values of said variables, but generated dependencies that expand
# them.
s;\([-a-zA-Z0-9._+]*\)\.o:;\\${LONG_TARGET}:;
#
# An older implementation of the previous code, but using the actual
# value of VDIR and VSHDIR at time of dependency generation.
#
#s;\([-a-zA-Z0-9._+]*\)\.o:;\\${VDIR}\1.${OBJEXT} ${VDIR}\1.${SOEXT} ${VSHDIR}\1.${OBJEXT} ${VSHDIR}\1.${SOEXT}:;
#
#
EOF
fi

g++ -MM -MG -DACE_LACKS_PRAGMA_ONCE $* |
    sed -f $SCRIPT ${ACE_DEPEND_SED_CMD} >>$TMP
/bin/rm -f $SCRIPT

cat << _EOF_ >> $TMP

# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
_EOF_

# copy to preserve permissions
cp $TMP $MAKE
rm -f ${MAKE}.bak $TMP
exit 0