#!/bin/csh -f

# this is a very low-tech version of configure, not built by autoconf.
# we check for the following libraries:

# we need to be able to list the required libraries for a given distribution

# libX11.a
# libsocket.a
# libnsl.a
# libpng.a
# libjpeg.a
# libz.a
# libreadline.a
# libtermcap.a

# evaluate command-line options
set vararch = 0
set prefix  = ""

set bindir  = ""
set libdir  = ""
set incdir  = ""
set mandir  = ""
set datadir  = ""
set sysconfdir  = ""
set optflags = "-g -O0"
set pedantic = ""

set root    = ""
set args    = ""
while ("$1" != "") 
 switch ($1)
  case --vararch
   set vararch = 1
   breaksw;
  case --enable-optimize
   set optflags = "-O2"
   breaksw;
  case --pedantic
   set pedantic = "-Wall -Werror"
   breaksw;
  case --prefix*
   set prefix = `echo $1 | tr = ' ' | awk '{print $2}'`
   breaksw;
  case --bindir*
   set bindir = `echo $1 | tr = ' ' | awk '{print $2}'`
   breaksw;
  case --libdir*
   set libdir = `echo $1 | tr = ' ' | awk '{print $2}'`
   breaksw;
  case --includedir*
   set incdir = `echo $1 | tr = ' ' | awk '{print $2}'`
   breaksw;
  case --sysconfdir*
   set sysconfdir = `echo $1 | tr = ' ' | awk '{print $2}'`
   breaksw;
  case --datadir*
   set datadir = `echo $1 | tr = ' ' | awk '{print $2}'`
   breaksw;
  case --mandir*
   set mandir = `echo $1 | tr = ' ' | awk '{print $2}'`
   breaksw;
  case --help:
   goto usage
  case -*: 
   echo ""
   echo "Unknown option: $1"
   goto usage
  default:
   set args=($args $1);
   breaksw;
 endsw
 shift
end
if ($#args != 1) goto usage

set needlibs = "png z jpeg readline X11 pthread m"
set syslibpath = "/usr/lib /usr/X11R6/lib /usr/openwin/lib"

set needincs = "math.h fcntl.h malloc.h errno.h time.h memory.h stdlib.h string.h X11/X.h"
set sysincpath = "/usr/include /usr/X11R6/include /usr/openwin/include"

# check the hardware architecture:
set sys=`uname -s` 
switch ($sys)
 case IRIX64:
   set arch="irix";
   breaksw;
 case SunOS:
   set ver=`uname -r | awk '{print substr($1,1,1)}'`;
   if ($ver == 5) then
     set arch="sol";
   else 
     set arch="sun4";
   endif
   # sun (at least) seems to need the socket library (linux does not)
   set needlibs = "$needlibs libsocket libnsl"
   breaksw;
 case Linux:
   set arch="linux";
   if (-e /etc/sidious.config) set arch="sid";
   set mach=`uname -m`
   if ("$mach" == "x86_64") then
    set arch="lin64";
   endif
   breaksw;
 case HP-UX:
    set arch="hpux";
    breaksw;
 default:
   echo "unknown architecture";
   exit 1;
   breaksw;
endsw
echo "setting architecture to: $arch" 

# set up the basic directory names:
# XXX : this should be set based on the rules for libdir below
set root = `pwd`
if ($prefix == "") set prefix = $root
if ($vararch) then
  set inc = $prefix/include/$arch
  set lib = $prefix/lib/$arch
else
  set inc = $prefix/include
  set lib = $prefix/lib
endif
if ($?LD_LIBRARY_PATH) then 
  set libpath = `echo $LD_LIBRARY_PATH | tr ':' ' '`
else
  set libpath = ""
endif

# check for basic libraries
set faillibs = ""
set libflags = ""
set libdirs  = ""
foreach f ( $needlibs )
    foreach g ( $syslibpath $lib $libpath )
	set name = "$g/lib$f.a"
	if (-e $name) goto got_lib;
	set name = "$g/lib$f.so"
	if (-e $name) goto got_lib;
    end
    echo "missing lib$f"
    set faillibs = "$faillibs lib$f"
    continue
got_lib:
    echo "found lib$f ($name)"
    echo "$libdirs" | grep -- "-L$g " > /dev/null
    if ($status) then
      set libdirs  = "$libdirs -L$g "
    endif
    echo "$libflags" | grep -- "-l$f " > /dev/null
    if ($status) then
      set libflags = "$libflags -l$f "
    endif
end

# we need a curses library; can choose one of the following:
# check for termcap, curses, etc
foreach f ( ncurses curses termcap )
    foreach g ( $syslibpath $lib $libpath )
	set name = "$g/lib$f.a"
	if (-e $name) goto got_curses;
	set name = "$g/lib$f.so"
	if (-e $name) goto got_curses;
    end
end
set faillibs = "$faillibs (ncurses | curses | termcap)"
echo "missing a valid curses library"
echo "missing: $faillibs"
echo "please find them and install them in $lib"
exit 1

got_curses:
  echo "found $f ($name)"
  echo "$libdirs" | grep -- "-L$g " > /dev/null
  if ($status) then
    set libdirs  = "$libdirs -L$g "
  endif
  echo "$libflags" | grep -- "-l$f " > /dev/null
  if ($status) then
    set libflags = "$libflags -l$f "
  endif

if ("$faillibs" != "") then
  echo "your installation is missing some important libraries"
  echo "missing: $faillibs"
  echo "please find them and install them in $lib"
  exit 1
endif    

# check for headers
set failincs = ""
set incdirs = ""
foreach f ( $needincs )
  foreach g ( $sysincpath $inc )
    set name = "$g/$f"
    if (-e $name) goto got_inc;
  end
  echo "missing $f"
  set failincs = "$failincs $f"
  continue
got_inc:
  echo "found $f ($name)"
  echo "$incdirs" | grep -- "-I$g " > /dev/null
  if ($status) then
    set incdirs = "$incdirs -I$g "
  endif
end

if ("$failincs" != "") then
  echo "your installation is missing some important library headers"
  echo "please find them and install them in $inc"
  exit 1
endif    

echo INCDIRS: $incdirs
echo LIBDIRS: $libdirs
echo LIBFLAGS: $libflags

echo ARCH: $arch
echo ROOT: $root
echo PREFIX: $prefix
echo 

#echo BINDIR $bindir
#echo LIBDIR $libdir
#echo INCDIR $incdir
#echo DATADIR $datadir
#echo MANDIR $mandir

if (-e Configure) mv Configure Configure.bak

rm -f Configure.in.tmp Configure.tmp
cp Configure.in Configure.in.tmp

# we don't currently need to modify the Makefile but since configure
# should create a new Makefile, we need to do this:
cp -f Makefile.in Makefile

# modify the BINDIR
if ("$bindir" == "") then
  set subdir = bin
  set subpath = bin
  if ($vararch) then 
    set subdir = 'bin/$(ARCH)'
    set subpath = "bin/$arch"
  endif
  set bindir = $prefix/$subdir
  set binpath = $prefix/$subpath
endif
cat Configure.in.tmp | sed "s|@BINDIR@| $bindir|" > Configure.tmp
rm -f Configure.in.tmp
mv Configure.tmp Configure.in.tmp
echo BINDIR $bindir

# modify the INCDIR
if ("$incdir" == "") then
  set subdir = include
  if ($vararch) set subdir = 'include/$(ARCH)'
  set incdir = $prefix/$subdir
endif
cat Configure.in.tmp | sed "s|@INCDIR@|$incdir|" > Configure.tmp
rm -f Configure.in.tmp
mv Configure.tmp Configure.in.tmp
echo INCDIR $incdir

# modify the LIBDIR
if ("$libdir" == "") then
  set subdir = lib
  if ($vararch) set subdir = 'lib/$(ARCH)'
  set libdir = $prefix/$subdir
endif
cat Configure.in.tmp | sed "s|@LIBDIR@|$libdir|" > Configure.tmp
rm -f Configure.in.tmp
mv Configure.tmp Configure.in.tmp
echo LIBDIR $libdir

# modify the MANDIR
if ("$mandir" == "") then
  set mandir = $prefix/man
endif
cat Configure.in.tmp | sed "s|@MANDIR@|$mandir|" > Configure.tmp
rm -f Configure.in.tmp
mv Configure.tmp Configure.in.tmp
echo MANDIR $mandir

# modify the HELPDIR
if ("$datadir" == "") then
  set datadir = $prefix
endif
set helpdir = $datadir/help
cat Configure.in.tmp | sed "s|@HELPDIR@|$helpdir|" > Configure.tmp
rm -f Configure.in.tmp
mv Configure.tmp Configure.in.tmp
echo HELPDIR $helpdir

if ($vararch) then
  cat Configure.in.tmp | sed "s|^\s*ARCH|# ARCH|" > Configure.tmp
else 
  cat Configure.in.tmp | sed "s|@ARCHVAL@|$arch|" > Configure.tmp
endif 
rm -f Configure.in.tmp
mv Configure.tmp Configure.in.tmp

cat Configure.in.tmp | sed "s|@ROOTDIR@|$root|" | sed "s|@INCDIRS@|$incdirs|" | sed "s|@LIBDIRS@|$libdirs|" | sed "s|@LIBFLAGS@|$libflags|" | sed "s|@OPTFLAGS@|$optflags $pedantic|" > Configure
rm -f Configure.in.tmp Configure.tmp

cat ohana-config.in | sed "s|@INCDIR@|$incdir|" | sed "s|@LIBDIR@|$libdir|" | sed "s|(ARCH)|ARCH|" > ohana-config

echo ""
echo "include $bindir in your path"

exit 0

usage:
cat <<EOF
USAGE: configure [OPTION]

set the installation directory root with --prefix
if you define the environment variable ARCH, you can set --vararch
 
Configuration:
  -h, --help              display this help and exit
  --enable-optimize       enable compiler optimization
  --pedantic              include -Wall -Werror on compilation

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
  --vararch               install with ARCH suffixes for variable architectures

Fine tuning of the installation directories:
  --bindir=DIR           user executables [PREFIX/bin/$ARCH] 
  --libdir=DIR           object code libraries [PREFIX/lib/$ARCH]
  --includedir=DIR       C header files [PREFIX/include]
  --mandir=DIR           man documentation [PREFIX/man]
  --datadir=DIR          read-only architecture-independent data [PREFIX/share]
  --sysconfdir=DIR       read-only single-machine data [PREFIX/etc]

EOF
 exit 2;
