#!/bin/sh
# simple configure script to locate system-libraries.
# 20.12.97 Thomas Niederreiter
# 6.6.98 Changed for S.u.S.E. by Michael Gernoth (based on S.u.S.E. diff)
# 25.11.98 tn: allow suse-patch to be disabled with --nosuse

BASEDIR="/usr/local/lib"
BASEDIR2="/usr/lib"
INCDIR="/usr/local/include"
INCDIR2="/usr/include"
LIBDIRPREFIX="/usr/local"
SUSE="no"

if test "$1" = --help; then 
	echo
	echo "Available configure-options:"
	echo "	--prefixdir <directoryname> (default: $LIBDIRPREFIX)"
	echo "	--nosuse : will override SuSE-Distribution autodetect"
	echo
	exit 1
fi

if test "$1" = --prefixdir; then
	if test -z "$2"; then
		echo
		echo "Warning: Need value for --prefixdir"
		echo
		exit 1
	else 
		LIBDIRPREFIX=$2
		shift 2
	fi
fi

if test "$1" = --nosuse; then
	NOSUSE=1
	SUSE="overriden"
else
	NOSUSE=0
fi
	
echo
echo "Autoconfiguration of X-CD-Roast 0.96e:"
echo

echo -n "Checking for S.u.S.E.-Distribution: "
if [ -f "/sbin/SuSEconfig" -a "$NOSUSE" = "0" ]; then
  SUSE="yes"
  BASEDIR="/usr/X11R6/lib"
  LIBDIRPREFIX="/usr/X11R6"
fi
echo $SUSE
echo

if [ "$SUSE" = "yes" ]; then
  echo "Copying /usr/X11R6/include/tk8.0.h to /usr/X11R6/include/tk.h"
  cp /usr/X11R6/include/tk8.0.h /usr/X11R6/include/tk.h
  echo "Copying /usr/include/tcl8.0.h to /usr/include/tcl.h" 
  cp /usr/include/tcl8.0.h /usr/include/tcl.h
  echo "Copying /usr/X11R6/lib/tkConfig8.0.sh to /usr/X11R6/lib/tkConfig.sh" 
  cp /usr/X11R6/lib/tkConfig8.0.sh /usr/X11R6/lib/tkConfig.sh

  if [ "$?" != "0" ]; then
	echo 
	echo "Error copying files: You have to be root to do this, or"
	echo "you dont have the tcl/tk/tix-packages installed"
	echo "(if you compiled and installed tcl/tk/tix yourself you have"
	echo " start ./configure with the --nosuse option)"
	exit
  fi

  echo
fi

echo -n "Looking for TCL 8.0-Library: "
DYNTCLLIBDIR=""
STATCLLIBDIR=""
if [ "$SUSE" = "yes" ]; then
  DYNTCLLIBDIR="/usr/lib"
  STATCLLIBDIR="/usr/lib"
fi
FND=""
if test -f $BASEDIR/libtcl8.0.so; then
	echo "$BASEDIR/libtcl8.0.so (shared)"
	DYNTCLLIBDIR=$BASEDIR
	FND=1
elif test -f $BASEDIR2/libtcl8.0.so; then
	echo "$BASEDIR2/libtcl8.0.so (shared)"
	DYNTCLLIBDIR=$BASEDIR2
	FND=1
fi

if test -f $BASEDIR/libtcl8.0.a; then
	if test -n "$FND"; then echo -n "                             "; fi
	echo "$BASEDIR/libtcl8.0.a (static)"
	STATCLLIBDIR=$BASEDIR
elif test -f $BASEDIR2/libtcl8.0.a; then
  	if test -n "$FND"; then echo -n "                             "; fi
  	echo "$BASEDIR2/libtcl8.0.a (static)"
  	STATCLLIBDIR=$BASEDIR2
fi

if test -z "$DYNTCLLIBDIR" -a -z "$STATCLLIBDIR"; then
	echo "not found"
fi

echo -n "Looking for TK 8.0-Library: "
DYNTKLIBDIR=""
STATKLIBDIR=""
if [ "$SUSE" = "yes" ]; then
  DYNTKLIBDIR="/usr/X11R6/lib"
  STATKLIBDIR="/usr/X11R6/lib"
fi
FND=""
if test -f $BASEDIR/libtk8.0.so; then
  	echo "$BASEDIR/libtk8.0.so (shared)"
  	DYNTKLIBDIR=$BASEDIR
  	FND=1
elif test -f $BASEDIR2/libtk8.0.so; then
  	echo "$BASEDIR2/libtk8.0.so (shared)"
  	DYNTKLIBDIR=$BASEDIR2
  	FND=1
fi

if test -f $BASEDIR/libtk8.0.a; then
  	if test -n "$FND"; then echo -n "                            "; fi
  	echo "$BASEDIR/libtk8.0.a (static)"
  	STATKLIBDIR=$BASEDIR
elif test -f $BASEDIR2/libtk8.0.a; then
  	if test -n "$FND"; then echo -n "                            "; fi
  	echo "$BASEDIR2/libtk8.0.a (static)"
  	STATKLIBDIR=$BASEDIR2
fi

if test -z "$DYNTKLIBDIR" -a -z "$STATKLIBDIR"; then
	echo "not found"
fi

echo -n "Looking for Tix 4.1-Library: "
DYNTIXLIBDIR=""
STATIXLIBDIR=""
if [ "$SUSE" = "yes" ]; then
  DYNTIXLIBDIR="/usr/X11R6/lib"
  STATIXLIBDIR="/usr/X11R6/lib"
fi
FND=""
if test -f $BASEDIR/libtix4.1.8.0.so; then
  	echo "$BASEDIR/libtix4.1.8.0.so (shared)"
  	DYNTIXLIBDIR=$BASEDIR
  	FND=1
elif test -f $BASEDIR2/libtix4.1.8.0.so; then
  	echo "$BASEDIR2/libtix4.1.8.0.so (shared)"
  	DYNTIXLIBDIR=$BASEDIR2
  	FND=1
fi

if test -f $BASEDIR/libtix4.1.8.0.a; then
  	if test -n "$FND"; then echo -n "                             "; fi
  	echo "$BASEDIR/libtix4.1.8.0.a (static)"
  	STATIXLIBDIR=$BASEDIR
elif test -f $BASEDIR2/libtix4.1.8.0.a; then
  	if test -n "$FND"; then echo -n "                             "; fi
  	echo "$BASEDIR2/libtix4.1.8.0.a (static)"
  	STATIXLIBDIR=$BASEDIR2
fi

if test -z "$DYNTIXLIBDIR" -a -z "$STATIXLIBDIR"; then
	echo "not found"
fi

echo -n "Looking for Tix 4.1-SAM-Library: "
DYNTIXSAMLIBDIR=""
STATIXSAMLIBDIR=""
if [ "$SUSE" = "yes" ]; then
  DYNTIXSAMLIBDIR="/usr/X11R6/lib"
  STATIXSAMLIBDIR="/usr/X11R6/lib"
fi
FND=""
if test -f $BASEDIR/libtixsam4.1.8.0.so; then
  	echo "$BASEDIR/libtixsam4.1.8.0.so (shared)"
  	DYNTIXSAMLIBDIR=$BASEDIR
  	FND=1
elif test -f $BASEDIR2/libtixsam4.1.8.0.so; then
  	echo "$BASEDIR2/libtixsam4.1.8.0.so (shared)"
  	DYNTIXSAMLIBDIR=$BASEDIR2
  	FND=1
fi

if test -f $BASEDIR/libtixsam4.1.8.0.a; then
  	if test -n "$FND"; then echo -n "                                 "; fi
  	echo "$BASEDIR/libtixsam4.1.8.0.a (static)"
  	STATIXSAMLIBDIR=$BASEDIR
elif test -f $BASEDIR2/libtixsam4.1.8.0.a; then
  	if test -n "$FND"; then echo -n "                                 "; fi
  	echo "$BASEDIR2/libtixsam4.1.8.0.a (static)"
  	STATIXSAMLIBDIR=$BASEDIR2
fi

if test -z "$DYNTIXSAMLIBDIR" -a -z "$STATIXSAMLIBDIR"; then
	echo "not found"
fi

echo -n "Looking for TCL 8.0 lib-directory: "
TCLLIBDIR=""
if [ "$SUSE" = "yes" ]; then
  TCLLIBDIR="/usr/lib"
fi
if test -d $BASEDIR/tcl8.0; then
  	echo "$BASEDIR/tcl8.0/"
  	TCLLIBDIR=$BASEDIR/tcl8.0/
elif test -d $BASEDIR2/tcl8.0; then
  	echo "$BASEDIR2/tcl8.0/"
  	TCLLIBDIR=$BASEDIR2/tcl8.0/
else
  	echo "not found"
fi

echo -n "Looking for TK 8.0 lib-directory: "
TKLIBDIR=""
if [ "$SUSE" = "yes" ]; then
  TKLIBDIR="/usr/X11R6/lib"
fi
if test -d $BASEDIR/tk8.0; then
  	echo "$BASEDIR/tk8.0/"
  	TKLIBDIR=$BASEDIR/tk8.0/
elif test -d $BASEDIR2/tk8.0; then
  	echo "$BASEDIR2/tk8.0/"
  	TKLIBDIR=$BASEDIR2/tk8.0/
else
 	echo "not found"
fi

echo -n "Looking for Tix 4.1 lib-directory: "
TIXLIBDIR=""
if [ "$SUSE" = "yes" ]; then
  TIXLIBDIR="/usr/lib/tcltk/tix4.1.0.006/lib"
fi
if test -d $BASEDIR/tix4.1; then
  	echo "$BASEDIR/tix4.1/"
  	TIXLIBDIR=$BASEDIR/tix4.1/
elif test -d $BASEDIR2/tix4.1; then
  	echo "$BASEDIR2/tix4.1/"
  	TIXLIBDIR=$BASEDIR2/tix4.1/
else
  	echo "not found"
fi

echo -n "Looking for TCL/TK/Tix include-files: "
TCLTKINCLUDE=""
if [ "$SUSE" = "yes" ]; then
  TCLTKINCLUDE="/usr/include -I/usr/X11R6/include -I/usr/lib/tcltk/tix4.1.0.006/include"
fi
if test -f $INCDIR/tcl.h -a -f $INCDIR/tk.h -a -f $INCDIR/tix.h; then
  	echo "$INCDIR/"
  	TCLTKINCLUDE=$INCDIR
elif test -f $INCDIR2/tcl.h -a -f $INCDIR2/tk.h -a -f $INCDIR2/tix.h; then
  	echo "$INCDIR2/"
  	TCLTKINCLUDE=$INCDIR2
else
  	echo "not found"
fi

# Now we have collected the system-info. Its time to check it.

echo
if test -z "$DYNTCLLIBDIR" -a -z "$STATCLLIBDIR"; then
	echo "No TCL 8.0 Library found. Please install TCL 8.0."
	echo "(further information can be found in the README.Compile-file)"
	exit 1
fi
  
if test -z "$DYNTKLIBDIR" -a -z "$STATKLIBDIR"; then
	echo "No TK 8.0 Library found. Please install TK 8.0."
	echo "(further information can be found in the README.Compile-file)"
	exit 1
fi
  
if test -z "$DYNTIXLIBDIR" -a -z "$STATIXLIBDIR" -a -z "$DYNTIXSAMLIBDIR" -a -z "$STATIXSAMLIBDIR"; then
	echo "No Tix 4.1 Library found. Please install Tix4.1."
	echo "(further information can be found in the README.Compile-file)"
	exit 1
fi
 
if test -z "$TCLLIBDIR" -o -z "$TKLIBDIR" -o -z "$TIXLIBDIR"; then
	echo "Not all TCL/TK/Tix-library-directories found."
	echo "Please check your installation of TCL/TK and Tix."
	echo "(further information can be found in the README.Compile-file)"
 	exit 1
fi

if test -z "$TCLTKINCLUDE"; then
	echo "Have not found all the TCL/TK/Tix-include-files." 
	echo "Please check your installation of TCL/TK and Tix."
	echo "(further information can be found in the README.Compile-file)"
	exit 1
fi

STATICOK=0
# got we all to build a static version?
if test -n "$STATIXSAMLIBDIR" -o -n "$STATIXSAMLIBDIR"; then
	STATICOK=1	
fi

SHAREDOK=0
# got we all to build a shared version?
if test -n "$DYNTIXLIBDIR" -o -n "$DYNTIXLIBDIR"; then
	SHAREDOK=1	
fi

if test "$STATICOK" = 0 -a "$SHAREDOK" = 0; then
	echo
	echo "Can't build shared or static version -> aborting."
	echo "Please check your installation of TCL/TK and Tix."
	echo "(further information can be found in the README.Compile-file)"
	exit 1
fi

echo "System-check OK."
echo "Creating src/etixwish/Makefile"
rm -f src/etixwish/Makefile

cat >src/etixwish/Makefile <<EOF
#
# automatically generated by the configure-script.
# usually no need to edit that.
#
# This directory contains the tcl-library-files
TCLLIBDIR=$TCLLIBDIR

# This directory contains the tk-library-files
TKLIBDIR=$TKLIBDIR

# This directory contains the tix-library-files
TIXLIBDIR=$TIXLIBDIR

# The directory that contains tcl.h, tk.h and tix.h
TCLTKINCLUDE=$TCLTKINCLUDE

# The directories where the  tcl/tk/tix-libraries are searched
TCLTKLIBDIRS=-L/usr/local/lib -L/usr/lib

# TCL/TK/Tix-Libraries to link with the static version
TCLSTATICLIBS=-ltixsam4.1.8.0 -ltk8.0 -ltcl8.0

# TCL/TK/Tix-Libraries to link with the shared version
TCLSHAREDLIBS=-ltix4.1.8.0 -ltk8.0 -ltcl8.0

# Library-path-prefix: Where X-CD-Roast looks for its utils..
LIBDIRPREFIX=$LIBDIRPREFIX

EOF

cat src/etixwish/Makefile.in >>src/etixwish/Makefile

echo "Done."

if test "$SHAREDOK" = "1"; then
	DEFTARGET=xcdroast-shared
elif test "$STATICOK" = "1"; then
	DEFTARGET=xcdroast-static
else
	# should never happen
	exit 1
fi

echo "Creating Makefile"
rm -f Makefile

cat >Makefile <<EOF
#
# automatically generated by the configure-script.
# usually no need to edit that.
#

# base-directory for X-CD-Roast installation
PREFIX=$LIBDIRPREFIX

all: $DEFTARGET utils

EOF

cat Makefile.in >>Makefile

echo "Done."

echo
echo "---------------------------------------------------------------"
echo
echo "Type \"make\" to compile X-CD-Roast and its utility-programs."
echo 
echo "Then type \"make install\" to install X-CD-Roast on your system."
echo "(Installation-prefix: $LIBDIRPREFIX)  "
echo
echo "Good Luck."

