#!/bin/sh

if [ $# -le 0 ]  ; then echo usage: Provide a machine name as arg ; 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

#Usage: ./xbin/add-dir ITEM LIST-OF-DIRECTORIES  VARIABLE-SETTING-TEMPLATE

./xbin/add-dir tk.tcl "/usr/lib/tk ${PUBLIC}/lib/tk /usr/local/lib/tk /usr/lib/X11/tk" \
  'TK_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=-I$v' 

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

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

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

./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=-L$v -lX11'

./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'




####### 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 X11_INCLUDE_DIR=-Iunknown makedefs > /dev/null ;
then true;
else
echo "#define HAVE_X11" >> tmpx
fi

if cmp tmpx h/config.h 2>1 > /dev/null ;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


