#!/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

# a place where you keep local things. Changing this may help to
# find things, otherwise edit the "LIST-OF-DIRECTORIES" for the
# given item.

if [ "$2x" != "x" ] ; then
PUBLIC=$2 ;
else
PUBLIC=/public
fi
export PUBLIC



TK_XINCLUDES=-Iunknown
# `add-dir'  searches for ITEM in LIST-OF-DIRECTORIES and then sets the
# directory in VARIABLE-SETTING-TEMPLATE
#Usage: ./xbin/add-dir ITEM LIST-OF-DIRECTORIES  VARIABLE-SETTING-TEMPLATE

./xbin/add-dir tkConfig.sh "${PUBLIC}/lib /usr/lib /usr/local/lib" 'TK_CONFIG_PREFIX="$v"'
./xbin/add-dir tclConfig.sh "${PUBLIC}/lib /usr/lib /usr/local/lib" 'TCL_CONFIG_PREFIX="$v"'
./xbin/add-dir dir "/usr/local/lib/info ${PUBLIC}/lib/info /usr/lib/info" 'INFO_DIR="$v"'

SOURCE=.
${SOURCE} makedefs

  

if [ -f ${TK_CONFIG_PREFIX}/tkConfig.sh -a \
           -f ${TCL_CONFIG_PREFIX}/tclConfig.sh ] ; then
 ${SOURCE} ${TK_CONFIG_PREFIX}/tkConfig.sh ;
 ${SOURCE}  ${TK_CONFIG_PREFIX}/tclConfig.sh ;
 ./xbin/add-dir tk.h "${PUBLIC}/include /usr/include  /usr/local/include" 'TK_INCLUDE="-I$v"'

 echo "TK_VERSION=${TK_VERSION}" >> makedefs
 echo "TCL_VERSION=${TCL_VERSION}" >> makedefs
 echo "TK_LIB_SPEC=${TK_LIB_SPEC}" >> makedefs
 echo "TK_LIBRARY=${TK_CONFIG_PREFIX}/tk${TK_VERSION}" >> makedefs
 echo "TCL_LIBRARY=${TCL_CONFIG_PREFIX}/tcl${TCL_VERSION}" >> makedefs
 echo "TK_BUILD_LIB_SPEC=${TK_BUILD_LIB_SPEC}" >> makedefs
 echo "TK_XLIBSW=${TK_XLIBSW}" >> makedefs
 TK_XLIB_DIR=`echo ${TK_XLIBSW} | sed  "s:-L\\([^ ]*\\) .*:\\1:g"`
 echo "TK_XLIB_DIR=${TK_XLIB_DIR}" >> makedefs 
 echo "TK_XINCLUDES=${TK_XINCLUDES}" >> makedefs
 echo "TCL_LIB_SPEC=${TCL_LIB_SPEC}" >> makedefs
 echo "TCL_DL_LIBS=${TCL_DL_LIBS}" >> makedefs
 echo "TCL_LIBS=${TCL_LIBS}" >> makedefs
 echo "HAVE_X11=-DHAVE_X11" >> makedefs
else
 echo "TK_CONFIG_PREFIX=unknown" >> makedefs
 ./xbin/add-dir X11/Xos.h "${PUBLIC}/include /usr/include /usr/local/X11R6/include /usr/local/X11/include " 'TK_XINCLUDES="-I$v"' 
  . makedefs
  if [ "$TK_XINCLUDES" = "-Iunknown" ] ; then
    echo "cant find X11 includes so not defining HAVE_X11"
    else
    echo HAVE_X11=-DHAVE_X11 >> makedefs
  fi
 echo unable to find tkConfig.sh and tclConfig.sh so not configuring tcl/tk
fi 


####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,"
  ./bin/file-sub makedefs $v "# begin makedefs" "# end makedefs" tmpx
  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


