#!/bin/sh

if [ $# -le 0 ]  ; then
  echo usage: ./add-defs machine-type;
  echo  or '  ' ./add-defs machine-type directory
  echo where directory is a place to find various local includes or libs
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
  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

# `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 tk.tcl "${PUBLIC}/lib/tk /usr/lib/tk /usr/local/lib/tk /usr/lib/X11/tk" \
  'TK_LIBRARY=$v'

./xbin/add-dir 'init.tcl' " ${PUBLIC}/lib ${PUBLIC}/lib/tcl /usr/lib/tcl /usr/local/lib/tcl  " 'TCL_LIBRARY=$v' 

./xbin/add-dir X11/Xlib.h "/usr/include ${PUBLIC}/X11/include /usr/X11/include /usr/X11R6/include /usr/openwin/include" 'X11_INCLUDE_DIR=$v' 

./xbin/add-dir tk.h "/usr/include/tcl ${PUBLIC}/include/tcl ${PUBLIC}/include /usr/local/include /usr/local/include/tcl  /usr/include  " 'TK_INCLUDE_DIR=$v'

./xbin/add-dir 'libtk.*' "/usr/lib /usr/X11/lib ${PUBLIC}/X11/lib /usr/X11R6/lib  ${PUBLIC}/lib /usr/local/lib" 'TK_LIBS_DIR=$v'

./xbin/add-dir 'libtcl.*' "/usr/lib /usr/X11/lib /usr/X11R6/lib ${PUBLIC}/X11/lib ${PUBLIC}/lib /usr/local/lib " 'TCL_LIBS_DIR=$v'

./xbin/add-dir 'libX11.*' "/usr/openwin/lib /usr/lib  /usr/X11/lib /usr/X11R6/lib ${PUBLIC}/X11/lib  ${PUBLIC}/lib /usr/local/lib " \
 'X11_LIBS_DIR=$v'

./xbin/add-dir '*.el*' "/usr/lib/emacs/site-lisp /usr/local/lib/emacs/site-lisp ${PUBLIC}/lib/emacs/site-lisp ${PUBLIC}/X11/lib " 'EMACS_SITE_LISP=$v'

./xbin/add-dir 'dir' "/usr/info /usr/local/info ${PUBLIC}/info " 'INFO_DIR=$v'

echo 'TK_INCLUDE=-I${TK_INCLUDE_DIR}' >> makedefs
echo 'X11_INCLUDE=-I${X11_INCLUDE_DIR}' >> makedefs
echo 'TCL_LIBS=-L${TCL_LIBS_DIR} -ltcl'  >> makedefs
echo 'TK_LIBS=-L${TK_LIBS_DIR} -ltk'  >> makedefs
echo 'X11_LIBS=-L${X11_LIBS_DIR} -lX11'  >> 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 fgrep X11_INCLUDE_DIR=unknown makedefs > /dev/null ;
then true;
else
echo "#define HAVE_X11" >> tmpx
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


