#!/bin/sh -f

comp=$1

lib=static

uname -s > tmp.out
read sys < tmp.out
uname -r > tmp.out
read ver < tmp.out
rm -f tmp.out
       
if [ $sys = SunOS ]
then           
   case $ver in          
   4*) sys=SunOS4;;
   5*) sys=SunOS5;;
   esac
fi

if [ "$1" = "" ]
then 
   echo " "
   echo "usage: lconfig  compiler"
   echo " "
   echo "Known Compilers:"
   ls confdir/unix/cc/static
   echo " "
   exit
fi
 
if [ "$1" = "CC" ]
then
echo Searching for compiler \"CC\" ... 
cp confdir/unix/cctest.c .
if CC -o cctest cctest.c
then
   ./cctest > tmp.out
   read comp < tmp.out
   rm -f tmp.out
   echo "ok, $comp"
else
   echo "Sorry, no \"CC\" not found."
   exit
fi
fi

if [ "$2" = "shared" ]
then
   if [ $sys = SunOS5 -o $sys = Linux  -o $sys = win32 ]
   then 
      lib=shared
   else
      echo "Sorry, can make shared libraries only under Solaris (CC,g++) or Linux (g++)."
      exit
   fi
else 
   if [ "$2" = "static" -o "$2" = "" ]
   then 
      lib=static
   else 
      echo usage: lconfig  compiler [ shared \| static ]
      echo " "
      exit
   fi
fi

echo " "
echo Configuring makefiles for $comp and $lib libraries under $sys $ver.


rm -f cctest.c
rm -f cctest
rm -f src/Make.src
rm -f src/MakeOpt.src
rm -f demo/Make.demo
rm -f test/Make.pro


if [ -f confdir/unix/cc/$lib/$comp ]
then 
  cp confdir/unix/cc/$lib/$comp src/Make.src
  cp confdir/unix/cc/$lib/$comp src/MakeOpt.src
  cp confdir/unix/cc/$lib/$comp demo/Make.demo
  cp confdir/unix/cc/$lib/$comp test/Make.pro
else 
  echo " "
  echo "WARNING: $comp not found in the list of known compilers,"
  echo "         you might have to edit the Makefile."
  echo "         (lconfig without arguments prints the list)"
  echo " "
  echo "CC = $comp" >> src/Make.src
  echo "LD = $comp" >> src/Make.src
  echo "CC = $comp" >> src/MakeOpt.src
  echo "LD = $comp" >> src/MakeOpt.src
  echo "CC = $comp" >> test/Make.pro
  echo "LD = $comp" >> test/Make.pro
  echo "CC = $comp" >> demo/Make.demo
  echo "LD = $comp" >> demo/Make.demo
fi


# Locating X11 directories

if [ -d /usr/openwin ]
then
   x11dir=/usr/openwin
elif [ -d /usr/X11 ]
then
   x11dir=/usr/X11
elif [ -d /usr/X11R5 ]
then
   x11dir=/usr/X11R5
elif [ -d /usr/X11R6 ]
then
   x11dir=/usr/X11R6
fi

if [ "$x11dir" = "" ]
then
   echo ""
   echo Assuming X11 headers in /usr/include/X11 and libraries in /usr/lib.
   echo ""
else
   echo ""
   echo Using X11 from $x11dir.
   echo "XLIB_PATH = -L$x11dir/lib"  >> demo/Make.demo
   echo ""                           >> demo/Make.demo
   echo "XLIB_PATH = -L$x11dir/lib"  >> test/Make.pro
   echo ""                           >> test/Make.pro
fi


if [ -x /bin/csh ]
then 
    /bin/csh -f set_paths.csh
elif [ -x /bin/tcsh ]
then 
    /bin/tcsh -f set_paths.csh
elif [ -x /usr/bin/csh ]
then 
    /usr/bin/csh -f set_paths.csh
elif [ -x /usr/bin/tcsh ]
then 
    /usr/bin/tcsh -f set_paths.csh
fi


cat src/MakeOpt.src


if [ -x /bin/ranlib -o -x /usr/bin/ranlib ]
then 
  echo "RAN = ranlib" >> src/Make.src
  echo "RAN = ranlib" >> src/MakeOpt.src
  echo "use ranlib"
else 
  echo "RAN = @touch"  >> src/Make.src
  echo "RAN = @touch"  >> src/MakeOpt.src
  echo "no ranlib"
fi


if [ $lib = shared  -a $sys != win32 ]
then
   echo 'LIB = $(DLIB)' >> src/Make.src
   echo 'LIB = $(DLIB)' >> src/MakeOpt.src
else
   echo 'LIB = $(SLIB)' >> src/Make.src
   echo 'LIB = $(SLIB)' >> src/MakeOpt.src
fi

if [ $sys = SunOS5 ]
then
   echo "SOL = -lsocket -lthread" >> demo/Make.demo
   echo "SOL = -lsocket -lthread" >> test/Make.pro
fi

cat confdir/unix/Make.src >> src/Make.src
cat confdir/unix/MakeOpt.src >> src/MakeOpt.src
cat confdir/unix/Make.demo >> demo/Make.demo
cat confdir/unix/Make.pro >> test/Make.pro

echo " "
echo "Configuration done. Type make to make the library and the demos."
echo " "

