#! /bin/sh
#
# $Id: mkdep,v 1.1 1997/05/22 22:01:44 ettore Exp $
#
# This is a simple script to build the dependencies.  They are put at
# the end of Makefile.in, after a magic string.  The Makefile should
# be rebuilt (using configure) afterwards.  
#
# The arguments are a list of files:
#
#  *.c files are handled by calling cpp as usual
#
#  *_VIC20.o files are considered VIC20-specific objects and create
#    the dependence with the corresponding .c file with `VIC20' defined
#
#  *_PET.o files are considered VIC20-specific objects and create
#    the dependence with the corresponding .c file with `PET' defined
#
#  other files are ignored
#
# It only works if the object files are in the same directory
# as the sources.
#

# Magic string: dependencies are put after this line.
magic="# DO NOT DELETE"

# Makefile name
makefile="Makefile.in"

# Put the magic string at the very start of the new dependencies.
echo $magic > .depend

for a in $* ; do

  case $a in

    *.c )
      # The sed command is to remove the initial `./' from source file
      # names.
      ( cc -E -MM $a | sed 's/\.\///' ) >> .depend
      continue ;;
    
    *_VIC20.o )
      src=`echo $a | sed 's/\(.*\)_VIC20\.o/\1.c/'`
      if [ ! -e $src ] ; then
	continue
      fi
      defines="-DVIC20" ;;

    *_PET.o )
      src=`echo $a | sed 's/\(.*\)_PET\.o/\1.c/'`
      if [ ! -e $src ] ; then
        continue
      fi
      defines="-DPET" ;;

    * )
      continue ;;
          
  esac

  # Build the dependence correcting the object file name with the
  # correct one.
  obj=`echo $src | sed 's/\.c/.o/'`
  ( cc $defines -E -MM $src | sed "s/$obj/$a/" ) >> .depend
    
done

# The .depend file has been built, now create the new makefile.

mv $makefile ${makefile}.old

# Remove everything from the magic string to the end of the file,
# and add the new dependencies to the end.
( sed -e "/^$magic/,\$d" < ${makefile}.old | cat - .depend ) > $makefile

rm -f .depend
