#!/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 [ static  | shared ]"
   echo " "
   echo "Known Compilers:"
   ls confdir/unix/cc/static
   echo " "
   echo "(shared libraries for Solaris (g++,sunpro) and Linux (g++) only)"
   echo " "
   exit
fi


if [ "$2" = "shared" ]
then
   if [ $sys = SunOS5 -o $sys = Linux ]
   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.

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

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


if [ $lib = shared -a $sys = Linux ]
then 
    comp=g++.linux
fi


if [ -f confdir/unix/cc/$lib/$comp ]
then 
  cp confdir/unix/cc/$lib/$comp src/Make.src
  cp confdir/unix/cc/$lib/$comp prog/Make.pro
  cp confdir/unix/cc/$lib/$comp test/Make.pro
  cp confdir/unix/cc/$lib/$comp demo/Make.pro
else 
  echo " "
  echo "WARNING: $comp not found in the list of known compilers,"
  echo "         you might have to edit the <LEDA/system.h> file."
  echo "         (lconfig without arguments prints the list)"
  echo " "
  echo "CC = $comp" >> src/Make.src
  echo "LD = $comp" >> src/Make.src
  echo "CC = $comp" >> prog/Make.pro
  echo "LD = $comp" >> prog/Make.pro
  echo "CC = $comp" >> test/Make.pro
  echo "LD = $comp" >> test/Make.pro
  echo "CC = $comp" >> demo/Make.pro
  echo "LD = $comp" >> demo/Make.pro
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
   if [ -d /usr/include/X11R6 ]
   then
      x11incdir=/usr/include/X11R6
   elif [ -d /usr/include/X11R5 ]
   then
      x11incdir=/usr/include/X11R5
   else
      x11incdir=/usr/include
   fi

   if [ -d /usr/lib/X11R6 ]
   then
      x11libdir=/usr/lib/X11R6
   elif [ -d /usr/lib/X11R5 ]
   then
      x11libdir=/usr/lib/X11R5
   else
      x11libdir=/usr/lib
   fi
else
   x11incdir=$x11dir/include
   x11libdir=$x11dir/lib
fi


if [ "$x11incdir" != "" ]
then
echo ""
echo using X11 headers from $x11incdir
echo "DFLAGS = -I$x11incdir"  > make.tmp
echo ""                      >> make.tmp
cp make.tmp make1.tmp
fi

if [ "$x11libdir" != "" ]
then
echo using X11 libraries in $x11libdir
echo "XLIB_PATH = -L$x11libdir"  >> prog/Make.pro
echo ""                          >> prog/Make.pro
echo "XLIB_PATH = -L$x11libdir"  >> test/Make.pro
echo ""                          >> test/Make.pro
echo "XLIB_PATH = -L$x11libdir"  >> demo/Make.pro
echo ""                          >> demo/Make.pro
fi;


cat src/x11/Makefile   >> make.tmp
rm src/x11/Makefile
mv make.tmp src/x11/Makefile

cat src/all/Makefile   >> make1.tmp
rm src/all/Makefile
mv make1.tmp src/all/Makefile


cat src/Make.src


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


#cp src/Make.src  sources/Makefile
#echo ""                      >> sources/Makefile
#echo "XFLAGS = -I$x11incdir" >> sources/Makefile
#echo ""                      >> sources/Makefile


if [ "$lib" = "shared" ]
then
   echo 'LIB = $(DLIB)' >> src/Make.src
#   cat confdir/unix/Make.src2  >> sources/Makefile
else
   echo 'LIB = $(SLIB)' >> src/Make.src
#   cat confdir/unix/Make.src1  >> sources/Makefile
fi

cat confdir/unix/Make.src    >> src/Make.src


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

cat confdir/unix/Make.pro >> prog/Make.pro
cat confdir/unix/Make.pro >> test/Make.pro
cat confdir/unix/Make.pro >> demo/Make.pro

