#!/bin/sh
#
# $1 - destination directory of the file
# $2 - source file
# $3 - sub-module
# $4 - module name

. ./Source-Tree/SETUP/all-systems/libSETUP.sh

base="$3/`basename $2`"
makefile="$1/Makefile.in"

#echo "      Installing file : $base"

if [ ! -f "$1/$base" ]; then
  # we do not yet have a copy of the file linked in
  case "$base" in
    *.sh)
      echo "#!$UNIX_SHELL"		>  "$1/$base"
      echo "#"				>> "$1/$base"
      cat "$TOPDIR/$2"			>> "$1/$base"
      chmod 755 "$1/$base"
      ;;
    *)
      # create symbolic links
      # contributed by Michael Andrews
      
      UPDIR=`echo $1/$3  | sed -e 's:/\.$::'`
      UPDIR=`echo $UPDIR | sed -e "s:$TOPDIR::"`
      UPDIR=`echo $UPDIR | sed -e 's:/[^/]*:/\.\.:g'`
      UPDIR=`echo $UPDIR | sed -e 's/^\///'`
      
      ln -s $UPDIR/$2 $1/$3
      #ln -s "$TOPDIR/$2" "$1/$3"
      ;;
  esac
  
  # now we need to update the Makefile
  # what we actually do here is to determine what type of
  # file this is, and add it to the correct list
  # the main SETUP software will later parse these lists
  # in order to produce a working Makefile
  
  case "$base" in
    *.c)
      y="`basename $base .c`"
      echo "$y.o: $base"			>> $1/Makefile.c
#      echo "	@echo \"    Compiling $4/$base\""	>> $1/Makefile.c
      echo "	\$(CC) \$(CFLAGS) -c $base"	>> $1/Makefile.c
      echo					>> $1/Makefile.c
      sh_printf " $y.o"				>> $1/Makefile.objs
      ;;
    *.cc)
      y="`basename $base .cc`"
      echo "$y.o: $base"			>> $1/Makefile.cc
#      echo "	@echo \"    Compiling $4/$base\""	>> $1/Makefile.cc
      echo "	\$(CXX) \$(CXXFLAGS) -c $base"	>> $1/Makefile.cc
      echo					>> $1/Makefile.cc
      sh_printf " $y.o"				>> $1/Makefile.objs
      ;;
    *.conf)
      y="`basename $base .conf`"
      echo "$y.configure: FORCE"		>> $1/Makefile.conf
#      echo "	@echo \"    Configuring $4/$y\""	>> $1/Makefile.conf
      echo "	../SETUP/Configure.sh $base"	>> $1/Makefile.conf
      echo					>> $1/Makefile.conf
      sh_printf " $y.configure"			>> $1/Makefile.configure
      ;;
    *.inst)
      y="`basename $base .inst`"
      echo "$y.install: FORCE"			>> $1/Makefile.inst
#      echo "	@echo \"    Installing $4/$y\""	>> $1/Makefile.inst
      echo "	../SETUP/Configure.sh --no-write $base"	>> $1/Makefile.inst
      echo					>> $1/Makefile.inst
      sh_printf " $y.install"			>> $1/Makefile.install
      ;;
  esac
fi  
