#!/bin/csh -f

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".'
set cc=$<

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'
set extra=$<
cat /dev/null > config.h
cat /dev/null > makeconf

# ask user for serial port

echo "Which serial port would you like to use (form /dev/xxx) "
echo "SPARCS and NeXT may use /dev/ttya /dev/ttyb"
echo "Linux might use /dev/ttyS0 /dev/ttyS1 ..."

set serial_port = $<

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


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'"'

# 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

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