#!/bin/sh
# configure script for cdf.
#
# Configure builds both a static and a shared library as well as tools.
# If you want to build just a static library, use: ./configure
#
# To impose specific compiler or flags or install directory, use for example:
#    prefix=$HOME CC=cc CFLAGS="-O4" ./configure
# or for csh/tcsh users:
#    (setenv prefix $HOME; setenv CC cc; setenv CFLAGS "-O4"; ./configure)

# Incorrect settings of CC or CFLAGS may prevent creating a shared library.
# If you have problems, try without defining CC and CFLAGS before reporting
# an error.

# start off configure.log
echo -------------------- >> configure.log
echo $0 $* >> configure.log
date >> configure.log

SRCDIR=$(cd $(dirname $0); pwd)
BUILDDIR=$(pwd)

# set command prefix for cross-compilation
if [ -n "${CHOST}" ]; then
    # normalize the chost before parsing it
    NORM_CHOST=$(sh "$SRCDIR"/config.sub $CHOST)
    uname="$(echo "${NORM_CHOST}" | sed -e 's/^[^-]*-\([^-]*\)$/\1/' -e 's/^[^-]*-[^-]*-\([^-]*\)$/\1/' -e 's/^[^-]*-[^-]*-\([^-]*\)-.*$/\1/')"
    CROSS_PREFIX="${CHOST}-"
    ARCH="$(echo "${NORM_CHOST}" | sed -e 's/-.*//')"
else
    ARCH="$(uname -m)"
fi
sname="$(uname -s)"

case "$ARCH" in
    x86_64)
        M32_64=64
        case "${CFLAGS}" in
            *-m32*)
                ARCH='i686'
                M32_64=32
            ;;
        esac
        ;;
    i386 | i486 | i586 | i686)
        M32_64=32
        case "${CFLAGS}" in
            *-m64*)
                ARCH='x86_64'
                M32_64=64
            ;;
        esac
        ;;
    *64*)
        M32_64=64
        ;;
esac

# destination name for windows import library
IMPORTLIB=

# establish commands for library building
if "${CROSS_PREFIX}ar" --version >/dev/null 2>/dev/null || test $? -lt 126; then
    AR=${AR:-"${CROSS_PREFIX}ar"}
    test -n "${CROSS_PREFIX}" && echo Using ${AR} | tee -a configure.log
else
    AR=${AR:-"ar"}
    test -n "${CROSS_PREFIX}" && echo Using ${AR} | tee -a configure.log
fi

RANLIBcmd=${RANLIB:-"${CROSS_PREFIX}ranlib"}
test -n "${CROSS_PREFIX}" && echo Using ${RANLIBcmd} | tee -a configure.log

# set defaults before processing command line options
LDCONFIG=${LDCONFIG:-"ldconfig"}
PC_CFLAGS=

prefix=${prefix:-}
SHAREDyn=yes
shared=1
build32=0
build64=0
SHAREDEXT=
LDSHAREDLIBC="-lc -lm"
LDFLAGS=
LDOPTIONS=
AROPTIONS=
gcc=0
warn=0
old_cc="$CC"
old_cflags="$CFLAGS"
RANLIB=yes

MAKE=
JNI=no
JNIOS=
JNIEXT=

CURSES=
CCURSES=
LCURSES=

FFLAGS2=
LDLIB=
MACVERSION=0
FORTRAN=no
COPTIONS=
COPTIONSld=
JDKx=

# leave this script, optionally in a bad way
leave()
{
  if test "$*" != "0"; then
    echo "** $0 aborting." | tee -a configure.log
  fi
  rm -f $test.[cfo] $test$shared_ext $test.gcno ./--version
  echo -------------------- >> configure.log
  echo >> configure.log
  echo >> configure.log
  exit $1
}

# process command line options
while test $# -ge 1
do
case "$1" in
    -h* | --help)
      echo 'usage:' | tee -a configure.log
      echo '  configure'
      echo '    --MAKE=make        Specified the make utility to build the CDF.'
      echo '                       Use GNU make (gmake) if the default make does not work'
      echo '                       properly.'
      echo '    --CC=cc            Specified the c compiler.'
      echo '                       Default is gcc or cc if it can be found.'
      echo '                       Can be set by CC environment variable.'
      echo '    --FC=fc            Specified the FORTRAN compiler'
      echo '                       Can be set by FC environment variable.'
      echo '    --CFLAGS=cflags    Specified the c compiler option(s)'
      echo '                       For default 64-bit, use "--m32" to compile 32-bit code.'
      echo '                       Can be set by CFLAGS environment variable.'
      echo '    --FFLAGS=fflags    Specified the FORTRAN compiler option(s)'
      echo '                       Can be set by FFLAGS environment variable.'
      echo '    --LDFLAGS=ldflags  Specified the linking option(s)'
      echo '                       Can be set by LDFLAGS environment variable.'
      echo '    --prefix=prefix    Install build files in prefix directory.'
      echo '                       Default is to the source directory.'
      echo '    --32 | --64        Build into 32 or 64-bit compilation mode.'
      echo '                       Used to build a different mode from the default,'
      echo '                       mostly for 64-bit-based systems to make 32-bit code.'
      echo '    --arch=arch        Build the cross-compilation for Mac M* processor.'
      echo '                       Used "x86_64" for making x86 code from Mac M* processors.'
      echo '    --zlib=[orig | ng]'
      echo '                       Choose the zlib fork: original zlib or next-generation'
      echo '                       (zlib replacement with local system optimizations).'
      echo '                       This option might not be suitable for making binary'
      echo '                       distribution.'
      echo '                       Default is original zlib.'
      echo '    --jni              Build JNI (Java Native Interface) for Java-CDF.'
      echo '    --with_jdk=jdk_home'
      echo '                       Home of the JDK kit to be used for JNI build.'
      echo '                       Can be set by JDK_HOME environment variable.'
      echo '    --with_ccurses=ccurses_home'
      echo "                       If curses's include file is not in the default"
      echo '                       search path (e.g., /usr/include on Linux).'
      echo '                       Can be set by CCURSES_HOME environment variable.'
      echo '                       No need for Mac OS as curses is included in the'
      echo '                       source package.'
      echo '                       Used only for CDF tool programs: cdfedit and cdfexport.'
      echo '    --with_lcurses=lcurses_home'
      echo "                       If curses's library file is not in the default"
      echo '                       search path (e.g., /lib64 or /usr/lib on Linux).'
      echo '                       Can be set by LCURSES_HOME environment variable.'
      echo '    --warn             Enables extra compiler warnings'
      echo ' '
      echo ' Note: Options are case-sensitive.'
      echo ' Once the process is finished, run: '
      echo '    make -- to compile the sources.'
      echo '    make test -- to test the test programs.'
      echo '    make install -- to place the built files into the installation directory.'
      echo ' '  
        exit 0 ;;
    --MAKE=*) MAKEx=$(echo $1 | sed 's/.*=//'); shift ;;
    --MAKE) MAKEx="$2"; shift; shift ;;
    --CC=*) CCx=$(echo $1 | sed 's/.*=//'); shift ;;
    --CC) CCx="$2"; shift; shift ;;
    --FC=*) FCx=$(echo $1 | sed 's/.*=//'); shift ;;
    --FC) FCx="$2"; shift; shift ;;
    --CFLAGS=*) CFLAGSx=$(echo $1 | sed 's/.*=//'); shift ;;
    --CFLAGS) CFLAGSx="$2"; shift; shift ;;
    --FFLAGS=*) FFLAGSx=$(echo $1 | sed 's/.*=//'); shift ;;
    --FFLAGS) FFLAGSx="$2"; shift; shift ;;
    --LDFLAGS=*) LDFLAGSx=$(echo $1 | sed 's/.*=//'); shift ;;
    --LDFLAGS) LDFLAGSx="$2"; shift; shift ;;
    -p*=* | --prefix=*) prefixx=$(echo $1 | sed 's/.*=//'); shift ;;
    -p* | --prefix) prefixx="$2"; shift; shift ;;
    -3* | --32) build32=1; shift ;;
    -6* | --64) build64=1; shift ;;
    -j* | --jni) JNIx=yes; shift ;;
    -a*=* | --arch=*) CCARCHx=$(echo $1 | sed 's/.*=//'); shift ;;
    --arch) CCARCHx="$2"; shift; shift ;;
    -z*=* | --zlib=*) ZLIBx=$(echo $1 | sed 's/.*=//'); shift ;;
    --zlib) ZLIBx="$2"; shift; shift ;;
    --with_jdk=*) JDKx=$(echo $1 | sed 's/.*=//'); shift ;;
    --with_jdk) JDKx="$2"; shift; shift ;;
    --with_ccurses=*) CCURSESx=$(echo $1 | sed 's/.*=//'); shift ;;
    --with_ccurses) CCURSESx="$2"; shift; shift ;;
    --with_lcurses=*) LCURSESx=$(echo $1 | sed 's/.*=//'); shift ;;
    --with_lcurses) LCURSESx="$2"; shift; shift ;;
    --warn) warnx=1; shift ;;

    *)
      echo "unknown option: $1" | tee -a configure.log
      echo "$0 --help for help" | tee -a configure.log
      leave 1;;
    esac
done

MAKE=${MAKEx:-make}
CC=${CCx:-${CC}}
FC=${FCx:-${FC}}
CFLAGS=${CFLAGSx:-${CFLAGS}}
FFLAGS=${CFLAGSx:-${CFLAGS}}
LDFLAGS=${LDFLAGSx:-${LDFLAGS}}
prefix=${prefixx:-${prefix}}
JNI=${JNIx:-${JNI}}
CCURSES=${CCURSESx:-${CCURSES_HOME}}
LCURSES=${LCURSESx:-${LCURSES_HOME}}
JDK_HOME=${JDKx:-${JDK_HOME}}
CCARCH=${CCARCHx:-${CCARCH}}
ZLIB=${ZLIBx:-orig}
warn=${warnx:-${warn}}

if test "$CCARCH"; then
  if [ $ARCH =~ "arm" -a "$CCARCH" != "x86_64" ] ; then
    echo 'Invalid --arch option: only "x86_64" is allowed for ARM processor'
    exit 1 # Command to come out of the program with status 1
  else
    M32_64=64
  fi
fi

if test $ZLIB; then
  case $ZLIB in
    orig | ng )
    ;;
  *)  
    echo "Invalid zlib selection: only \"orig\" or \"ng\" is allowed" 
    exit 1 # Command to come out of the program with status 1
    ;; 
  esac
fi

if test $prefix; then
  INSTALLDIR=$prefix
else
  INSTALLDIR=.
fi

# temporary file name
test=ztest$$

# put arguments in log, also put test file in log if used in arguments
show()
{
  case "$@" in
    *$test.c*)
      echo "=== $test.c ===" >> configure.log
      cat $test.c >> configure.log
      echo "===" >> configure.log;;
  esac
  echo "$@" >> configure.log
}

# check for gcc vs. cc and set compile and link flags based on the system identified by uname
cat > $test.c <<EOF
extern int getchar();
int main() {return getchar();}
EOF

cc=${CC:-${CROSS_PREFIX}gcc}
fortran=${FC:-gfortran}

printf "Checking for c compiler... " | tee -a configure.log
case "$cc" in
  *gcc*) gcc=1 ;;
  *clang*) gcc=1 ;;
esac
case $($cc -v 2>&1) in
  *gcc*) gcc=1 ;;
  *clang*) gcc=1 ;;
esac

if [ $build32 -eq 1 ] ; then
  CFLAGS="$CFLAGS -m32"
  FFLAGS="$FFLAGS -m32"
  LDFLAGS="$LDFLAGS -m32"
fi
if [ $build64 -eq 1 ] ; then
  CFLAGS="$CFLAGS -m64"
  FFLAGS="$FFLAGS -m64"
  LDFLAGS="$LDFLAGS -m64"
fi

VERSION=3.9.2
shared_ext=".so"

show $cc -c $test.c
if test $gcc -eq 1 && ($cc $CFLAGS -c $test.c) >> configure.log 2>&1; then
  echo "$cc" | tee -a configure.log
  CC="$cc"
  CFLAGS="$CFLAGS"

  # Re-check ARCH if the compiler is a cross-compiler.
  if $CC -print-multiarch 1> /dev/null 2>&1 && test -n "$($CC -print-multiarch)" 1> /dev/null 2>&1; then
      CC_ARCH=$($CC $CFLAGS -print-multiarch | sed 's/-.*//g')
  else
      CC_ARCH=$($CC $CFLAGS -dumpmachine | sed 's/-.*//g')
  fi

  case $CC_ARCH in
    i386 | i486 | i586 | i686)
      # Honor user choice if gcc is multilib and 64-bit is requested
      if [ $build64 -eq 1 ] ; then
        ARCH=x86_64
        M32_64=64
      else
        ARCH=$CC_ARCH
        M32_64=32
      fi ;;
    x86_64)
      # Honor user choice if gcc is multilib and 32-bit is requested
      if [ $build32 -eq 1 ] ; then
        ARCH=$CC_ARCH
        M32_64=32
      else
        M32_64=64
      fi ;;
    arm | armeb)
      ARCH=arm
      if [ "$(uname)" = "eabi" ] ; then
        uname=arm
      fi ;;
    armv8l)
      ARCH=armv8-a ;;
    aarch64 | aarch64_be | arm64)
      if [ "$(uname)" = "elf" ] ; then
        uname=aarch64
      fi
      ARCH=aarch64 ;;
    powerpc | ppc)
      ARCH=powerpc ;;
    powerpc64 | ppc64)
      ARCH=powerpc64 ;;
    powerpc64le | ppc64le)
      ARCH=powerpc64le ;;
    sparc | sparc64)
      ARCH=$CC_ARCH ;;
  esac

  if [ "$ARCH" = "sparc" ] || [ "$ARCH" = "sparc64" ] ; then
    CFLAGS="-xO2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE $CFLAGS"
  else
    CFLAGS="-O2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE $CFLAGS"
  fi
 
  if test -n "$CCARCH"; then
    CFLAGS="$CFLAGS -arch $CCARCH"
    LDFLAGS="$LDFLAGS -arch $CCARCH"
  fi
  CFLAGS="$CFLAGS "
  if [ $warn -eq 1 ] ; then
    CFLAGS="$CFLAGS -Wextra"
  fi
  if test -z "$uname"; then
    uname=$( (uname -s || echo unknown) 2>/dev/null)
  fi

  case "$uname" in
  Linux* | linux* | GNU | GNU/* | solaris*)
        LD=gcc
        FFLAGS2=$FFLAGS
        FFLAGS="$FFLAGS -fsecond-underscore"
        SHAREDEXT=so
        shared_ext=".so"
        JNIEXT=so
        AROPTIONS=rc
        ARCH1=1
        ARCH2=$M32_64
        ARCH3='setup'
        LDOPTIONS="-shared" ;;
  *BSD | *bsd* | DragonFly)
        LD=gcc
        SHAREDEXT=so
        shared_ext=".so"
        JNIEXT=so
        AROPTIONS=rc
        ARCH1=1
        ARCH2=$M32_64
        ARCH3=setup
        COPTIONS="-DSOLARIS $COPTIONS"
        CFLAGS="-DSOLARIS $CFLAGS"
        LDOPTIONS="-shared" ;;
  solaris*)
        LD=gcc
        FFLAGS2=$FFLAGS
        FFLAGS="$FFLAGS -ext_names=fsecond-underscore"
        MAKE=gmake
        AROPTIONS=rc
        COPTIONS="$COPTIONS -DSOLARIS"
        CFLAGS="$CFLAGS -DSOLARIS"
        if [ "$CC" = "cc" ]; then
          case $ARCH in
          i386* | i486* | i586* | i686*) 
            LD=ld ;;
          x86_64*) 
            LD="ld -64"
            COPTIONS="$COPTIONS -DSOLARIS64"
            CFLAGS="$CFLAGS -DSOLARIS64" ;;
          sparc64*) 
            LD=gcc
            COPTIONS="$COPTIONS -DSOLARIS64"
            CFLAGS="$CFLAGS -DSOLARIS64" ;;
          sparc*) 
            LD=gcc ;;
          esac;
        fi
        if [ "$CC" = "gcc" ]; then
          case $ARCH in
          i386* | i486* | i586* | i686*) 
            LDOPTIONS="-shared"
            LD=gcc ;;
          x86_64*) 
            LDOPTIONS="-shared"
            LD=gcc
            COPTIONS="$COPTIONS -DSOLARIS64"
            CFLAGS="$CFLAGS -DSOLARIS64" ;;
          sparc64*) 
            LDOPTIONS="-G"
            LD=gcc
            COPTIONS="$COPTIONS -DSOLARIS64"
            CFLAGS="$CFLAGS -DSOLARIS64" ;;
          sparc*) 
            LDOPTIONS="-G"
            LD=gcc ;;
          esac;
        fi
        ARCH1=
        ARCH2=$M32_64
        ARCH3=setup
        RANLIB=no
        SHAREDEXT=so
        shared_ext=".so"
        JNIEXT=so ;;
  CYGWIN* | Cygwin* | cygwin*)
        LD=gcc
        FFLAGS2=$FFLAGS
        FFLAGS="$FFLAGS -fsecond-underscore"
        AROPTIONS=rc
        SHAREDEXT=dll
        shared_ext='.dll'
        JNIEXT=dll
        ARCH1=1
        ARCH2=$M32_64
        ARCH3=cygwin 
        LDSHAREDLIBC="-lgcc"
        LDOPTIONS="-shared" ;;
  MSYS* | msys*)
        LD=gcc
        FFLAGS2=$FFLAGS
        FFLAGS="$FFLAGS -fsecond-underscore"
        AROPTIONS=rc
        SHAREDEXT=dll
        shared_ext='.dll'
        JNIEXT=dll
        ARCH1=1
        ARCH2=$M32_64
        ARCH3=cygwin 
        LDSHAREDLIBC="-lgcc"
        LDOPTIONS="-shared" ;;
  MINGW* | mingw*)
        LD=gcc
        FFLAGS2=$FFLAGS
        FFLAGS="$FFLAGS -fsecond-underscore"
        AROPTIONS=rc
        SHAREDEXT=dll
        shared_ext='.dll'
        JNIEXT=dll
        ARCH1=2
        ARCH2=$M32_64
        ARCH3=cygwin
        LDSHAREDLIBC="-lgcc"
        LDOPTIONS="-shared" ;;
  QNX*)  # This is for QNX6. I suppose that the QNX rule below is for QNX2,QNX4
        LD=gcc
        AROPTIONS=rc
        SHAREDEXT=so
        sharedext=".so"
        JNIEXT=so
        LDOPTIONS="-shared"
        ARCH1=
        ARCH2=$M32_64
        ARCH3=setup 
        LDOPTIONS="-shared" ;;
  HP-UX*)
        LD=ld
        AROPTIONS=rc
        ARCH1=
        ARCH2=$M32_64
        RANLIB=no
        LDOPTIONS="-shared"
        case $( (uname -m || echo unknown) 2>/dev/null) in ia64)
          SHAREDEXT=so
          shared_ext=".so" ;;
        *)
          SHAREDEXT=sl
          shared_ext=".sl" ;;
        esac 
        LDOPTIONS="-shared" ;;
  Darwin* | darwin*)
        LD=gcc
        AROPTIONS="-static -o"
        SHAREDEXT=dylib
        shared_ext=".dylib"
        JNIEXT=jnilib
        if [ "$ARCH" = "x86_64" ] ; then
          ARCH1=2
        else
          ARCH1=3
        fi
        ARCH2=$M32_64
        ARCH3=setup
        if libtool -V 2>&1 | grep Apple > /dev/null; then
          AR=libtool
        else
          AR="/usr/bin/libtool"
        fi
        RANLIB=no
        MACVERSION="$(uname -r | cut -f1 -d.)"
        XCODEDir="$(xcode-select -p)"
        LDLIB=-L$XCODEDir/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib
        LDOPTIONS="-shared" ;;
  aarch64)
        LD=gcc
        AROPTIONS="-static -o"
        AR=libtool
        RANLIB=no
        SHAREDEXT=dylib
        shared_ext=".dylib"
        JNIEXT=jnilib
        ARCH1=3
        ARCH2=$M32_64
        ARCH3=setup 
        LDOPTIONS="-dynamic" 
        MACVERSION="$(uname -r | cut -f1 -d.)"
        XCODEDir="$(xcode-select -p)"
        LDLIB=-L$XCODEDir/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib
        LDOPTIONS="-shared" ;;
  *)
        ;;
  esac
else
  # find system name and corresponding cc options
  tCC=${CC:-cc}
  gcc=0
  if test $tCC $CFLAGS -c $test.c >> configure.log 2>&1; then
    :
  else
    echo "$cc Fail: not recognized!"
    exit 1
  fi
  CC=$tCC
  if test -z "$uname"; then
    uname=$( (uname -sr || echo unknown) 2>/dev/null)
  fi
  case "$uname" in
  HP-UX*)
        ARCH1=
        ARCH2=$M32_64
        ARCH3='setup'
        case $( (uname -m || echo unknown) 2>/dev/null) in ia64)
           SHAREDEXT="so" ;;
        *)
           SHAREDEXT="sl" ;;
        esac
        LDOPTIONS="-b" ;;
  AIX*)  # Courtesy of dbakker@arrayasolutions.com
        SHAREDEXT=o 
        ARCH1=
        ARCH2=$M32_64
        ARCH3='setup'
        LDOPTIONS="-bnoentry" ;;
  *)    ARCH1=
        ARCH2=$M32_64
        ARCH3='setup'
        LDOPTIONS="-shared" ;;

  esac
fi

echo >> configure.log

case "$cc" in
  *gcc*) gcc=1 ;;
  *clang*) gcc=1 ;;
esac
case $($cc -v 2>&1) in
  *gcc*) gcc=1 ;;
  *clang*) gcc=1 ;;
esac

# Symbol versioning
case "$uname" in
  CYGWIN* | Cygwin* | cygwin* | MINGW* | mingw* | MSYS* | msys* | Darwin* | darwin*)
    echo "Checking for Symbol versioning... No."
  ;;
  *)
    if [ $shared -eq 1 ] ; then
      echo "Checking for Symbol versioning... Yes."
      CFLAGS="$CFLAGS -DHAVE_SYMVER"
    else
      echo "Checking for Symbol versioning... No."
    fi
  ;;
esac

# Simplify some later conditionals
case "$uname" in
Linux* | linux*)
  LINUX=1 ;;
*)
  LINUX=0 ;;
esac

echo >> configure.log

# define functions for testing compiler and library characteristics and logging the results

try()
  {
    show "$@"
    ( "$@" ) >> configure.log 2>&1
    ret=$?
    if [ $ret -ne 0 ] ; then
      echo "(exit code $ret)" >> configure.log
    fi
    return $ret
  }

echo >> configure.log

printf "Checking for fortran compiler... " | tee -a configure.log
unamem=$(uname -m)
isARM=0
case "$unamem" in
 "arm"*)
  isARM=1
  ;;
 *)
  isARM=0
  ;;
esac

if [ $isARM -eq 1 -a "$CCARCH" = "x86_64" ] ; then
  echo "No... Not support for cross-compiling..."
else
  cat > $test.f << EOF
         PROGRAM TEST
C        INCLUDE 'CDF.INC'
         STOP
         END
EOF
  foundFC=0
  if test $CCARCH; then
    foption="-arch x86_64"
  else
    foption=
  fi
  if test $FC; then
    if try $FC $foption -c $test.f 2>/dev/null; then
      foundFC=1
    fi
  else
    for fcy in "gfortran" "f90" "f77" "xlf"; do
      if try $fcy -c $test.f 2>/dev/null; then
        FC="$fcy"
        foundFC=1
        break
      fi
    done
  fi
  if [ $foundFC -eq 1 ] ; then
    echo "$FC... Yes"
    FORTRAN=yes
    GFLAGS=
  else
    echo "No... None is found... "
  fi
  $(rm $test.f)
fi

echo >> configure.log

cat > $test.c << EOF
int foo() { return 0; }
EOF
echo "Checking for obsessive-compulsive compiler options..." >> configure.log
if try $CC -c $CFLAGS $test.c; then
  :
else
  echo "Compiler error reporting is too harsh for $0 (perhaps remove -Werror)." | tee -a configure.log
  leave 1
fi

echo >> configure.log

cat > $test.c << EOF
int foo() { return 0; }
EOF
echo "Checking for position-independent code (pic) compiler options..." >> configure.log
for apic in "-fPIC" "-fpic" "-KPIC" "-Kpic" "+z"; do
  if try $CC -c $CFLAGS $apic $test.c; then
    echo "Compiler option for position-independent code (pic) is $apic." | tee -a configure.log
    PIC="$apic"
    break
  fi
done

echo >> configure.log

# see if shared library build supported
FOUNDSHR=0
cat > $test.c <<EOF
extern int getchar();
int hello() {return getchar();}
EOF
if [ $shared -eq 1 ] ; then
  printf "Checking for shared library support... " | tee -a configure.log
  # we must test in two steps (cc then ld), required at least on SunOS 4.x
  for ashare in "-shared" "-dynamic" "-G" "-b" "-bnoentry"; do
    if try $CC -c $CFLAGS $test.c &&
       try $LD $LDFLAGS $ashare -o $test$shared_ext $test.o $LDSHAREDLIBC; then
      echo "Building shared library $shared_ext With $LD... Yes." | tee -a configure.log
      LDOPTIONS=$ashare
      FOUNDSHR=1
      SHAREDyn=yes
      break
    fi
  done
  if [ $FOUNDSHR -ne 1 ] ; then
    if [ -z "$old_cc" && -z "$old_cflags" ] ; then
      echo "No." | tee -a configure.log
      SHAREDyn=no
      shared=0
    else
      echo 'No; try without defining CC and CFLAGS' | tee -a configure.log
      SHAREDyn=no
      shared=0
    fi
  fi
fi

echo >> configure.log

# check for large file support, and if none, check for fseeko()
cat > $test.c <<EOF
#include <sys/types.h>
off64_t dummy = 0;
EOF
if try $CC -c $CFLAGS -D_LARGEFILE64_SOURCE=1 $test.c; then
  CFLAGS="$CFLAGS -D_LARGEFILE64_SOURCE=1"
  echo "Checking for off64_t... Yes." | tee -a configure.log
  echo "Checking for fseeko... Yes." | tee -a configure.log
else
  echo "Checking for off64_t... No." | tee -a configure.log
  echo >> configure.log
  cat > $test.c <<EOF
#include <sys/types.h>
int main() {
  _off64_t dummy = 0;
  return 0;
}
EOF
  if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
    echo "Checking for _off64_t... Yes." | tee -a configure.log
  else
    echo "Checking for _off64_t... No." | tee -a configure.log
  fi
  echo >> configure.log
  cat > $test.c <<EOF
#include <stdio.h>
int main(void) {
  fseeko(NULL, 0, 0);
  return 0;
}
EOF
  if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
    echo "Checking for fseeko... Yes." | tee -a configure.log
  else
    CFLAGS="$CFLAGS -DNO_FSEEKO"
    echo "Checking for fseeko... No." | tee -a configure.log
  fi
fi
echo >> configure.log

# check for ncurses
printf "Checking for ncurses... " | tee -a configure.log
cat > $test.c <<EOF
#include <ncurses.h>
int main() {
 WINDOW *win=newwin(0,0,10,10);
 return 0; }
EOF

if [ "$CCURSES" = "" ] ; then
  if [ "$sname" = "Darwin" ] ; then
    macosxCURSES="$SRCDIR/macosx/ncurses"
    CCURSES="$macosxCURSES/include"
    CCURSESx="-I$macosxCURSES/include"
    CFLAGS="$CFLAGS $CCURSESx"
    LCURSES="$macosxCURSES/lib/libncurses.a"
    CURSESINC="$CCURSESx"
    CURSESLIB="$macosxCURSES/lib/libncurses.a"
    CURSES=yes  
  fi
fi

if [ "$CCURSES" = "" ] ; then
  for cursesi in "/usr/include/ncurses" "/usr/include/curses"; do 
    if try $CC $CFLAGS -I $cursesi -c $test.c; then
      goodcurses=0
      if test $CCARCH; then
        extra="-arch $CCARCH"
      else
        extra=
      fi
      CCURSES=$cursesi
      CCURSESx="-I $CCURSES" 
      break
    fi
  done
  if [ "$CCURSES" != "" ] ; then
    for cursesl in "-lncursesw" "-lncurses" "-lcurses"; do 
      if try $LD $extra $LDFLAGS -o $test $test.o $cursesl $LDSHAREDLIBC; then
        CURSES=yes
        CURSESINC="$CCURSESx"
        CURSESLIB="$cursesl"
        goodcurses=1
        break
      fi
    done
    if [ $goodcurses -eq 1 ] ; then
      echo "Yes." | tee -a configure.log
    else
      CURSES=no
      echo "No." | tee -a configure.log
    fi
  else
    CURSES=no
    echo "No." | tee -a configure.log
  fi
fi

echo >> configure.log

# check for unistd.h and save result in zconf.h
cat > $test.c <<EOF
#include <unistd.h>
int main() { return 0; }
EOF
if try $CC -c $CFLAGS $test.c; then
  echo "Checking for unistd.h... Yes." | tee -a configure.log
  COPTIONS="$COPTIONS -DHAVE_UNISTD_H"
  CFLAGS="$CFLAGS -DHAVE_UNISTD_H"
else
  echo "Checking for unistd.h... No." | tee -a configure.log
fi

echo >> configure.log

# Check for ANSI C compliant compiler
cat > $test.c <<EOF
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
int main() {
#ifdef STDC
  return 0;
#endif
  return 1;
}
EOF
if try $CC -c $CFLAGS $test.c; then
  echo "Checking for ANSI C compliant compiler...  Yes." | tee -a configure.log
  COPTIONS="$COPTIONS -DHAVE_STDARG_H"
  CFLAGS="$CFLAGS -DHAVE_STDARG_H"
  :
else
  echo "Checking for ANSI C compliant compiler...  No." | tee -a configure.log
  echo "Error: ANSI C compatible compiler needed, cannot continue." | tee -a configure.log
  leave 1
fi

echo >> configure.log

# Check for attribute(aligned) support in compiler
cat > $test.c << EOF
int main(void) {
  __attribute__((aligned(8))) int test = 0;
  (void)test;
  return 0;
}
EOF
if try $CC $CFLAGS $test.c $LDSHAREDLIBC; then
    echo "Checking for attribute(aligned)... Yes." | tee -a configure.log
#    CFLAGS="$CFLAGS -DHAVE_ATTRIBUTE_ALIGNED"
else
    echo "Checking for attribute(aligned)... No." | tee -a configure.log
fi

echo >> configure.log

INCDIR1=
INCDIR2=
# Check forJNI build
 unameL=$(echo $uname | tr '[:upper:]' '[:lower:]')
 case $unameL in 
  linux | solaris)
   JNIOS=$unameL ;;
  cygwin* | msys* | mingw*)
   JNIOS=win32 ;;
  darwin)
   JNIOS=$unameL ;;
  *) ;;
 esac

if [ "$JNI" = "yes" ]; then 
 if [ ! -d "$JDK_HOME" ]; then 
   echo "Checking for JNI build... No." | tee -a configure.log
   echo "  JNI can't be built as JDK_HOME=$JDK_HOME is not properly defined..." | tee -a configure.log
   leave 1
 fi
 cat > $test.c << EOF
 #include <jni.h>
 int main(void) { return 0; }
EOF

EXTRA=
 case $unameL in 
  linux | solaris)
   JDK_HOMEx=$JDK_HOME
   INCDIR1=$JDK_HOME/include
   INCDIR2=$INCDIR1/$unameL ;;
  cygwin* | msys* | mingw*)
   JDK_HOMEx=$JDK_HOME
   INCDIR1=$JDK_HOME/include
   INCDIR2=$INCDIR1/win32
   EXTRA=-D__int64="long long" ;;
  darwin)
   JDK_HOMEx=$JDK_HOME/Contents/Home
   INCDIR1=$JDK_HOME/Contents/Home/include
   INCDIR2=$INCDIR1/$unameL ;;
  *)
   echo "Checking for JNI build... No." | tee -a configure.log
   echo "  JNI can't be built as OS name: $unameL is not supported..." | tee -a configure.log
   leave 1 ;;
 esac

 $CC -c -I"$INCDIR1" -I"$INCDIR2" $EXTRA $test.c  >> configure.log 2>&1
 ret=$?
 if test $ret ; then
   echo "Checking for JNI build... Yes." | tee -a configure.log
 else
   echo "Checking for JNI build... No." | tee -a configure.log
   echo "  ** JNI can't be built as JDK_HOME is not properly defined..." | tee -a configure.log
   leave 1
 fi
fi

echo >> configure.log

makefile=Makefile
curdir=${PWD}
if [ "$ZLIB" = "ng" ] ; then
  s32or64=
  zOPTIONS=
  ARCHx=
  GENERICx=
  if [ $ARCH1 -eq 1 ] ; then
#   x86 / cygwin
    ARCHx="arch/x86"
    GENERICx=arch/generic
    if test $ARCH2; then
      if [ $ARCH2 -eq 32 ] ; then
        s32or64=--32
      fi
      if [ $ARCH2 -eq 64 ] ; then
        s32or64=--64
      fi
    fi
  else
    if [ $ARCH1 -eq 2 ] ; then
# Macosx x86_64
      if [ "$ARCH" = "x86_64" ] ; then
        ARCHx="arch/x86"
      else
        ARCHx="arch/arm"
      fi
      GENERICx=arch/generic
      if test $ARCH2 ; then
        if [ $ARCH2 -eq 32 ] ; then
         s32or64=--archs="-arch i386"
        fi
        if [ $ARCH2 -eq 64 ] ; then
         s32or64=--archs="-arch x86_64"
        fi
      fi
    else
      if [ $ARCH1 -eq 3 ] ; then
# Macosx arm
        ARCHx="arch/arm"
        GENERICx=arch/generic
        if test $CCARCH ; then
          if [ "$CCARCH" = "x86_64" ] ; then
            s32or64=--archs="-arch x86_64"
          fi
        else
          if test $ARCH2 ; then
            if [ $ARCH2 -eq 32 ] ; then
              s32or64=--32
            else
              if [ $ARCH2 -eq 64 ] ; then
                s32or64=--64
              fi
            fi
          fi
        fi
      fi
    fi
  fi

  echo " "
  if [ "$ZLIB" = "ng" ] ; then
    if [ "$ARCH3" = "setup" ] ; then
      echo Creating Zlib Makefile file...
    else
      echo Creating Zlib Makefile file... May take a few moments.
    fi
    CC=$CC 
  fi
fi

if [ "$ZLIB" = "orig" ] ; then
  ZLIBDIR="zlib"
else
  ZLIBDIR="zlib-ng"
fi

LDOPTIONSx="$LDOPTIONS $LDFLAGS"
echo "Uname: $uname"
echo "ARCH: $ARCH"

# show the results in the log
echo >> configure.log
echo MAKE = $MAKE >> configure.log
echo AR = $AR >> configure.log
echo CC = $CC >> configure.log
echo FC = $FC >> configure.log
echo LD = $LD >> configure.log
echo CFLAGS = $CFLAGS >> configure.log
echo COPTIONS = $COPTIONS >> configure.log
echo FOPTIONS = $FFLAGS >> configure.log
echo LDCONFIG = $LDCONFIG >> configure.log
echo COPTIONSld = $LDFLAGS >> configure.log
echo LDOPTIONS = $LDOPTIONSx >> configure.log
echo AROPTIONS = $AROPTIONS >> configure.log
echo PIC = $PIC >> configure.log
echo ZLIB = $ZLIBDIR >> configure.log
echo SHARED = $SHAREDyn >> configure.log
echo SHAREDEXT = $SHAREDEXT >> configure.log
echo ARCH1 = $ARCH1 >> configure.log
echo ARCH2 = $ARCH2 >> configure.log
echo ARCH3 = $ARCH3 >> configure.log
echo RANLIBcmd = $RANLIBcmd >> configure.log
echo RANLIB = $RANLIB >> configure.log
echo INSTALLDIR = $INSTALLDIR >> configure.log
echo SRCDIR = $SRCDIR >> configure.log
echo BUILDDIR = $BUILDDIR >> configure.log
echo VERSION = $VERSION >> configure.log
echo JDK_HOME = $JDK_HOMEx >> configure.log
echo JNI = $JNI >> configure.log
echo JNIOS = $JNIOS >> configure.log
echo JNIEXT = $SHAREDEXT >> configure.log
echo SYSLIBS = $LDSHAREDLIBC >> configure.log
echo CURSES = $CURSES >> configure.log
echo CURSESINC = $CURSESINC >> configure.log
echo CURSESLIB = $CURSESLIB >> configure.log
echo FOPTIONSld = $FFLAGS2 >> configure.log
echo FORTRAN = $FORTRAN >> configure.log
echo MACVERSION = $MACVERSION >> configure.log
echo >> configure.log

echo MAKE = $MAKE
echo AR = $AR
echo CC = $CC
echo FC = $FC
echo LD = $LD
echo CFLAGS = $CFLAGS
echo COPTIONS = $COPTIONS
echo FOPTIONS = $FFLAGS
echo LDCONFIG = $LDCONFIG
echo COPTIONSld = $LDFLAGS
echo LDOPTIONS = $LDOPTIONSx
echo AROPTIONS = $AROPTIONS
echo PIC = $PIC
echo ZLIB = $ZLIBDIR
echo SHARED = $SHAREDyn
echo SHAREDEXT = $SHAREDEXT
echo ARCH1 = $ARCH1
echo ARCH2 = $ARCH2
echo ARCH3 = $ARCH3
echo RANLIBcmd = $RANLIBcmd
echo RANLIB = $RANLIB
echo INSTALLDIR = $INSTALLDIR
echo SRCDIR = $SRCDIR
echo BUILDDIR = $BUILDDIR
echo VERSION = $VERSION
echo JDK_HOME = $JDK_HOMEx
echo JNI = $JNI
echo JNIOS = $JNIOS
echo JNIEXT = $JNIEXT
echo SYSLIBS = $LDSHAREDLIBC
echo CURSES = $CURSES
echo CURSESINC = $CURSESINC
echo CURSESLIB = $CURSESLIB
echo FOPTIONSld = $FFLAGS2
echo FORTRAN = $FORTRAN
echo MACVERSION = $MACVERSION

# update Makefile with the configure results

if test "$SRCDIR" != "$BUILDDIR" ; then
  OUTMAKE=$BUILDDIR/Makefile
else
  OUTMAKE=$SRCDIR/Makefile
fi
sed < $SRCDIR/Makefile.in "
/^MAKE *=/s#=.*#=$MAKE#
/^CC *=/s#=.*#=$CC#
/^FC *=/s#=.*#=$FC#
/^LD *=/s#=.*#=$LD#
/^SHARED *=/s#=.*#=$SHAREDyn#
/^SHAREDEXT *=/s#=.*#=$SHAREDEXT#
/^PIC *=/s#=.*#=$PIC#
/^COPTIONS *=/s#=.*#=$CFLAGS#
/^FOPTIONS *=/s#=.*#=$FFLAGS#
/^AROPTIONS *=/s#=.*#=$AROPTIONS#
/^ZLIB *=/s#=.*#=$ZLIBDIR#
/^VERSION *=/s#=.*#=$VERSION#
/^RANLIBcmd *=/s#=.*#=$RANLIBcmd#
/^RANLIB *=/s#=.*#=$RANLIB#
/^SYSLIBS *=/s#=.*#=$LDSHAREDLIBC#
/^CURSESINC *=/s#=.*#=$CURSESINC#
/^CURSESLIB *=/s#=.*#=$CURSESLIB#
/^LDOPTIONS *=/s#=.*#=$LDOPTIONSx#
/^CURSES *=/s#=.*#=$CURSES#
/^FC *=/s#=.*#=$FC#
/^FOPTIONSld *=/s#=.*#=$FFLAGS2#
/^ARCH1 *=/s#=.*#=$ARCH1#
/^ARCH2 *=/s#=.*#=$ARCH2#
/^ARCH3 *=/s#=.*#=$ARCH3#
/^INSTALLDIR *=/s#=.*#=$INSTALLDIR#
/^JDK_HOME *=/s#=.*#=$JDK_HOMEx#
/^JNI *=/s#=.*#=$JNI#
/^JNIOS *=/s#=.*#=$JNIOS#
/^JNIEXT *=/s#=.*#=$JNIEXT#
/^SRCDIR *=/s#=.*#=$SRCDIR#
/^BUILDDIR *=/s#=.*#=$BUILDDIR#
/^FORTRAN *=/s#=.*#=$FORTRAN#
/^MACVERSION *=/s#=.*#=$MACVERSION#
/^COPTIONSld *=/s#=.*#=$LDFLAGS#
" > $OUTMAKE

#cd $SRCDIR/src/lib
SRCDIR1=$SRCDIR/src/lib
BUILDDIR1=$BUILDDIR/src/lib
if test "$SRCDIR" != "$BUILDDIR" ; then 
  mkdir -p $BUILDDIR/src/lib
  OUTMAKE=$BUILDDIR/src/lib/Makefile 
else
  OUTMAKE=$SRCDIR/src/lib/Makefile 
fi
sed < $SRCDIR/src/lib/Makefile.in "
/^MAKE *=/s#=.*#=$MAKE#
/^AR *=/s#=.*#=$AR#
/^SHARED *=/s#=.*#=$SHAREDyn#
/^PIC *=/s#=.*#=$PIC#
/^CCx *=/s#=.*#=$CC#
/^LDx *=/s#=.*#=$LD#
/^COPTIONS *=/s#=.*#=$CFLAGS#
/^COPTIONSld *=/s#=.*#=$LDFLAGS#
/^SYSLIBS *=/s#=.*#=$SYSLIBS#
/^SHAREDEXT *=/s#=.*#=$SHAREDEXT#
/^LDOPTIONS *=/s#=.*#=$LDOPTIONSx#
/^AROPTIONS *=/s#=.*#=$AROPTIONS#
/^RANLIBcmd *=/s#=.*#=$RANLIBcmd#
/^RANLIB *=/s#=.*#=$RANLIB#
/^VERSION *=/s#=.*#=$VERSION#
/^ZLIB *=/s#=.*#=$ZLIBDIR#
/^ARCH2 *=/s#=.*#=$ARCH2#
/^SRCDIR *=/s#=.*#=$SRCDIR1#
/^BUILDDIR *=/s#=.*#=$BUILDDIR1#
" > $OUTMAKE

if [ $ZLIB = "orig" ] ; then
#  cd $SRCDIR/src/lib/zlib
  SRCDIRz=$SRCDIR/src/lib/zlib
  BUILDDIRz=$BUILDDIR/src/lib/zlib
  if test "$SRCDIR" != "$BUILDDIR" ; then
    mkdir -p $BUILDDIR/src/lib/zlib
    OUTMAKE=$BUILDDIR/src/lib/zlib/Makefile
  else
    OUTMAKE=$SRCDIR/src/lib/zlib/Makefile
  fi
  sed < $SRCDIR/src/lib/zlib/Makefile.in "
/^MAKE *=/s#=.*#=$MAKE#
/^PIC *=/s#=.*#=$PIC#
/^CCx *=/s#=.*#=$CC#
/^COPTIONS *=/s#=.*#=$CFLAGS#
/^SRCDIR *=/s#=.*#=$SRCDIRz#
/^BUILDDIR *=/s#=.*#=$BUILDDIRz#
" > $OUTMAKE
else
  SRCDIRz=$SRCDIR/src/lib/zlib-ng
  BUILDDIRz=$BUILDDIR/src/lib/zlib-ng
  if test "$SRCDIR" != "$BUILDDIR" ; then
    mkdir -p $BUILDDIR/src/lib/zlib-ng
    OUTMAKE=$BUILDDIR/src/lib/zlib-ng/Makefile
  else
    OUTMAKE=$SRCDIR/src/lib/zlib-ng/Makefile
  fi
  cd $BUILDDIR/src/lib/zlib-ng
  yyy=
  if [ $build32 -eq 1 ] ; then
    yyy=--32
  fi
  if [ $build64 -eq 1 ] ; then
    yyy=--64
  fi
  $SRCDIR/src/lib/zlib-ng/configure $yyy --zlib-compat --without-gzfileops
fi
 
SRCDIRt=$SRCDIR/src/tools
BUILDDIRt=$BUILDDIR/src/tools
if test "$SRCDIR" != "$BUILDDIR" ; then
  mkdir -p $BUILDDIR/src/tools
  OUTMAKE=$BUILDDIR/src/tools/Makefile
else
  OUTMAKE=$SRCDIR/src/tools/Makefile
fi
sed < $SRCDIR/src/tools/Makefile.in "
/^CURSES *=/s#=.*#=$CURSES#
/^CCx *=/s#=.*#=$CC#
/^COPTIONS *=/s#=.*#=$CFLAGS#
/^COPTIONSld *=/s#=.*#=$LDFLAGS#
/^SYSLIBS *=/s#=.*#=$LDSHAREDLIBC#
/^CURSESINC *=/s#=.*#=$CURSESINC#
/^CURSESLIB *=/s#=.*#=$CURSESLIB#
/^SRCDIR *=/s#=.*#=$SRCDIRt#
/^BUILDDIR *=/s#=.*#=$BUILDDIRt#
" > $OUTMAKE

SRCDIRe=$SRCDIR/cdf_tests
BUILDDIRe=$BUILDDIR/cdf_tests
if test "$SRCDIR" != "$BUILDDIR" ; then
  mkdir -p $BUILDDIR/cdf_tests
  OUTMAKE=$BUILDDIR/cdf_tests/Makefile
else
  OUTMAKE=$SRCDIR/cdf_tests/Makefile
fi
sed < $SRCDIR/cdf_tests/Makefile.in "
/^MAKE *=/s#=.*#=$MAKE#
/^CCx *=/s#=.*#=$CC#
/^COPTIONS *=/s#=.*#=$CFLAGS#
/^COPTIONSld *=/s#=.*#=$LDFLAGS#
/^SYSLIBS *=/s#=.*#=$LDSHAREDLIBC#
/^SHARED *=/s#=.*#=$SHAREDyn#
/^SHAREDEXT *=/s#=.*#=$SHAREDEXT#
/^FORTRAN *=/s#=.*#=$FORTRAN#
/^FCx *=/s#=.*#=$FC#
/^FOPTIONS *=/s#=.*#=$FFLAGS#
/^FOPTIONSld *=/s#=.*#=$FFLAGS2#
/^SRCDIR *=/s#=.*#=$SRCDIRe#
/^BUILDDIR *=/s#=.*#=$BUILDDIRe#
" > $OUTMAKE

if test $JNI = "yes"; then
  SRCDIRj=$SRCDIR/cdfjava/jni
  BUILDDIRj=$BUILDDIR/cdfjava/jni
  if test "$SRCDIR" != "$BUILDDIR" ; then
    mkdir -p $BUILDDIR/cdfjava/jni
    OUTMAKE=$BUILDDIR/cdfjava/jni/Makefile
  else
    OUTMAKE=$SRCDIR/cdfjava/jni/Makefile
  fi
  sed < $SRCDIR/cdfjava/jni/Makefile.in "
/^SHARED *=/s#=.*#=$SHAREDyn#
/^PIC *=/s#=.*#=$PIC#
/^CCx *=/s#=.*#=$CC#
/^LDx *=/s#=.*#=$LD#
/^COPTIONS *=/s#=.*#=$CFLAGS#
/^COPTIONSld *=/s#=.*#=$LDFLAGS#
/^SYSLIBS *=/s#=.*#=$SYSLIBS#
/^JNIEXT *=/s#=.*#=$JNIEXT#
/^SHAREDEXT *=/s#=.*#=$SHAREDEXT#
/^LDOPTIONS *=/s#=.*#=$LDOPTIONSx#
/^JDK_HOME *=/s#=.*#=$JDK_HOMEx#
/^JNIOS *=/s#=.*#=$JNIOS#
/^VERSION *=/s#=.*#=$VERSION#
/^SRCDIR *=/s#=.*#=$SRCDIRj#
/^BUILDDIR *=/s#=.*#=$BUILDDIRj#
" > $OUTMAKE
fi

cd $curdir
$(rm -f test*.o test*.c test*.f)
$(rm -f ztest*)
$(rm -f a.*)

leave 0
