#!/bin/csh -f
# configuration script for libs

echo 'Which cc do you want to use?'
echo '"gcc" is generally a good choice, if you have it.  Otherwise,'
echo 'the name is usually "cc".'
echo -n 'cc to use? '
set cc=$<

echo " "
echo 'Do you want to try to make the extra libraries?'
echo 'These are libraries that are not used by ic, so normally'
echo 'you would answer no to this question'
echo -n 'Make extra libs? (y/n) '
set extra=$<
cat /dev/null > config.h
cat /dev/null > makeconf

# ask user for serial port
echo " "
echo "Which default serial port would you like to use (form /dev/xxx) "
echo "SPARCS and NeXT might use /dev/ttya or /dev/ttyb"
echo "Linux might use /dev/cua0 /dev/cua1 ..."
echo "HP 7xx and DECstation might use /dev/tty00 or /dev/tty01"
echo "(Note that this can be overridden on the command line, and the "
echo " default can be changed by reconfiguring the executable, so what"
echo " you choose here isn't a big deal)."

echo -n 'Serial port? '
set serial_port = $<

echo " "
echo 'Do you want to use the debugging version of malloc?'
echo 'Typically you should say "no" unless you are trying to debug'
echo 'this software.  (Memory usage goes up and performance goes down.)'
echo -n 'Use debugging malloc? (y/n) '
set use_debug_malloc = $<

echo " "
echo 'Use optimization?'
echo 'Typically you should say "yes" unless you are trying to debug'
echo 'this software.'
echo -n 'Use optimization? (y/n) '
set use_optimization = $<

echo " "
echo "# makeconf" >> makeconf
echo "# makeconf"
echo "# generated by libs/config" >>makeconf
echo "# generated by libs/config"
echo "CC_NAME="$cc >>makeconf
echo "CC_NAME="$cc
if ($extra == 'y' || $extra == 'yes') then
  echo "OTHER_OBJS=other_objs" >>makeconf
  echo "OTHER_OBJS=other_objs"
else 
  echo "OTHER_OBJS=" >>makeconf
  echo "OTHER_OBJS="
endif
if ($use_optimization == 'y' || $use_optimization == 'yes') then
  echo "OPTIMIZATION=-O" >>makeconf
  echo "OPTIMIZATION=-O"
else
  echo "OPTIMIZATION=" >>makeconf
  echo "OPTIMIZATION="
endif

set foo=`which protos`
if ($#foo == 1) then
  echo 'Found "protos"'
  echo "PROTOS=protos" >>makeconf
else
  echo '"protos" not found'
  echo "PROTOS=echo" >>makeconf
endif

cat makeconf.in >>makeconf

echo "--- makeconf is done ---"

echo '/* config.h  generated by libs/config */' >> config.h
echo '/* config.h  generated by libs/config */'

# see which include files we have

grep HAS config2.c | awk '{print $2}' | sed s/HAS_// > config.files

set files=(`cat config.files`)

foreach file ($files)
  'rm' -f config2.o
  $cc -DHAS_$file -c config2.c -o config2.o >& /dev/null
  if (-e config2.o) then
    echo "#define HAS_"$file
    echo "#define HAS_"$file >> config.h
  else
    echo "#define NEEDS_"$file
    echo "#define NEEDS_"$file >> config.h
  endif
end

echo "#define DEFAULT_SERIAL_PORT "'"'$serial_port'"' >> config.h
echo "#define DEFAULT_SERIAL_PORT "'"'$serial_port'"'

if ($use_debug_malloc == 'y' || $use_debug_malloc == 'yes') then
  echo "#define MALLOC_DEBUG" >> config.h
  echo "#define MALLOC_DEBUG"
else
  echo "#undef MALLOC_DEBUG" >> config.h
  echo "#undef MALLOC_DEBUG"
endif

# see which functions we have

$cc -c config.c
$cc config.o -lm >& config.out

# create list of procedures found in config.c

grep ';' config.c | tr -d '();	 ' | tr '[a-z]' '[A-Z]' > config.procs

set procs=(`cat config.procs`)

foreach proc ($procs)
 set b = (`grep -i $proc config.out`)
 if ($#b) then
   echo "#define NEEDS_$proc"
   echo "#define NEEDS_$proc" >> config.h
 else
   echo "#define HAS_$proc"
   echo "#define HAS_$proc" >> config.h
 endif
end

# Find which prototypes might be necessary to define
# yuk.

grep HAS config3.c | awk '{print $2}' | sed s/HAS_// | sed s/_PROTO// > config.protos

set protos=(`cat config.protos`)

foreach proto ($protos)
  'rm' -f config3.o
  $cc -I. "-DHAS_"$proto"_PROTO" -c config3.c -o config3.o >& /dev/null
  if (-e config3.o) then
    echo "#define HAS_"$proto"_PROTO"
    echo "#define HAS_"$proto"_PROTO" >> config.h
  else
    echo "#define NEEDS_"$proto"_PROTO"
    echo "#define NEEDS_"$proto"_PROTO" >> config.h
  endif
end

echo "#define UNIX" >> config.h

echo '#define LIB_DIRECTORY "/usr/local/lib/"' >> config.h

echo ' '
echo 'Configuration is now complete.  You should probably look at config.h'
echo 'and makeconf to double-check things before trying to make.'
echo 'First, do a make in the current (libs) directory.  Next, you can'
echo 'try ic, dl, and mon in any order.'






