#!/bin/sh

if [ $# -le 0 ]  ; then
  echo usage: ./add-defs machine-type;
  echo  or '  ' ./add-defs machine-type directory
  echo where directory might be '/usr/local' or '/public' or '/lusr' -- a place to find various local includes or libs
  echo see echo h/*.defs
exit 1 ; fi

if [ -f h/$1.defs ] ; then echo using $1.defs ;
  else echo h/$1.defs does not exist
  echo Build one or use one of `ls h/*.defs`
  exit 1
fi

echo $1 > machine


#  rm -f makedefs
#  echo > makedefs
#  echo "# begin makedefs" >> makedefs
#  echo "# constructed by ${USER} using: $0 $1 $2 $3 $4 $5" >> makdefs

  if [ -d ${PWD}/unixport ] ; 
   then  echo "GCLDIR=${PWD}" >> makedefs ;
   else echo "GCLDIR=`pwd`" >> makedefs ;
  fi
  echo "SHELL=/bin/sh" >> makedefs
  echo "MACHINE=$1"  >> makedefs


####machine specific .defs files may over ride the above####


####### insert the h/machine.defs file ############
  cat h/$1.defs >> makedefs
  if [ -f ${HOME}/local_gcl.defs ] ; then
    cat ${HOME}/local_gcl.defs >> makedefs
  fi
  echo "# end makedefs" >> makedefs
   

  echo inserting h/$1.defs in ..
  for v in makefile unixport/make_kcn */makefile ; 
  do
  echo " $v,"
  ./xbin/file-sub makedefs $v "# begin makedefs" "# end makedefs"
  mv tmpx $v
  done

echo ""

# Copy the config.h over.

cat h/$1.h > tmpx
if [ -f ${HOME}/local_gcl.h ] ; then
 cat  ${HOME}/local_gcl.h >> tmpx
fi

if fgrep =unknown makedefs > /dev/null ;
then echo " if the 'unknown' directories exist you may provide
a second argument to ./add-defs of a local directory where things might be,
or edit ./add-defs so that it can find them.   Otherwise just continue
and the portions with unknown will not be compiled."
fi


if cmp tmpx h/config.h  > /dev/null 2>&1 ;then true;
else
rm -f h/config.h
cp tmpx h/config.h
fi
rm -f tmpx

# machine specific stuff that cant be handled normally...
if [ -f ./xbin/$1-fix ] ; then ./xbin/$1-fix ; fi
