#!/bin/csh -f

# this scripts searches for the needed libraries in standard locations and in the 
# current psconfig installation tree

set version = ""
set args = ""
while ("$1" != "") 
 switch ("$1")
  case -version:
   shift
   set version = $1
   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 supplied PSVERSION
if ("$version" != "") then
  setenv PSVERSION $version
endif

# set install paths for PSVERSION
set libdir  = `source psconfig.csh --libs $PSVERSION`
set incdir  = `source psconfig.csh --include $PSVERSION`

# make these directories
mkdir -p $libdir
mkdir -p $incdir

# I supplement this list with LD_LIBRARY_PATH below
set syslibpath = "/lib /usr/lib /usr/X11R6/lib /usr/local/lib "
set needlibs   = ""
set needlibs   = "$needlibs png"
set needlibs   = "$needlibs z"
set needlibs   = "$needlibs jpeg"
set needlibs   = "$needlibs readline"
set needlibs   = "$needlibs X11"
set needlibs   = "$needlibs pthread"
set needlibs   = "$needlibs cfitsio"
set needlibs   = "$needlibs mysqlclient"
set needlibs   = "$needlibs gsl"
set needlibs   = "$needlibs xml2"
set needlibs   = "$needlibs fftw3"
set needlibs   = "$needlibs fftw3f"
set needlibs   = "$needlibs m"

# I supplement this list with CPATH below
set sysincpath = "/usr/include /usr/local/include /usr/X11R6/include "
set needincs = ""
set needincs = "$needincs X11/Xatom.h"
set needincs = "$needincs X11/Xlib.h"
set needincs = "$needincs X11/Xresource.h"
set needincs = "$needincs X11/Xutil.h"
set needincs = "$needincs X11/cursorfont.h"
set needincs = "$needincs X11/keysym.h"
set needincs = "$needincs X11/keysymdef.h"
set needincs = "$needincs arpa/inet.h"
set needincs = "$needincs assert.h"
set needincs = "$needincs complex.h"
set needincs = "$needincs ctype.h"
set needincs = "$needincs errno.h"
set needincs = "$needincs fcntl.h"
set needincs = "$needincs fitsio.h"
set needincs = "$needincs glob.h"
set needincs = "$needincs gsl/gsl_randist.h"
set needincs = "$needincs gsl/gsl_rng.h"
set needincs = "$needincs inttypes.h"
set needincs = "$needincs jpeglib.h"
set needincs = "$needincs limits.h"
set needincs = "$needincs malloc.h"
set needincs = "$needincs math.h"
set needincs = "$needincs memory.h"
set needincs = "$needincs netdb.h"
set needincs = "$needincs netinet/ip.h"
set needincs = "$needincs png.h"
set needincs = "$needincs pthread.h"
set needincs = "$needincs readline/history.h"
set needincs = "$needincs readline/readline.h"
set needincs = "$needincs regex.h"
set needincs = "$needincs signal.h"
set needincs = "$needincs stdint.h"
set needincs = "$needincs stdio.h"
set needincs = "$needincs stdlib.h"
set needincs = "$needincs string.h"
set needincs = "$needincs sys/ipc.h"
set needincs = "$needincs sys/resource.h"
set needincs = "$needincs sys/sem.h"
set needincs = "$needincs sys/socket.h"
set needincs = "$needincs sys/stat.h"
set needincs = "$needincs sys/time.h"
set needincs = "$needincs sys/types.h"
set needincs = "$needincs sys/uio.h"
set needincs = "$needincs sys/un.h"
set needincs = "$needincs sys/wait.h"
set needincs = "$needincs time.h"
set needincs = "$needincs unistd.h"
set needincs = "$needincs zlib.h"

# XXX need to have options for non-ANSI includes? (ie, varargs.h)
# set needincs = "$needincs values.h" - this only provides values already defined in limits.h?
# set needincs = "$needincs libxml/parser.h"
# set needincs = "$needincs cfuncs.h" - from non-ANSI option in ohana.h
# set needincs = "$needincs float.h" - is from missing_proto (CFHT)
# set needincs = "$needincs floatingpoint.h" - is from missing_proto (CFHT)
# set needincs = "$needincs stdarg.h" - from std includes (in gcc path)
# set needincs = "$needincs varargs.h" - from std includes (in gcc path)
# set needincs = "$needincs stdbool.h" - from std includes (in gcc path) ?

# check the hardware architecture:
set sys=`uname -s` 
set ranlib = "ranlib"
set dlltype = "so"
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 syslibpath = "$syslibpath /usr/openwin/lib"
   set sysincpath = "$sysincpath /usr/openwin/include"
   set needlibs = "$needlibs libsocket libnsl"
   set ranlib = "touch"
   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";
    set syslibpath = "/lib64 /usr/lib64 /usr/X11R6/lib64 $syslibpath"
   endif
   breaksw;
 case Darwin:
   set arch="darwin";
   set mach=`uname -m`
   if ("$mach" == "i386") then
    set arch="darwin_x86";
   endif
   set syslibpath = "$syslibpath /sw/lib /usr/local/lib/mysql"
   set sysincpath = "$sysincpath /sw/include /usr/include/sys"
   set dlltype = dylib
   breaksw;
 case HP-UX:
    set arch="hpux";
    breaksw;
 default:
   echo "unknown architecture";
   exit 1;
   breaksw;
endsw
echo "setting architecture to: $arch" 

# add the LD_LIBRARY_PATH
if ($?LD_LIBRARY_PATH) then 
  set libpath = `echo $LD_LIBRARY_PATH | tr ':' ' '`
else
  set libpath = ""
endif

# check for basic libraries
echo ""
echo "searching for needed external libraries..."
set faillibs = ""
set libflags = ""
set libdirs  = ""
set nonomatch
foreach f ( $needlibs )
    foreach g ( $libdir $libpath $syslibpath )
        if (! -e $g) continue
        set name = $g/lib$f.a
        if (-e $name[1]) goto got_lib;
        set name = $g/lib$f.$dlltype
        if (-e $name[1]) goto got_lib;
    end
    # if we find a .so.N without a matching .so, we link this in
    # the installed libdir
    foreach g ( $libpath $syslibpath )
        set name = $g/lib$f.$dlltype*
        #echo "libname: $name"
        if (-e $name[1]) then
          echo "making link to numbers .$dlltype library $libdir/lib$f.$dlltype"
          ln -s $name[$#name] $libdir/lib$f.$dlltype
          if ($status) exit 1
          goto got_lib
        endif
    end
    echo "missing lib$f"
    set faillibs = "$faillibs lib$f"
    continue
got_lib:
    echo "found lib$f ($name[$#name])"
end

# we need a curses library; can choose one of the following:
# check for termcap, curses, etc
foreach f ( ncurses curses termcap )
    foreach g ( $libdir $libpath $syslibpath )
        set name = $g/lib$f.a
        if (-e $name[1]) goto got_curses;
        set name = $g/lib$f.$dlltype
        if (-e $name[1]) goto got_curses;
    end
    # if we find a .so.N without a matching .so, we link this in
    # the installed libdir
    foreach g ( $libpath $syslibpath )
        set name = $g/lib$f.$dlltype*
        if ($#name > 0) then
          echo "making link to numbers .$dlltype library $libdir/lib$f.$dlltype"
          ln -s $name[$#name] $libdir/lib$f.$dlltype
          goto got_curses;
        endif
    end
end
set faillibs = "$faillibs (ncurses | curses | termcap)"
echo "missing a valid curses library"
echo "missing: $faillibs"
echo "please find one of them and install them in $libdir"
exit 1

got_curses:
  echo "found $f ($name[$#name])"

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

# add the CPATH
if ($?CPATH) then 
  set incpath = `echo $CPATH | tr ':' ' '`
else
  set incpath = ""
endif

# check for headers
echo ""
echo "searching for needed external header files..."
set failincs = ""
set incdirs = ""
foreach f ( $needincs )
  foreach g ( $incdir $incpath $sysincpath )
    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)"
end

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

exit 0

usage:
cat <<EOF
USAGE: pschecklibs

searches for needed libraries and include files in system and psconfig installation directories

Additional options
  -h, --help              display this help and exit

EOF
 exit 2;
