#!/bin/csh -f

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

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

# evaluate command-line options
set vararch = 0
set root = ""
set args = ""
while ("$1" != "") 
 switch ($1)
  case --prefix
   shift
   set root = $1
   breaksw;
  case --vararch
   set vararch = 1
   breaksw;
  case -*: 
   goto usage
   breaksw;
  default:
   set args=($args $1);
   breaksw;
 endsw
 shift
end
if ($#args != 1) goto usage

# sun (at least) seems to need the socket library 
# linux does not
set sockets = ""

# 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
   set sockets = "libsocket.a libnsl.a"
   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:
if ($root == "") then
    set root = `pwd`
endif
set inc = $root/include/$arch
set lib = $root/lib/$arch

# check for basic libraries
set fail = 0
foreach f ( libpng.a libjpeg.a libreadline.a libtermcap.a $sockets )
    foreach g ( /usr/lib $lib )
	set name = "$g/$f"
	if (-e $name) then
	    echo "found $g/$f"
	    goto next_lib;
	endif
    end
    echo "missing $f"
    set fail = 1
next_lib:
    continue
end

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

# check for basic libraries
set fail = 0
foreach f ( libX11.a )
    foreach g ( /usr/lib $lib /usr/X11R6/lib /usr/openwin/lib )
	set name = "$g/$f"
	if (-e $name) then 
	    echo "found $g/$f"
	    set xlib = $g
	    goto next_xlib;
	endif
    end
    echo "missing $f"
    set fail = 1
next_xlib:
    continue
end

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

# check for X11 headers
set fail = 0
foreach f ( X11/X.h )
    foreach g ( /usr/include $inc /usr/X11R6/include /usr/openwin/include )
	set name = "$g/$f"
	if (-e $name) then 
	    echo "found $g/$f"
	    set xinc = $g
	    goto next_xinc;
	endif
    end
    echo "missing $f"
    set fail = 1
next_xinc:
    continue
end

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

echo ARCH: $arch
echo ROOT: $root
echo XINC: $xinc
echo XLIB: $xlib

if (-e Configure) then
  mv Configure Configure.bak
endif

if ($vararch) then
  cat Configure.in | sed "s|ROOTDIR|$root|" | sed "s|^\s*ARCH|# ARCH|"   | sed "s|XINCDIR|$xinc|" | sed "s|XLIBDIR|$xlib|" > Configure
else 
  cat Configure.in | sed "s|ROOTDIR|$root|" | sed "s|ARCHVAL|$arch|" | sed "s|XINCDIR|$xinc|" | sed "s|XLIBDIR|$xlib|" > Configure
endif 

exit 0

usage:
 echo "USAGE: configure [--prefix (dir)] [--vararch]"
 echo "set the installation directory root with --prefix"
 echo "if you define the environment variable $ARCH, you can set --vararch"
 exit 2;
