:
#!/bin/sh -x
# The above ":" is necessary on some buggy systems.

#==============================================================================
# unix/configure: Detect feature support and pass that information to Makefile.
# - For Zip, ZipNote, ZipCloak and ZipSplit            Last update: 13 Dec 2012
#==============================================================================

#==============================================================================
# Purpose:
#
# This unix/configure script is invoked by unix/Makefile for generic targets
# to probe the system, the compiler, and other parts of the environment to
# determine values for system-dependent "make" macros.  It generates the
# files "$PROD/flags" and "$PROD/flags_bz" that hold the flags used by
# unix/Makefile to enable or disable features based on available support for
# them.
#
# This script checks for the build and manual directories, and will try to
# create them if necessary.
#
# This shell script is typically invoked by unix/Makefile (via sh) rather than
# being executed directly by the user.
#
# To build Zip automatically using this file, type:
#   make -f unix/Makefile generic
# If this fails, then type "make list" to get a list of specific targets.
# However, the specific targets tend to be out of date and may not enable all
# available features.  In many cases, the specific targets may not even work
# anymore.
#
#
# Parameters:
#
# Setting parameters allows enabling optional features (such as optional
# compression methods), disabling features, and pointing the script to
# available libraries.
#
# Parameters are usually provided by the Makefile and are specified by the
# user on the initial "make" invocation.   Note that some parameters take
# values, in the form "var=value", where var is one of the parameters listed
# below.
#
# In most cases, setting a parameter to 1 enables a feature and to 0 (or
# omitting the parameter) disables it.
#
#
# Environment variables:
#
# Compilation parameters can also be set in the environment.
# export "var=value" for var names like:
#   LOCAL_ZIP
#
#
# Zip Ports:
#
# This configure script supports a number of non-unix Zip ports, where Zip
# is built under Unix (or a Unix work-alike), but the resulting Zip program
# may be for another system or execution environment.  This script will be
# invoked from either unix/Makefile, or from a port-specific Makefile.  The
# port may be explicitly identified by the PORT parameter, or implicitly
# detected by compiler configuration tests.
#
# Supported ports include:
#   msdos : DOS port built using the DJGPP native or cross-compiler
#   os2   : OS/2 port
#   qdos  : Sinclair Q-DOS port built using a gcc cross-compiler
#   zos   : z/OS MVS port built under z/OS UNIX
#   zvm   : z/VM CMS port built under z/VM UNIX
#
#==============================================================================
# Parameter and Environment variables supported:
#
#   Build utility configuration:
#    PROD        - Build directory root (default: .)
#    PORT        - Port name (default: unix)
#
#    CC          - C Compiler program name (for core components)
#                                          (default: cc)
#    CC_BZ       - C Compiler program name (for BZIP2 components)
#                                          (default: cc)
#    CPP         - C Preprocessor program name (default: $CC -E)
#    AS          - Assembler program name (default: $CC)
#    LD          - Linker program name (default: $CC)
#    RANLIB      - Archive index program name (default: ranlib)
#
#    CFLAGS      - C Compiler flag initial values
#    CFLAGS_OPT  - C Compiler optimization option override (non-null)
#    LOCAL_ZIP   - C compiler flag user override values
#
#    AFLAGS      - Assembler flag initial values
#
#    LFLAGS1     - Linker flag initial values (before output file spec)
#    LFLAGS2     - Linker flag initial values (after output file spec)
#
#    OSDEP_H     - OS-dependent C header file names
#    OSDEP_OCU   - OS-dependent C object file names for utilities
#    OSDEP_OCZ   - OS-dependent C object file names for zip
#    OSDEP_OSU   - OS-dependent Assembler object file names for utilities
#    OSDEP_OSZ   - OS-dependent Assembler object file names for zip
#
#    PGMEXT      - Binary program file extension
#    DLLEXT      - Binary program DLL (.so) file extension
#
#    LIST        - Enable compile/linker listing generation
#
#   Zip optional feature configuration:
#    AES_WG      - Enable AES encryption (WinZip/Gladman)
#    IZ_BZIP2    - Enable BZIP2 compression
#    IZ_ZLIB     - Use ZLIB for deflate compression
#    LIBZIP      - Build Zip object library
#    LZMA        - Enable LZMA compression
#    PPMD        - Enable PPMd compression
#
#==============================================================================
# Build utility generated configuration flags:
#
#  - Generated based on build utility program, platform and features selected
#  - Generated flags are appended to initial user-specified value
#  - LOCAL_ZIP value is appended to final CF and CF_BZ values
#
#  CC          - C Compiler program name (for core components)
#  CC_BZ       - C Compiler program name (for BZIP2 components)
#  CF          - C Compiler flag values (for core components)
#  CF_BZ       - C Compiler flag values (for BZIP2 components)
#  CPP         - C Preprocessor program name
#  AS          - Assembler program name
#  AF          - Assembler flag initial values
#  LD          - Linker program name
#  LF1         - Linker flags (before output file spec)
#  LF2         - Linker flags (after output file spec)
#  AR          - Archive program name (and options)
#  RANLIB      - Archive index program name
#  LN          - File link program name (and options)
#  PROD        - Root build directory
#  PORT        - Port name build control
#  IZ_BZIP2    - BZIP2 compression build directory
#  IZ_ZLIB     - ZLIB compression build directory
#  LIB_AES_WG  - AES (WinZip/Gladman) encryption build dependencies
#  LIB_BZ      - BZIP2 compression build dependencies
#  LIB_LZMA    - LZMA compression build dependencies
#  LIB_PPMD    - PPMd compression build dependencies
#  LIB_Z       - ZLIB compression build dependencies
#  LIB_ZIP     - Zip object library
#  MK_BZ       - BZIP2 compression build makefile
#  OSDEP_H     - OS-dependent C header file names
#  OSDEP_OCU   - OS-dependent C object file names for utilities
#  OSDEP_OCZ   - OS-dependent C object file names for zip
#  OSDEP_OSU   - OS-dependent Assembler object file names for utilities
#  OSDEP_OSZ   - OS-dependent Assembler object file names for zip
#  PGMEXT      - Binary program file extension
#  DLLEXT      - Binary program DLL (.so) file extension
#  BINDIR      - Binary program installation directory
#  BINPERMS    - Binary program installation file permissions
#  MANEXT      - Manual page file extension
#  MANDIR      - Manual page installation directory
#  MANPERMS    - Manual page installation file permissions
#
#==============================================================================

trap "rm -f conftest* core a.out; exit 1" 1 2 3 15

#==============================================================================
# Evaluate command-line "make"-macro-like var=value expressions.
#==============================================================================
echo ''
echo 'Generating system-dependent flags...'

# Evaluate only variables in this list.  Complain about others, but continue.
valid_vars1='|AES_WG|AFLAGS|AS|CC|CC_BZ|CFLAGS|CFLAGS_OPT|CPP|DLLEXT'
valid_vars2='|IZ_BZIP2|IZ_ZLIB|LD|LFLAGS1|LFLAGS2|LIST|LOCAL_ZIP|LZMA'
valid_vars3='|OSDEP_H|OSDEP_OCU|OSDEP_OCZ|OSDEP_OSU|OSDEP_OSZ'
valid_vars4='|PGMEXT|PPMD|PROD|RANLIB|'
valid_vars="${valid_vars1}${valid_vars2}${valid_vars3}${valid_vars4}"

argc=$#
while [ ${argc} -gt 0 ]; do
  var=` echo "${1}" | sed -e 's/=.*//' `
  val=` echo "${1}" | sed -e 's/^[^=]*=//' `
  if echo "${valid_vars}" | grep "|${var}|" >/dev/null 2>&1 ; then
    eval ${1}
  else
    echo "   Unrecognized variable name \"${var}\" in \"${1}\"."
  fi
  shift
  argc=` expr ${argc} - 1 `
done

#==============================================================================
# Build utility names, command-line option variables, default values.
#==============================================================================
CC=${CC:-cc}                               # C compiler name (core components)
CC_BZ=${CC_BZ:-${CC}}                      # C compiler name (BZIP2 components)
CC_LIST=''                                 # C compiler name, if LIST enabled
CFLAGS=${CFLAGS:-}' -I.'                   # C compiler flags (core components)
CFLAGS_BZ=${CFLAGS_BZ:-}' -I'              # C cmplr flags (BZIP2 components)
CFLAGS_OPT=${CFLAGS_OPT:-}                 # C cmplr optimze (and list) options
CFLAGS_USR=${LOCAL_ZIP:-}                  # C compiler user override options

CPP=${CPP:-"${CC} -E"}                     # C preprocessor name

AS=${AS:-"${CC}"}                          # Assembler name (core components)
AFLAGS=${AFLAGS:-}                         # Assembler flags

LD=${LD:-"${CC}"}                          # Linker name
LD_LIST=''                                 # Linker name, if LIST enabled
LFLAGS1=${LFLAGS1:-}                       # Linker options, part 1
LFLAGS2=${LFLAGS2:--s}                     # Linker options, part 2

CC_TST=$CC                                 # C cmplr/options for feature tests
BIF_TST=''                                 # C builtin fnctn feature test flags
CFLAGS_TST=''                              # C cmplr feature test optns (core)
CFLAGS_TST_BZ=''                           # C cmplr feature test optns (BZIP2)

CPP_TST=$CPP                               # C preprocessor for feature tests

AR='ar cq'                                 # Object archive prgrm name/options
AR_BZ='ar'                                 #                           (BZIP2)
RANLIB=${RANLIB:-ranlib}                   # Object archive index utility

LN='ln -s'                                 # Create symbolic link
RM='rm -f'                                 # Remove file(s)

# Miscellaneous command macros.
MKDIR='mkdir'                              # Create directory

# Build Directories
PROD=${PROD:-.}                            # Root
PROD_MAN="${PROD}/manout"                  # Manuals

# Build Controls
PORT=${PORT:-}                             # Zip port name
                                           # - Configure tests may set if null
                                           # - Default is "unix" (set below).

PORT_SPEC=''                               # Port specification method
                                           # - Explicit ('E') via PORT=port
                                           #   Implicit ('I') via config test

AES_WG=${AES_WG:-}                         # AES (WinZip/Gladman) encryption
IZ_BZIP2=${IZ_BZIP2:-}                     # BZIP2 compression
IZ_ZLIB=${IZ_ZLIB:-}                       # ZLIB compression
LZMA=${LZMA:-}                             # LZMA compression
PPMD=${PPMD:-}                             # PPMd compression

# Generated Build Dependencies for "make".
# These contain an object library name, if the feature is enabled.
LIB_AES_WG=''                              # AES (WinZip/Gladman) encryption
LIB_BZ=''                                  # BZIP2 compression
LIB_LZMA=''                                # LZMA compression
LIB_PPMD=''                                # PPMd compression
LIB_Z=''                                   # ZLIB compression
LIB_ZIP=''                                 # Zip object library

# Build makefiles.
MK_BZ='Makefile'                           # BZIP2 compression

# OS-dependent files.
# Defaults to unix files, customized based on PORT and tests.
OSDEP_H=${OSDEP_H:-'unix/osdep.h'}         # C header file names
OSDEP_OCU=${OSDEP_OCU:-"${PROD}/unix.o"}   # C object file names, utilities
OSDEP_OCZ=${OSDEP_OCZ:-"${PROD}/unix.o"}   # C object file names, zip
OSDEP_OSU=${OSDEP_OSU:-}                   # Asm object file names, utilities
OSDEP_OSZ=${OSDEP_OSZ:-}                   # Asm object file names, zip

# OS-dependent file extensions.
# Defaults to unix files, customized based on PORT and tests.
PGMEXT=${PGMEXT:-}                         # Program file extension
DLLEXT=${DLLEXT:-'.so'}                    # DLL (.so) file extension
MANEXT=${MANEXT:-'.l'}                     # man page file extension

# OS-dependent installation directories.
BINDIR=${BINDIR:-"$HOME/bin"}              # Where to install executables
MANDIR=${MANDIR:-'manl'}                   # Where to install man pages

# OS-dependent installation file permissions.
BINPERMS=${BINPERMS:-'755'}                # Executable programs
MANPERMS=${MANPERMS:-'644'}                # man pages

# System identification (raw).
UNAME_S=''                                 # System/kernel name
UNAME_V=''                                 # System/kernel major version
UNAME_R=''                                 # System/kernel minor version
UNAME_O=''                                 # Operating system
UNAME_M=''                                 # Machine hardware name
UNAME_P=''                                 # Processor name
UNAME_X=''                                 # Expanded system info (not saved)
SIZER_V=''                                 # Tru64 system/kernel info


#==============================================================================
# Check for build root directory.  Create if required.
#==============================================================================
echo "Check build root directory: (${PROD})"
if [ -d "${PROD}" ]; then
  echo "-- build root directory OK"
elif [ -f "${PROD}" ]; then
  echo "^^ directory name exists as an ordinary file.  Unable to build"
  exit 1
else
  echo "-- Creating build root directory"
  ${MKDIR} ${PROD}
  status=$?
  if [ $status -ne 0 ]; then
    echo ''
    echo "^^ directory creation failed (${status})"
    exit $status
  fi
fi


#==============================================================================
# Check for build manuals directory.  Create if required.
#==============================================================================
echo "Check build directory for manuals: (${PROD_MAN})"
if [ -d "${PROD_MAN}" ]; then
  echo "-- build manuals directory is OK"
elif [ -f "${PROD_MAN}" ]; then
  echo "^^ directory name exists as an ordinary file.  Unable to build"
  exit 1
else
  echo "-- Creating build manuals directory"
  ${MKDIR} ${PROD_MAN}
  status=$?
  if [ $status -ne 0 ]; then
    echo ''
    echo "^^ directory creation failed (${status})"
    exit $status
  fi
fi


#==============================================================================
# Assign default PORT.  Note if explicit or implicit specification.
#==============================================================================
if [ -z "${PORT}" ]; then
  PORT='unix'
  PORT_SPEC='I'
else
  PORT_SPEC='E'
fi


#==============================================================================
# Query the system information.
#==============================================================================
echo "Query for system identification"
UNAME_S=` uname -s 2>/dev/null `
case "${UNAME_S}" in
   'OS/390' )
      # z/OS kernel still announces itself as OS/390 (for compatibility)
      # - Must request for current product info via -I
      UNAME_S=` uname -Is 2>/dev/null `
      UNAME_V=` uname -Iv 2>/dev/null `
      UNAME_R=` uname -Ir 2>/dev/null `
      UNAME_M=` uname -Im 2>/dev/null `
      ;;

   *  )
      # All others use standard uname query
      UNAME_V=` uname -v 2>/dev/null `
      UNAME_R=` uname -r 2>/dev/null `
      UNAME_O=` uname -o 2>/dev/null `
      UNAME_M=` uname -m 2>/dev/null `
      UNAME_P=` uname -p 2>/dev/null `
      UNAME_X=` uname -X 2>/dev/null `

      # Use sizer to capture more system configuration details
      # - Tru64 UNIX, and relatives
      if [ -f /usr/sbin/sizer ]; then
        SIZER_V=` /usr/sbin/sizer -v 2>/dev/null | \
                sed -e 's/^[^ ]* [^ ]* [^ ]* \([^ ]*\) .*/\1/' `
      fi
      ;;
esac

echo "  OS name: ${UNAME_S}"


#==============================================================================
# Validate basic compiler and linker operation.
#==============================================================================
echo "Check C compiler+linker operation"
cat > conftest.c << _EOF_
#include <stdio.h>
int main()
{
   printf( "  Ok\n");
   return 0;
}
_EOF_
$CC $CFLAGS -o conftest conftest.c
status=$?
if [ $status -ne 0 ]; then
  echo ''
  echo "^^ C compiler+linker \"${CC}\" does not work as expected."
  echo "^^ Failing command was: $CC $CFLAGS -o conftest conftest.c"
  exit $status
else
  ./conftest
  status=$?
  if [ $status -ne 0 ]; then
    echo "^^ Test executable exited with bad status: ${status}."
    exit $status
  fi
fi


#==============================================================================
# Perform compile- and link-based configuration discovery tests.
#==============================================================================

#------------------------------------------------------------------------------
# A) Determine C compiler type, set appropriate compile and linker options.
#------------------------------------------------------------------------------

# Each compiler is recognized using standard pre-defined preprocessor symbols.
#
# The options set in for each compiler includes the basic options required for
# successful compilation of UnZip, as well as the options for standard code
# optimization.

echo 'Check C compiler type (optimization and listing options)'

if [ -z "${CFLAGS_OPT}" ]; then
  # Sun C?
  cat > conftest.c << _EOF_
int main()
{
#ifndef __SUNPRO_C
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-xO3'
    echo "  Sun C ($CFLAGS_OPT)"
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # Tru64 DEC/Compaq/HP C?
  cat > conftest.c << _EOF_
int main()
{
#ifndef __DECC
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-O3'
    echo "  DEC C ($CFLAGS_OPT)"
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # HP-UX HP C?
  cat > conftest.c << _EOF_
int main()
{
#ifdef __GNUC__
   bad code
#endif
#ifndef __hpux
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    # HP-UX, not GCC.  Lame bundled or real ANSI compiler?
    CFLAGS_OPT_TRY="+O3 +Onolimit"
    $CC $CFLAGS $CFLAGS_OPT_TRY -c conftest.c 2>&1 | \
       grep '(Bundled)' >/dev/null
    status=$?
    if [ $status -ne 0 ]; then
      CFLAGS_OPT="${CFLAGS_OPT_TRY}"
      echo "  HP-UX ANSI C ($CFLAGS_OPT)"
    else
      CFLAGS_OPT=' '    #Blank = Lame bundled compiler recognized.
      echo '  HP-UX Bundled C (no opt)'
    fi
#   Note: Your HP-UX system may require addition of -DNO_MKSTEMP
#         - In HP-UX versions below 11.31, writes to the temporary
#           file may fail.  Refer to INSTALL for details.
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # MinGW 64-bit C?
  # MinGW also defines __GNUC__, so must be tested before GCC.
  cat > conftest.c << _EOF_
int main()
{
#ifndef __MINGW64__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-O3'
    PORT='mingw64'
    echo "  MinGW 64 ($CFLAGS_OPT)"
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # MinGW 32-bit C?
  # MinGW also defines __GNUC__, so must be tested before GCC.
  cat > conftest.c << _EOF_
int main()
{
#ifndef __MINGW32__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-O3'
    PORT='mingw32'
    echo "  MinGW 32 ($CFLAGS_OPT)"
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # LLVM-based C?
  # LLVM also defines __GNUC__, so must be tested before GCC.
  cat > conftest.c << _EOF_
int main()
{
#ifndef __llvm__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    # LLVM-based C.  Determine specific compiler.

    if [ -z "${CFLAGS_OPT}" ]; then
      # LLVM Clang?
      cat > conftest.c << _EOF_
int main()
{
#ifndef __clang__
   bad code
#endif
   return 0;
}
_EOF_
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CC='clang'
        LD='clang'
        CC_TST='clang'
        CFLAGS_OPT='-O3'
        echo "  LLVM Clang ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # LLVM Apple GCC?
      cat > conftest.c << _EOF_
int main()
{
#ifndef __APPLE_CC__
   bad code
#endif
   return 0;
}
_EOF_
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CFLAGS_OPT='-O3'
        echo "  LLVM Apple GCC ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # Default to LLVM GCC.
      CFLAGS_OPT='-O3'
      echo "  LLVM GCC ($CFLAGS_OPT)"
    fi

    # Special Mac OS X shared library "ld" option?
    if [ "${UNAME_S}" = 'Darwin' ]; then
      OSDEP_H="${OSDEP_H} unix/macosx.h"

      # "ld64: warning: option -s is obsolete and being ignored"
      if [ "${LFLAGS2}" = '-s' ]; then
        LFLAGS2=''
      fi

      lflags2_try='-Wl,-search_paths_first'
      $CC $CFLAGS ${lflags2_try} conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        LFLAGS2="${LFLAGS2} ${lflags2_try}"
      fi
      rm -f conftest
    fi
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # Portable C Compiler (PCC)?
  # PCC also defines __GNUC__, so must be tested before GCC.
  cat > conftest.c << _EOF_
int main()
{
#ifndef __PCC__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-O'
    echo "  Portable C Compiler (PCC) ($CFLAGS_OPT)"
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # EKOPath C?
  # EKOPath also defines __GNUC__, so must be tested before GCC.
  cat > conftest.c << _EOF_
int main()
{
#ifndef __PATHCC__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-O3'
    echo "  EKOPAth C ($CFLAGS_OPT)"
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # Intel C?
  # Intel also defines __GNUC__, so must be tested before GCC.
  cat > conftest.c << _EOF_
int main()
{
#ifndef __INTEL_COMPILER
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-O3'
    echo "  Intel C ($CFLAGS_OPT)"
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # GNU C?
  cat > conftest.c << _EOF_
int main()
{
#ifndef __GNUC__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    # GNU C.  Check for special-case platforms.

    if [ -z "${CFLAGS_OPT}" ]; then
      # GNU C for MAC OS X (Darwin)?
      cat > conftest.c << _EOF_
int main()
{
#if !(defined(__APPLE__) || defined(__darwin__))
   bad code
#endif
   return 0;
}
_EOF_
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CFLAGS_OPT='-O3'
        OSDEP_H="${OSDEP_H} unix/macosx.h"

        # "ld64: warning: option -s is obsolete and being ignored"
        if [ "${LFLAGS2}" = '-s' ]; then
          LFLAGS2=''
        fi

        # Special Mac OS X shared library "ld" option?
        lflags2_try='-Wl,-search_paths_first'
        $CC $CFLAGS ${lflags2_try} conftest.c >/dev/null 2>/dev/null
        status=$?
        if [ $status -eq 0 ]; then
          LFLAGS2="${LFLAGS2} ${lflags2_try}"
        fi
        rm -f conftest

        echo "  GNU C for Mac OS X Darwin ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # GNU C for AIX?
      cat > conftest.c << _EOF_
int main()
{
#ifndef _AIX
   bad code
#endif
   return 0;
}
_EOF_
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CC_TST="${CC_TST} -D_ALL_SOURCE"
        CC_TST="${CC_TST} -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1"
        CFLAGS_OPT='-O3'
        CFLAGS_TST="${CFLAGS_TST} -D_ALL_SOURCE"
        CFLAGS_TST="${CFLAGS_TST} -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1"
        echo "  GNU C for AIX ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # Other GNU C
      CFLAGS_OPT='-O3'
      echo "  GNU C ($CFLAGS_OPT)"
    fi
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # IBM C?
  cat > conftest.c << _EOF_
int main()
{
#ifndef __IBMC__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    # IBM C.  Determine platform.

    if [ -z "${CFLAGS_OPT}" ]; then
      # IBM XL C for Linux?
      cat > conftest.c << _EOF_
int main()
{
#ifndef __TOS_LINUX__
   bad code
#endif
   return 0;
}
_EOF_
      C_PARMS="-q64 -qlonglong -qlibansi -qnoignerrno"
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CC='c89'
        LD='c89'
        CC_TST="c89 ${C_PARMS} -D_ALL_SOURCE"
        CC_TST="${CC_TST} -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1"
        CFLAGS_OPT="-O3 ${C_PARMS}"
        CFLAGS_TST="${CFLAGS_TST} -D_ALL_SOURCE"
        CFLAGS_TST="${CFLAGS_TST} -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1"
        LFLAGS2="${LFLAGS2} -q64"
        AR='ar -cq -X64'
        AR_BZ='ar -X64'
        if [ -n "${LIST}" ]; then # Listings enabled?
          CFLAGS_OPT="${CFLAGS_OPT} -qlist -qsource -qshowinc -qreport"
        fi
        echo "  IBM XL C for Linux ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # IBM XL C for AIX?
      cat > conftest.c << _EOF_
int main()
{
#if !(defined(_AIX) && defined(__PPC__))
   bad code
#endif
   return 0;
}
_EOF_
      C_PARMS="-q64 -qlonglong -qlibansi -qnoignerrno"
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CC='c89'
        LD='c89'
        CC_TST="c89 ${C_PARMS} -D_ALL_SOURCE"
        CC_TST="${CC_TST} -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1"
        CFLAGS_OPT="-O3 ${C_PARMS}"
        CFLAGS_TST="${CFLAGS_TST} -D_ALL_SOURCE"
        CFLAGS_TST="${CFLAGS_TST} -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1"
        LFLAGS2="${LFLAGS2} -q64"
        AR='ar -cq -X64'
        AR_BZ='ar -X64'
        if [ -n "${LIST}" ]; then # Listings enabled?
          CFLAGS_OPT="${CFLAGS_OPT} -qlist -qsource -qshowinc -qreport"
        fi
        echo "  IBM XL C for AIX ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # IBM z/OS XL C?
      cat > conftest.c << _EOF_
int main()
{
#ifndef __MVS__
   bad code
#endif
   return 0;
}
_EOF_
      C_PARMS="-Wc,longlong,libansi,noignerrno"
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        # Use c89 to get ANSI
        # - Avoid xlc (terminates with sigkill on compile error)
        CC='c89'
        LD='c89'
        CC_TST="c89 ${C_PARMS}"
        CFLAGS_OPT="-O3 ${C_PARMS} -Wc,'langlvl(extended)'"
        CFLAGS_OPT="${CFLAGS_OPT} -Wc,checkout,'flag(i)',inline"
        CFLAGS_OPT="${CFLAGS_OPT} -Wc,'suppress(ccn3389,ccn3374,ccn3413)'"
        CFLAGS_OPT="${CFLAGS_OPT} -Wc,'suppress(ccn3495,ccn3409,ccn3469)'"
        CFLAGS_OPT="${CFLAGS_OPT} -Wc,'suppress(ccn3457)'"
        LFLAGS1='-Wl,EDIT=NO'
        LFLAGS2=''
        if [ -n "${LIST}" ]; then # Listings enabled?
          CC_LIST="sh ./zos/zcc c89"    # Script does listing capture
          CFLAGS_OPT="${CFLAGS_OPT} -Wc,list,source,showinc,inlrpt"
          CFLAGS_OPT="${CFLAGS_OPT} -Wc,aggr,expmac"
          LD_LIST="sh ./zos/zld c89"    # Script does listing capture
          LFLAGS1="${LFLAGS1} -Wl,map,xref"
        fi
       #FIX_ME[AD]: Switch to 64-bit after 32-bit is fully debugged
       #if echo "${LOCAL_ZIP}" | grep -i ILP32 >/dev/null 2>&1 ; then
       #  :
       #else                        # Default to 64-bit build
       #  CC_TST="${CC_TST} -Wc,lp64 -Wl,lp64"
       #  CFLAGS_OPT="${CFLAGS_OPT} -Wc,lp64"
       #  LFLAGS1="${LFLAGS1} -Wl,lp64"
       #fi
        CC_TST="${CC_TST} -D_ALL_SOURCE -D_OPEN_SYS_TIMED_EXT"
        CC_TST="${CC_TST} -D_XOPEN_SOURCE_EXTENDED=1"
        if [ "${PORT}" = 'unix' ]; then
          CFLAGS_TST="${CFLAGS_TST} -DZOS_UNIX"
        fi
        # Oldest supported z/OS release
        CFLAGS_TST="${CFLAGS_TST} -Wc,'target(zOSV1R10)'"
        CFLAGS_TST="${CFLAGS_TST} -DEBCDIC -DSYSV"
        CFLAGS_TST="${CFLAGS_TST} -D_ALL_SOURCE -D_OPEN_SYS_TIMED_EXT"
        CFLAGS_TST="${CFLAGS_TST} -D_XOPEN_SOURCE_EXTENDED=1"
        RANLIB="echo"               # z/OS UNIX has no ranlib utility
        echo "  IBM z/OS XL C ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # IBM XL C for z/VM?
      cat > conftest.c << _EOF_
int main()
{
#ifndef __VM__
   bad code
#endif
   return 0;
}
_EOF_
      C_PARMS="-Wc,longlong,libansi,noignerrno"
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CC='c89'
        LD='c89'
        CC_TST="c89 ${C_PARMS}"
        CFLAGS_OPT="-O3 ${C_PARMS} -Wc,'langlvl(extended)'"
        CFLAGS_OPT="${CFLAGS_OPT} -Wc,checkout,'flag(i)',inline"
        CFLAGS_OPT="${CFLAGS_OPT} -Wc,'suppress(ccn3389,ccn3374,ccn3413)'"
        CFLAGS_OPT="${CFLAGS_OPT} -Wc,'suppress(ccn3495,ccn3409,ccn3469)'"
        CFLAGS_OPT="${CFLAGS_OPT} -Wc,'suppress(ccn3457)'"
        LFLAGS1='-Wl,EDIT=NO'
        LFLAGS2=''
        if [ -n "${LIST}" ]; then # Listings enabled?
          CC_LIST="sh ./zos/zcc c89"    # Script does listing capture
          CFLAGS_OPT="${CFLAGS_OPT} -Wc,list,source,showinc,inlrpt"
          CFLAGS_OPT="${CFLAGS_OPT} -Wc,aggr,expmac"
          LD_LIST="sh ./zos/zld c89"    # Script does listing capture
          LFLAGS1="${LFLAGS1} -Wl,map,xref"
        fi
        CC_TST="${CC_TST} -D_ALL_SOURCE -D_OPEN_SYS_TIMED_EXT"
        CC_TST="${CC_TST} -D_XOPEN_SOURCE_EXTENDED=1"
        if [ "${PORT}" = 'unix' ]; then
          CFLAGS_TST="${CFLAGS_TST} -DZOS_UNIX"
        fi
        CFLAGS_TST="${CFLAGS_TST} -DEBCDIC -DSYSV"
        CFLAGS_TST="${CFLAGS_TST} -D_ALL_SOURCE -D_OPEN_SYS_TIMED_EXT"
        CFLAGS_TST="${CFLAGS_TST} -D_XOPEN_SOURCE_EXTENDED=1"
        RANLIB="echo"               # z/VM UNIX has no ranlib utility
        echo "  IBM XL C for z/VM ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # IBM ILE C for iSeries?
      cat > conftest.c << _EOF_
int main()
{
#ifndef __OS400__
   bad code
#endif
   return 0;
}
_EOF_
      $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CC='c89'
        LD='c89'
        CC_TST='c89'
        CFLAGS_OPT='-O3 -Wc,inline'
        if [ -n "${LIST}" ]; then # Listings enabled?
          CFLAGS_OPT="${CFLAGS_OPT} -qlist -qsource -qshowinc -qreport"
        fi
        echo "  IBM ILE C for iSeries ($CFLAGS_OPT)"
      fi
    fi

    if [ -z "${CFLAGS_OPT}" ]; then
      # IBM XL C for other platform.
      CFLAGS_OPT='-O3'
      echo "  IBM XL C ($CFLAGS_OPT)"
    fi
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # DJGPP C?
  cat > conftest.c << _EOF_
int main()
{
#ifndef __DJGPP__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-O3'
    echo "  DJGPP C ($CFLAGS_OPT)"
    # Set port values, if defaulted.
    if [ -z "${PORT}" ]; then
      PORT='msdos'
    fi
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  # Cygnus C?
  cat > conftest.c << _EOF_
int main()
{
#ifndef __CYGWIN__
   bad code
#endif
   return 0;
}
_EOF_
  $CC $CFLAGS -c conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -eq 0 ]; then
    CFLAGS_OPT='-O3'
    echo "  Cygwin C ($CFLAGS_OPT)"
    # Set port values, if defaulted.
    if [ -z "${PORT}" ]; then
      PORT='msdos'
    fi
  fi
fi

if [ -z "${CFLAGS_OPT}" ]; then
  CFLAGS_OPT='-O'
  echo "  Other-unknown C ($CFLAGS_OPT)"
fi

if [ "${CC}" != "${CC_TST}" ]; then
  echo "  Feature test compiler updated (${CC_TST})"
fi

#------------------------------------------------------------------------------
# B) Finalize specific customizations for target port.
#------------------------------------------------------------------------------
if [ "${PORT}" = "unix" ]; then
  echo "Standard Unix target"
  CFLAGS="${CFLAGS} -DUNIX"
  CFLAGS_BZ="${CFLAGS_BZ} -DUNIX"
else
  echo "Set build configuration for target \"${PORT}\" port"
  case "${PORT}" in
     unix )
        # UNIX values set in defaults, customized based on tests.
        ;;

     mingw* )
        CFLAGS="${CFLAGS} -DWIN32"
        CFLAGS_BZ="${CFLAGS_BZ} -D_WIN32"
        OSDEP_H='win32/osdep.h win32/zipup.h'
        OSDEP_OCU="${PROD}/win32.o ${PROD}/win32i64.o"
        OSDEP_OCZ="${PROD}/nt.o ${OSDEP_OCU} ${PROD}/win32zip.o"
        OSDEP_OSU=''
        OSDEP_OSZ=''
        PGMEXT='.exe'
        DLLEXT='.dll'
        ;;

     msdos )
        CFLAGS="${CFLAGS} -DMSDOS"
        CFLAGS_BZ="${CFLAGS_BZ} -DMSDOS"
        OSDEP_H='msdos/osdep.h msdos/zipup.h'
        OSDEP_OCU="${PROD}/msdos.o"
        OSDEP_OCZ="${PROD}/msdos.o"
        OSDEP_OSU=''
        OSDEP_OSZ=''
        PGMEXT='.EXE'
        DLLEXT='.DLL'
        ;;

     os2 )
        CFLAGS="${CFLAGS} -DOS2"
        CFLAGS_BZ="${CFLAGS_BZ} -DOS2"
        OSDEP_H='os2/os2acl.h  os2/os2zip.h  os2/osdep.h  os2/zipup.h'
        OSDEP_OCU="${PROD}/os2.o"
        OSDEP_OCZ="${PROD}/os2.o"
        OSDEP_OSU=''
        OSDEP_OSZ=''
        PGMEXT='.EXE'
        DLLEXT='.DLL'
        ;;

     qdos )
        CFLAGS="${CFLAGS} -DQDOS"
        CFLAGS_BZ="${CFLAGS_BZ} -DQDOS"
        OSDEP_H='qdos/osdep.h  qdos/zipup.h'
        OSDEP_OCU="${PROD}/qdos.o ${PROD}/qfileio.o"
        OSDEP_OCZ="${PROD}/qdos.o ${PROD}/qfileio.o"
        OSDEP_OSU=''
        OSDEP_OSZ=''
        PGMEXT=''
        DLLEXT=''
        ;;

     zos )
        CFLAGS="${CFLAGS} -DMVS"
        CFLAGS_BZ="${CFLAGS_BZ} -DMVS"
        OSDEP_H='zos/cmsmvs.h  zos/mvs.h  zos/zipup.h'
        OSDEP_OCU="${PROD}/mvs.o"
        OSDEP_OCZ="${PROD}/mvs.o"
        OSDEP_OSU=''
        OSDEP_OSZ=''
        PGMEXT=''
        DLLEXT=''
        ;;

     zvm )
        CFLAGS="${CFLAGS} -DVM_CMS"
        CFLAGS_BZ="${CFLAGS_BZ} -DVM_CMS"
        OSDEP_H='zvm/cstat.h zos/cmsmvs.h  zos/zipup.h'
        OSDEP_OCU="${PROD}/cms.o"
        OSDEP_OCZ="${PROD}/cms.o"
        OSDEP_OSU=''
        OSDEP_OSZ=''
        PGMEXT=''
        DLLEXT=''
        ;;

     *  )
        echo "^^ Zip port not recognized.  Unable to build"
        exit 1
        ;;
  esac
fi

#------------------------------------------------------------------------------
# C) Determine C preprocessor type.
#------------------------------------------------------------------------------
echo 'Check C preprocessor type'
case "${UNAME_S}" in
   'z/OS'|'z/VM')
      # z/OS UNIX, z/VM UNIX requires wrapper script for CPP tests.
      # "cc -E" returns 0 even when warning/error detected.
      CPP="${CC} -E"

      CPP_TST="sh ./zos/zcpp ${CPP}"   # Wrapper script checks stderr.
      ;;

   *  )
      # Other UNIX systems.
      # On SVR4, cc -E does not produce correct assembler files.  Use /lib/cpp.
      # Solaris as(1) needs -P, maybe others as well ?
      CPP="${CC} -E"
      [ -f /usr/ccs/lib/cpp ] && CPP='/usr/ccs/lib/cpp -P'
      [ -f /usr/lib/cpp ]     && CPP='/usr/lib/cpp'
      [ -f /lib/cpp ]         && CPP='/lib/cpp'
      [ -f /usr/bin/cpp ]     && CPP='/usr/bin/cpp'
      [ -f /xenix ]           && CPP="${CC} -E"
      [ -f /lynx.os ]         && CPP="${CC} -E"

      # Verify operation with tentative CPP.
      echo "#include <stdio.h>" >conftest.c
      $CPP conftest.c >/dev/null 2>/dev/null || CPP="${CC} -E"

      CPP_TST="${CPP}"
      ;;
esac

#------------------------------------------------------------------------------
# D) Determine if assembler match and CRC routines can be used.
#------------------------------------------------------------------------------
case "${UNAME_S}" in
   'z/OS'|'z/VM')
      # z/OS UNIX, z/VM UNIX can not run i386 code.
      # Host assembler uses unique internal macro phase, not CPP.
      ;;

   *  )
      # Other UNIX systems.
      # FIX_ME: Need to customize for ports.
      echo 'Check if we can (and want to) use assembly code for CRC and match'
      asm_crc=0
      asmv=0
      # CRC.  Fail intentionally, if NO_ASM or NO_ASM_CRC is defined.
      cat > conftest.S << _EOF_
#if defined( NO_ASM) || defined( NO_ASM_CRC)
    bad code
#else
# include "crc_i386.S"
#endif
_EOF_
      ${CPP_TST} ${CFLAGS} conftest.S > _crc_i386.s 2> /dev/null
      status=$?
      if [ $status -eq 0 -a -s _crc_i386.s ]; then
        grep error _crc_i386.s > /dev/null
        status=$?
        if [ $status -ne 0 ]; then
          $AS -c _crc_i386.s > /dev/null 2> /dev/null
          status=$?
          if [ $status -eq 0 -a -f _crc_i386.o ]; then
            echo '-- Using assembly code for CRC'
            asm_crc=1
            CFLAGS_TST="${CFLAGS_TST} -DASM_CRC"
            OSDEP_OSU="${OSDEP_OSU} ${PROD}/crc_gcc.o"
            OSDEP_OSZ="${OSDEP_OSZ} ${PROD}/crc_gcc.o"
          fi
        fi
      fi
      # Match.  Fail intentionally, if NO_ASM or NO_ASMV is defined.
      cat > conftest.S << _EOF_
#if defined( NO_ASM) || defined( NO_ASMV)
    bad code
#else
# include "match.S"
#endif
_EOF_
      ${CPP_TST} ${CFLAGS} conftest.S > _match.s 2> /dev/null
      status=$?
      if [ $status -eq 0 -a -s _match.s ]; then
        grep error _match.s > /dev/null
        status=$?
        if [ $status -ne 0 ]; then
          $AS -c _match.s > /dev/null 2> /dev/null
          status=$?
          if [ $status -eq 0 -a -f _match.o ]; then
            echo '-- Using assembly code for match'
            asmv=1
            CFLAGS_TST="${CFLAGS_TST} -DASMV"
            OSDEP_OSZ="${OSDEP_OSZ} ${PROD}/mch_gcc.o"
          fi
        fi
      fi
      # If using assembler for anything, then check the C compiler for a
      # "_" prefix on global names.
      if [ $asmv -ne 0 -o $asm_crc -ne 0 ]; then
        echo 'Check if C compiler adds "_" prefix to global names'
        echo "int foo() { return 0;}" > conftest.c
        $CC -c conftest.c >/dev/null 2>/dev/null
        nm conftest.o | grep foo >/dev/null 2>/dev/null
        status=$?
        if [ $status -eq 0 ]; then
          # Found "foo".  Look for "_foo".
          nm conftest.o | grep _foo >/dev/null 2>/dev/null
          status=$?
          [ $status -ne 0 ] && AFLAGS="${AFLAGS} -DNO_UNDERLINE"
        fi
      fi
      rm -f conftest.c conftest.S _match.s _match.o _crc_i386.s _crc_i386.o

      # Windows (MinGW) assumes ASMV and ASM_CRC unless dissuaded.
      if [ $asmv -eq 0 -a $asm_crc -eq 0 ]; then
        CFLAGS_TST="${CFLAGS_TST} -DNO_ASM"
      else
        if [ $asmv -eq 0 ]; then
          CFLAGS_TST="${CFLAGS_TST} -DNO_ASMV"
        fi
        if [ $asm_crc -eq 0 ]; then
          CFLAGS_TST="${CFLAGS_TST} -DNO_ASM_CRC"
        fi
      fi
      ;;
esac

lprod=0
#------------------------------------------------------------------------------
# E) Configure optional AES (WinZip/Gladman) encryption library.
#------------------------------------------------------------------------------
# AES (WinZip/Gladman) encryption.  (User must request: "AES_WG=1".)
if [ -n "${AES_WG}" ]; then
  echo "Check AES_WG (AES WinZip/Gladman) encryption support"
  if [ -d "aes_wg" ]; then
    echo "  Check for AES_WG header file in aes_wg directory"
    if [ -f "aes_wg/aes.h" ]; then
      cat > conftest.c << _EOF_
int main()
{
#include "aes_wg/aes.h"
#ifndef CRYPT_AES_WG
   forget it
#endif
   return 0;
}
_EOF_
      $CC_TST $CFLAGS -DCRYPT_AES_WG -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        LIB_AES_WG="libaes.a"
        CFLAGS_TST="${CFLAGS_TST} -DCRYPT_AES_WG"
        if [ $lprod -eq 0 ]; then
          lprod=1
          LFLAGS1="${LFLAGS1} -L${PROD}"
        fi
        LFLAGS2="${LFLAGS2} -laes"
        echo "-- Including AES_WG encryption support"
        echo "-- Building libaes.a in ${PROD}"
      else
        echo "^^ AES_WG test build failed. Check aes_wg directory"
        echo "^^ Unable to include AES_WG encryption support"
      fi
    else
      echo "^^ AES_WG source code not found in aes_wg directory"
      echo "^^ Unable to include AES_WG encryption support"
    fi
  else
    echo "^^ Directory aes_wg not found"
    echo "^^ Unable to include AES_WG encryption support"
  fi
else
  echo "No AES WG encryption"
fi

#------------------------------------------------------------------------------
# F) Configure optional LZMA compression library.
#------------------------------------------------------------------------------
# LZMA compression.  (User must request: "LZMA=1".)
if [ -n "${LZMA}" ]; then
  echo "Check LZMA compression support"
  if [ -d "szip" ]; then
    echo "  Check for LZMA header file in szip/"
    if [ -f "szip/LzmaDec.h" ]; then
      cat > conftest.c << _EOF_
#include "szip/LzmaEnc.h"
int main()
{
   typedef int zoff_t;
#ifndef LZMA_SUPPORT
   forget it
#endif
   return 0;
}
_EOF_
      $CC_TST $CFLAGS -DLZMA_SUPPORT -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        LIB_LZMA="liblzma.a"
        CFLAGS_TST="${CFLAGS_TST} -DLZMA_SUPPORT"
        if [ $lprod -eq 0 ]; then
          lprod=1
          LFLAGS1="${LFLAGS1} -L${PROD}"
        fi
        LFLAGS2="${LFLAGS2} -llzma"
        echo "-- Including LZMA compression support"
        echo "-- Building liblzma.a in ${PROD}"
      else
        echo "^^ LZMA test build failed. Check szip directory"
        echo "^^ Unable to include LZMA compression support"
      fi
    else
      echo "^^ LZMA source code not found in szip directory"
      echo "^^ Unable to include LZMA compression support"
    fi
  else
    echo "^^ Directory szip not found"
    echo "^^ Unable to include LZMA compression support"
  fi
else
  echo "No LZMA compression"
fi

#------------------------------------------------------------------------------
# F') Configure optional PPMd compression library.
#------------------------------------------------------------------------------
# PPMd compression.  (User must request: "PPMD=1".)
if [ -n "${PPMD}" ]; then
  echo "Check PPMd compression support"
  if [ -d "szip" ]; then
    echo "  Check for PPMd header file in szip/"
    if [ -f "szip/Ppmd.h" ]; then
      cat > conftest.c << _EOF_
#include "szip/Ppmd.h"
int main()
{
   typedef int zoff_t;
#ifndef PPMD_SUPPORT
   forget it
#endif
   return 0;
}
_EOF_
      $CC_TST $CFLAGS -DPPMD_SUPPORT -c conftest.c >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        LIB_PPMD="libppmd.a"
        CFLAGS_TST="${CFLAGS_TST} -DPPMD_SUPPORT"
        if [ $lprod -eq 0 ]; then
          lprod=1
          LFLAGS1="${LFLAGS1} -L${PROD}"
        fi
        LFLAGS2="${LFLAGS2} -lppmd"
        echo "-- Including PPMd compression support"
        echo "-- Building libppmd.a in ${PROD}"
      else
        echo "^^ PPMd test build failed. Check szip directory"
        echo "^^ Unable to include PPMd compression support"
      fi
    else
      echo "^^ PPMD source code not found in szip directory"
      echo "^^ Unable to include PPMd compression support"
    fi
  else
    echo "^^ Directory szip not found"
    echo "^^ Unable to include PPMd compression support"
  fi
else
  echo "No PPMd compression"
fi

#------------------------------------------------------------------------------
# G) Configure optional BZIP2 compression library.
#------------------------------------------------------------------------------
# BZIP2 compression.  (User must request: "IZ_BZIP2=bzip2_dir".)
if [ -n "${IZ_BZIP2}" ]; then
  echo "Check BZIP2 compression support"
  if [ -d "${IZ_BZIP2}" ]; then
    echo "  Check for BZIP2 header file in IZ_BZIP2 (${IZ_BZIP2})"
    if [ -f "${IZ_BZIP2}/bzlib.h" ]; then
      if [ "${IZ_BZIP2}" = "bzip2" ]; then
        echo "  BZIP2 files found within Zip tree"
        echo "  Check for BZIP2 compiled library in IZ_BZIP2 (${IZ_BZIP2})"
        if [ -f "${IZ_BZIP2}/libbz2.a" ]; then
#
#         A bzip2 library built with BZ_NO_STDIO should have an
#         unresolved external, "bz_internal_error".  The default,
#         full-function library will not mention it.
#
          nm ${IZ_BZIP2}/libbz2.a | grep bz_internal_error >/dev/null
          status=$?
          if [ $status -eq 0 ]; then
            echo "    Found BZIP2 BZ_NO_STDIO library, ${IZ_BZIP2}/libbz2.a"
            LIB_BZ="${IZ_BZIP2}/libbz2.a"
            CFLAGS_TST="${CFLAGS_TST} -I${IZ_BZIP2} -DBZIP2_SUPPORT"
            LFLAGS1="${LFLAGS1} -L${IZ_BZIP2}"
            LFLAGS2="${LFLAGS2} -lbz2"
            echo "-- Including BZIP2 compression support"
            echo "-- Using libbz2.a in IZ_BZIP2 (${IZ_BZIP2})"
          else
            echo "    -----------------------------------------------------"
            echo "    Found BZIP2 library, ${IZ_BZIP2}/libbz2.a, but the"
            echo "      library was not compiled with BZ_NO_STDIO"
            echo "    ERROR: This (default) variant of bzip2 library is NOT"
            echo "      supported with Zip because of its incompatible"
            echo "      error handling!"
            echo "      Please see the Zip installation instructions in"
            echo "      the INSTALL text file."
            echo "    -----------------------------------------------------"
            echo "  ^^ Incompatibe BZIP2 support within the Zip tree"
            echo "^^ Unable to include BZIP2 compression support"
          fi
        else
          echo "  Check for BZIP2 Makefile in I_BZIP2 (${IZ_BZIP2})"
          if [ -f "${IZ_BZIP2}/Makefile" ]; then
            LIB_BZ="${IZ_BZIP2}/libbz2.a"
            CFLAGS_TST="${CFLAGS_TST} -I${IZ_BZIP2} -DBZIP2_SUPPORT"
            LFLAGS1="${LFLAGS1} -L${IZ_BZIP2}"
            LFLAGS2="${LFLAGS2} -lbz2"
            echo "-- Including BZIP2 compression support"
            echo "-- Building libbz2.a in ${IZ_BZIP2}"
          else
            echo "  ^^ Incomplete BZIP2 support within the Zip tree"
            echo "^^ Unable to include BZIP2 compression support"
          fi
        fi
      else
        echo "  BZIP2 support is outside the Zip tree"
        echo "  Check for BZIP2 compiled library in IZ_BZIP2 (${IZ_BZIP2})"
        if [ -f "${IZ_BZIP2}/libbz2.a" ]; then
#
#         A bzip2 library built with BZ_NO_STDIO should have an
#         unresolved external, "bz_internal_error".  The default,
#         full-function library will not mention it.
#
          nm ${IZ_BZIP2}/libbz2.a | grep bz_internal_error >/dev/null
          status=$?
          if [ $status -eq 0 ]; then
            echo "    Found BZIP2 BZ_NO_STDIO library, ${IZ_BZIP2}/libbz2.a"
            LIB_BZ="${IZ_BZIP2}/libbz2.a"
            CFLAGS_TST="${CFLAGS_TST} -I${IZ_BZIP2} -DBZIP2_SUPPORT"
            LFLAGS1="${LFLAGS1} -L${IZ_BZIP2}"
            LFLAGS2="${LFLAGS2} -lbz2"
            echo "-- Including BZIP2 compression support"
            echo "-- Using libbz2.a in IZ_BZIP2 (${IZ_BZIP2})"
          else
            echo "    -----------------------------------------------------"
            echo "    Found BZIP2 library, ${IZ_BZIP2}/libbz2.a, but the"
            echo "      library was not compiled with BZ_NO_STDIO"
            echo "    ERROR: This (default) variant of bzip2 library is NOT"
            echo "      supported with Zip because of its incompatible"
            echo "      error handling!"
            echo "      Please see the Zip installation instructions in"
            echo "      the INSTALL text file."
            echo "    -----------------------------------------------------"
            echo "  ^^ Incompatibe BZIP2 support outside the Zip tree"
            echo "^^ Unable to include BZIP2 compression support"
          fi
        else
          echo "  ^^ ${IZ_BZIP2}/libbz2.a not found (IZ_BZIP2 != \"bzip2\"),"
          echo "  ^^ Incomplete BZIP2 support outside the Zip tree"
          echo "^^ Unable to include BZIP2 compression support"
        fi
      fi
    else
      echo "  -- ${IZ_BZIP2}/bzlib.h not found"
      echo "  Check if OS already has BZIP2 library installed"
      cat > conftest.c << _EOF_
#include "bzlib.h"
int main()
{
  bz_stream strm;
  BZ2_bzCompressEnd(&strm);
  return 0;
}
_EOF_
      $CC_TST $CFLAGS -o conftest conftest.c -lbz2 >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        echo "  -- OS supports BZIP2 - linking in bzip2"
        CFLAGS_TST="${CFLAGS_TST} -DBZIP2_SUPPORT"
        LFLAGS2="${LFLAGS2} -lbz2"
        echo "-- Including OS BZIP2 compression support"
      else
        echo "  ^^ Neither bzlib.h nor libbz2.a was found"
        echo "^^ Unable to include BZIP2 compression support"
      fi
    fi
  else
    echo "^^ Directory ${IZ_BZIP2} not found"
    echo "^^ Unable to include BZIP2 compression support"
  fi
else
  echo "No BZIP2 compression"
fi

#------------------------------------------------------------------------------
# H) Configure optional ZLIB compression library.
#------------------------------------------------------------------------------
# ZLIB compression.  (User must request: "IZ_ZLIB=zlib_dir".)
if [ -n "${IZ_ZLIB}" ]; then
  echo "Check ZLIB deflate compression support"
  if [ -d "${IZ_ZLIB}" ]; then
    echo "  Check for ZLIB header file in IZ_ZLIB (${IZ_ZLIB})"
    if [ -f "${IZ_ZLIB}/zlib.h" ]; then
      if [ "${IZ_ZLIB}" = "zlib" ]; then
        echo "  ZLIB found within the Zip tree"
        echo "  Check for zlib compiled library in IZ_ZLIB (${IZ_ZLIB})"
        if [ -f "${IZ_ZLIB}/libz.a" ]; then
            LIB_Z="${IZ_ZLIB}/libz.a"
            CFLAGS_TST="${CFLAGS_TST} -I${IZ_ZLIB} -DUSE_ZLIB"
            LFLAGS1="${LFLAGS1} -L${IZ_ZLIB}"
            LFLAGS2="${LFLAGS2} -lz"
          echo "-- Including ZLIB deflate compression support"
          echo "-- Using libz.a in IZ_ZLIB (${IZ_ZLIB})"
        else
          echo "  Check for ZLIB Makefile in IZ_ZLIB (${IZ_ZLIB})"
          if [ -f "${IZ_ZLIB}/Makefile" ]; then
            LIB_Z="${IZ_ZLIB}/libz.a"
            CFLAGS_TST="${CFLAGS_TST} -I${IZ_ZLIB} -DUSE_ZLIB"
            LFLAGS1="${LFLAGS1} -L${IZ_ZLIB}"
            LFLAGS2="${LFLAGS2} -lz"
            echo "-- Including ZLIB deflate compression support"
            echo "-- Building libz.a in ${IZ_ZLIB}"
          else
            echo "  ^^ Incomplete ZLIB support within the Zip tree"
            echo "^^ Unable to include ZLIB compression support"
          fi
        fi
      else
        echo "  ZLIB support is outside the Zip tree"
        echo "  Check for ZLIB compiled library in IZ_ZLIB (${IZ_ZLIB})"
        if [ -f "${IZ_ZLIB}/libz.a" ]; then
          echo "    Found ZLIB library, ${IZ_ZLIB}/libz.a"
          LIB_Z="${IZ_ZLIB}/libz.a"
          CFLAGS_TST="${CFLAGS_TST} -I${IZ_ZLIB} -DUSE_ZLIB"
          LFLAGS1="${LFLAGS1} -L${IZ_ZLIB}"
          LFLAGS2="${LFLAGS2} -lz"
          echo "-- Including ZLIB deflate compression support"
          echo "-- Using libz.a in IZ_ZLIB (${IZ_ZLIB})"
        else
          echo "  ^^ ${IZ_ZLIB}/libz.a not found (IZ_ZLIB != \"zlib\"),"
          echo "  ^^ Incomplete ZLIB support outside the Zip tree"
          echo "^^ Unable to include ZLIB compression support"
        fi
      fi
    else
      echo "  -- ${IZ_ZLIB}/zlib.h not found"
      echo "  Check if OS already has ZLIB library installed"
      cat > conftest.c << _EOF_
#include "zlib.h"
int main()
{
  z_streamp strm;
  inflateReset(&strm);
  return 0;
}
_EOF_
      $CC_TST $CFLAGS -o conftest conftest.c -lz >/dev/null 2>/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        echo "  -- OS supports ZLIB - linking in zlib"
        CFLAGS_TST="${CFLAGS_TST} -DUSE_ZLIB"
        LFLAGS2="${LFLAGS2} -lz"
        echo "-- Including OS ZLIB compression support"
      else
        echo "  ^^ Neither zlib.h nor libz.a was found"
        echo "^^ Unable to include ZLIB compression support"
      fi
    fi
  else
    echo "^^ Directory ${IZ_ZLIB} not found"
    echo "^^ Unable to include ZLIB compression support"
  fi
else
  echo "ZLIB not used"
fi

#------------------------------------------------------------------------------
# H') Configure Zip object library.
#------------------------------------------------------------------------------
# Zip object library.  (User must request: "LIBZIP=1".)
if [ -n "${LIBZIP}" ]; then
  LIB_ZIP="libizzip.a"
  echo "-- Building Zip object library (${LIB_ZIP})"
fi

#------------------------------------------------------------------------------
# I) Common compiler feature checks.
#------------------------------------------------------------------------------

# Feature macro common conventions:
#
# HAVE_HEADER_H
# - Indicates that <header.h> is available.
# - Preferred for new header tests
#
# HAVE_DIR_HEADER_H
# - Indicates that <dir/header.h> is available.
# - Preferred for new header tests
#
# NO_HEADER_H
# - Indicates that <header.h> is not available.
#
# NO_DIR_HEADER_H
# - Indicates that <dir/header.h> is not available.
#
# FUNCTION=function
# - Indicates that function() is available.
# - If not available, Zip provides a replacement as follows:
#   - tailor.h:
#     - Replacement FUNCTION macro defined to declare replacement function.
#     - NEED_FUNCTION defined to enable compile of replacment function
#   - util.c:
#     - Defines body of the replacement function, but this is compiled only
#       when NEED_FUNCTION is defined.
# - All code references to function() must be coded as FUNCTION()
#
# NO_FUNCTION
# - Indicates that function() is not available.
# - No replacement is provided for these cases.


# Disable ANSI function prototypes where not supported.
echo 'Check for prototypes'
echo "int main(int argc, char *argv[]) { return 0; }" > conftest.c
$CC_TST $CFLAGS -c conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_PROTO"

# Check if compiler supports "const" keyword.
# - Handles mips cc and non ANSI compilers.
# - Does it need more?
echo 'Check the handling of const'
cat > conftest.c << _EOF_
typedef int charset[2];
int main()
{
  const charset x;
  const char *foo;
  return 0;
}
_EOF_
$CC_TST $CFLAGS -c conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_CONST"

# Check for size_t type.
echo 'Check for size_t'
cat > conftest.c << _EOF_
#include <sys/types.h>
int main()
{
  size_t s;
  return 0;
}
_EOF_
$CC_TST $CFLAGS -c conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_SIZE_T"

# Check for off_t type.
echo 'Check for off_t'
cat > conftest.c << _EOF_
#include <sys/types.h>
int main()
{
  off_t s;
  return 0;
}
_EOF_
$CC_TST $CFLAGS -c conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_OFF_T"

# Check for Large File Support (required for ZIP64).
# - Now we set the 64-bit file environment and check the size of off_t.
echo 'Check for Large File Support'
cat > conftest.c << _EOF_
# define _LARGEFILE_SOURCE       /* Some OSes need this for fseeko().     */
# define _LARGEFILE64_SOURCE
# define _FILE_OFFSET_BITS 64    /* Select default interface as 64 bit.   */
# define _LARGE_FILES            /* Some OSes need this for 64-bit off_t. */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
  off_t offset;
  struct stat s;
  /* See if have 64-bit off_t. */
  if (sizeof(offset) < 8) {
    printf("  off_t is %d bytes\n", sizeof(off_t));
    return 1;
  }
  /* See if have 64-bit stat. */
  if (sizeof(s.st_size) < 8) {
    printf("  s.st_size is %d bytes\n", sizeof(s.st_size));
    return 2;
  }
  printf("  off_t is %d bytes\n", sizeof(off_t));
  printf("  s.st_size is %d bytes\n", sizeof(s.st_size));
  return 3;
}
_EOF_
$CC_TST $CFLAGS -o conftest conftest.c >/dev/null 2>/dev/null
status=$?
if [ $status -ne 0 ]; then
  echo '-- no Large File Support'
else
  ./conftest
  status=$?
  if [ $status -eq 1 ]; then
    echo '-- no Large File Support - no 64-bit off_t'
  elif [ $status -eq 2 ]; then
    echo '-- no Large File Support - no 64-bit stat'
  elif [ $status -eq 3 ]; then
    echo '-- yes we have Large File Support!'
    CFLAGS_TST="${CFLAGS_TST} -DLARGE_FILE_SUPPORT"
    CFLAGS_TST_BZ="${CFLAGS_TST_BZ} -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES"
  else
    echo "-- no Large File Support - conftest returned $status"
  fi
fi

# Check size of UIDs and GIDs.
# (Now zip stores variable size UIDs/GIDs using a new extra field.  This
# tests if this OS uses 16-bit UIDs/GIDs and so if the old 16-bit storage
# should also be used for backward compatibility.)

echo 'Check size of UIDs and GIDs'
cat > conftest.c << _EOF_
# define _LARGEFILE_SOURCE       /* Some OSes need this for fseeko       */
# define _LARGEFILE64_SOURCE
# define _FILE_OFFSET_BITS 64    /* Select default interface as 64 bit   */
# define _LARGE_FILES            /* Some OSes need this for 64-bit off_t */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main( )
{
  struct stat s;

 #if !defined(__MVS__) && !defined(__VM__)
  /*
   2011/09/02 [AD] Comment out: Problem reported to IBM
                   - Over-optimized trivial program fails during printf()
   */
  printf("  s.st_uid is %u bytes\n", sizeof(s.st_uid));
  printf("  s.st_gid is %u bytes\n", sizeof(s.st_gid));
 #endif

  /* See if have 16-bit UID. */
  if (sizeof(s.st_uid) != 2) {
    return 1;
  }
  /* See if have 16-bit GID. */
  if (sizeof(s.st_gid) != 2) {
    return 2;
  }
  return 3;
}
_EOF_
$CC_TST $CFLAGS -o conftest conftest.c >/dev/null 2>/dev/null
status=$?
if [ $status -ne 0 ]; then
  errmes="-- UID/GID test failed on compile"
  errmes="${errmes} - disabling old 16-bit UID/GID support"
  echo "${errmes}"
  CFLAGS_TST="${CFLAGS_TST} -DUIDGID_NOT_16BIT"
else
  ./conftest
  status=$?
  if [ $status -eq 1 ]; then
    echo '-- UID not 2 bytes - disabling old 16-bit UID/GID support'
    CFLAGS_TST="${CFLAGS_TST} -DUIDGID_NOT_16BIT"
  elif [ $status -eq 2 ]; then
    echo '-- GID not 2 bytes - disabling old 16-bit UID/GID support'
    CFLAGS_TST="${CFLAGS_TST} -DUIDGID_NOT_16BIT"
  elif [ $status -eq 3 ]; then
    echo '-- 16-bit UIDs and GIDs - keeping old 16-bit UID/GID support'
  else
    errmes="-- test failed - conftest returned $status"
    errmes="${errmes} - disabling old 16-bit UID/GID support"
    echo "${errmes}"
    CFLAGS_TST="${CFLAGS_TST} -DUIDGID_NOT_16BIT"
  fi
fi

# Check for time_t structure.
echo 'Check for time_t'
cat > conftest.c << _EOF_
#include <sys/types.h>
#include <time.h>
int main()
{
  time_t t;
  return 0;
}
_EOF_
$CC_TST $CFLAGS -c conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_TIME_T"

# Check for utime.h header.
echo 'Check for utime.h'
echo "#include <utime.h>" > conftest.c
$CPP_TST conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -eq 0 ] && CFLAGS_TST="${CFLAGS_TST} -DHAVE_UTIME_H"

# Check for sys/timeb.h header.
echo 'Check for sys/timeb.h'
echo "#include <sys/timeb.h>" > conftest.c
$CPP_TST conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -eq 0 ] && CFLAGS_TST="${CFLAGS_TST} -DHAVE_SYS_TIMEB_H"

# Check for sys/time.h header and gettimeofday() function.
echo 'Check for sys/time.h and gettimeofday() needed for entry timing'
echo '  Check for sys/time.h'
echo "#include <sys/time.h>" > conftest.c
$CPP_TST conftest.c >/dev/null 2>/dev/null
status=$?
if [ $status -ne 0 ]; then
  echo "-- No sys/time.h - usec timing using gettimeofday() not supported"
else
  CFLAGS_TST="${CFLAGS_TST} -DHAVE_SYS_TIME_H"
  echo '  Check for gettimeofday()'
  cat > conftest.c << _EOF_
#include "stdio.h"
#include <sys/time.h>
int main()
{
  struct timeval now;
  unsigned long long usec_time;

  gettimeofday(&now, NULL);
  usec_time = now.tv_sec * 1000000 + now.tv_usec;
  return 0;
}
_EOF_
  $CC_TST -o conftest conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -ne 0 ]; then
    echo "^^ returned $status - usec timing using gettimeofday() not supported"
  else
    echo '-- enabling usec timing using gettimeofday()'
    CFLAGS_TST="${CFLAGS_TST} -DENABLE_ENTRY_TIMING"
  fi
fi

# Check for optional header files.
# - If found, define HAVE_HEADER_H macro.
INCLS='ctype.h wctype.h langinfo.h locale.h'
for inc in $INCLS
do
  echo "Check for $inc"
  echo "#include <$inc>" > conftest.c
  $CPP_TST conftest.c >/dev/null 2>/dev/null
  status=$?
  [ $status -eq 0 ] && \
    CFLAGS_TST="${CFLAGS_TST} -DHAVE_`echo $inc | tr '[a-z]./' '[A-Z]__'`"
done

# This all needs some work.  See note at the top of tailor.h.

# Check for wchar.h header.
echo 'Check for wchar.h'
echo "#include <wchar.h>" > conftest.c
$CPP_TST conftest.c >/dev/null 2>/dev/null
status=$?
if [ $status -eq 0 ]; then
  CFLAGS_TST="${CFLAGS_TST} -DHAVE_WCHAR_H"

  # Realistic check for working wide char library functions.
  # - Add FUNCTION_NAME='function_name' to flags if found.
  FUNCS='towupper towlower iswprint'
  for func in $FUNCS
  do
    echo "Check for wide char $func"
    cat > conftest.c << _EOF_
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_CTYPE_H
#  include <ctype.h>
#endif
#ifdef HAVE_WCHAR_H
#  include <wchar.h>
#endif
#ifdef HAVE_WCTYPE_H
#  include <wctype.h>
#endif
int main()
{
  int  out;
  out = $func( 'a' );
  printf( " out: >0x%X<.\n", out);
  return (out == 0);
}
_EOF_
    $CC_TST ${CFLAGS} ${CFLAGS_OPT} ${CFLAGS_TST} ${CFLAGS_USR} -o conftest \
     conftest.c >/dev/null 2>/dev/null
    status=$?
    if [ $status -eq 0 ]; then
      ./conftest >/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CFLAGS_TST="${CFLAGS_TST} -D`echo $func | tr '[a-z]' '[A-Z]'`=$func"
      fi
    fi
  done

  # Check for wide char for Unicode support.
  echo 'Check for wide char Unicode support'
  cat > conftest.c << _EOF_
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_CTYPE_H
#  include <ctype.h>
#endif
#ifdef HAVE_WCHAR_H
#  include <wchar.h>
#endif
#ifdef HAVE_WCTYPE_H
#  include <wctype.h>
#endif
int main()
{
  size_t wsize;
  wchar_t *wide_string;
  wchar_t wc = (wchar_t)'a';   /* any old character will do */
  wchar_t wco;
  int i;

  if ((wide_string = (wchar_t *)malloc(4 * sizeof(wchar_t))) == NULL) {
    return 0;
  }

  /* Get wide string. */
  wsize = mbstowcs(wide_string, "foo", 3);
  wide_string[wsize] = (wchar_t) NULL;

  /* Make sure we have everything we need for wide support. */
  wco = towupper(wc);
  wco = towlower(wc);
  i = iswprint(wc);

#ifndef __STDC_ISO_10646__
  return 1;
#else
  printf("  __STDC_ISO_10646__ = %d (yyyymm)\n", __STDC_ISO_10646__);
  return 2;
#endif
}
_EOF_
  $CC_TST $CFLAGS -o conftest conftest.c >/dev/null 2>/dev/null
  status=$?
  if [ $status -ne 0 ]; then
    echo '^^ no Unicode (wchar_t) support'
  else
    ./conftest
    status=$?
    if [ $status -eq 0 ]; then
      echo '-- no Unicode wchar_t support - wchar_t allocation error'
    elif [ $status -eq 1 ]; then
      errmes="^^ no Unicode support"
      echo "${errmes} - wchar_t encoding unspecified, may not be UCS"
    elif [ $status -eq 2 ]; then
      echo '-- have wchar_t with known UCS encoding - enabling Unicode support'
      CFLAGS_TST="${CFLAGS_TST} -DUNICODE_SUPPORT -DUNICODE_WCHAR"
    else
      echo "^^ no Unicode (wchar_t) support - conftest returned $status"
    fi
  fi
fi

# Check for setlocale() support.
echo "Check for setlocale support (needed for UNICODE Native check)"
cat > conftest.c << _EOF_
#include <locale.h>
int main()
{
  char *loc = setlocale(LC_CTYPE, "");
  return 0;
}
_EOF_
$CC_TST $CFLAGS -c conftest.c >/dev/null 2>/dev/null
status=$?
if [ $status -eq 0 ]; then
  echo '  have setlocale, can check for charset type'
  echo '-- enabling UTF8-native support!'
  CFLAGS_TST="${CFLAGS_TST} -DUNICODE_SUPPORT -DUTF8_MAYBE_NATIVE"
else
  echo '^^ no Unicode (UTF-8 native) support'
  CFLAGS_TST="${CFLAGS_TST} -DNO_SETLOCALE"
fi

# Check for gcc no-builtin flag.
# - gnu C supports -fno-builtin since version 2
# - from configure 2.4i (Onno) 2004-12-05
echo 'Check for gcc no-builtin flag'
cat > conftest.c << _EOF_
int main()
{
#if __GNUC__ >= 2
   return 0;
#else
   forget it
#endif
}
_EOF_
$CC_TST $CFLAGS -c conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -eq 0 ] && BIF_TST="${BIF_TST} -fno-builtin"

# Check for missing functions.
# - add NO_'function_name' to flags if missing.
FUNCS='fchmod fchown lchown mktemp mktime mkstemp nl_langinfo rename rmdir'
FUNCS="${FUNCS} strchr strrchr"
#echo "Check for missing functions"
for func in $FUNCS
do
  echo "Check for $func"
  echo "int main(){ $func(); return 0; }" > conftest.c
  $CC_TST $CFLAGS $BIF_TST -o conftest conftest.c >/dev/null 2>/dev/null
  status=$?
  [ $status -ne 0 ] && \
    CFLAGS_TST="${CFLAGS_TST} -DNO_`echo $func | tr '[a-z]' '[A-Z]'`"
done

# Check (seriously) for a working lchmod().
echo 'Check for lchmod'
temp_file="/tmp/zip_test_$$"
temp_link="link_$$"
( echo '#include <unistd.h>' ; \
  echo "int main() { lchmod(\"${temp_file}\", 0666); }" \
) > conftest.c
ln -s "${temp_link}" "${temp_file}" && \
$CC_TST $CFLAGS $BIF_TST -o conftest conftest.c >/dev/null 2>/dev/null && \
 ./conftest
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_LCHMOD"
rm -f "${temp_file}"

# Check for memset().
echo 'Check for memset'
echo "int main(){ char k; memset(&k,0,0); return 0; }" > conftest.c
$CC_TST $CFLAGS -o conftest conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DZMEM"

# Check for memmove().
echo 'Check for memmove'
cat > conftest.c << _EOF_
#include <string.h>
int main() { int a; int b = 0; memmove( &a, &b, sizeof( a)); return a; }
_EOF_
$CC_TST $CFLAGS -o conftest conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNEED_MEMMOVE"

# Check for errno declaration.
echo 'Check for errno declaration'
cat > conftest.c << _EOF_
#include <errno.h>
main()
{
  errno = 0;
  return 0;
}
_EOF_
$CC_TST $CFLAGS -c conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_ERRNO"

# Check for strerror().
echo 'Check for strerror'
cat > conftest.c << _EOF_
#include <string.h>
int main() { strerror( 0); return 0; }
_EOF_
$CC_TST $CFLAGS -o conftest conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNEED_STRERROR"

# Check for directory libraries.
echo 'Check for directory libraries'
cat > conftest.c << _EOF_
int main() { return closedir(opendir(".")); }
_EOF_
$CC_TST $CFLAGS -o conftest conftest.c >/dev/null 2>/dev/null
status=$?
if [ $status -ne 0 ]; then
  OPT=""
  LIBS='ndir dir ucb bsd BSD PW x dirent'
  for lib in $LIBS
  do
    $CC_TST $CFLAGS -o conftest conftest.c -l$lib >/dev/null 2>/dev/null
    status=$?
    [ $status -eq 0 ] && OPT=-l$lib && break
  done
  if [ ${OPT} ]; then
    LFLAGS="${LFLAGS2} ${OPT}"
  else
    CFLAGS_TST="${CFLAGS_TST} -DNO_DIR"
  fi
fi

# Check for readlink().
# - Dynix/ptx 1.3 needed this.
echo 'Check for readlink'
echo "int main(){ return readlink(); }" > conftest.c
$CC_TST $CFLAGS -o conftest conftest.c >/dev/null 2>/dev/null
status=$?
if [ $status -ne 0 ]; then
  $CC_TST $CFLAGS -o conftest conftest.c -lseq >/dev/null 2>/dev/null
  status=$?
  [ $status -eq 0 ] && LFLAGS2="${LFLAGS2} -lseq"
fi

# Check for directory header.
echo 'Check for directory header file'
OPT=""
INCLS='dirent.h sys/ndir.h ndir.h sys/dir.h'
for inc in $INCLS
do
  echo "#include <$inc>" > conftest.c
  $CPP_TST conftest.c >/dev/null 2>/dev/null
  status=$?
  [ $status -eq 0 ] && OPT="-DHAVE_`echo $inc | tr '[a-z]./' '[A-Z]__'`" \
    && break
done
CFLAGS_TST="${CFLAGS_TST} ${OPT}"

# Check for sys/param.h.
echo 'Check for sys/param.h'
echo "#include <sys/param.h>" > conftest.c
$CPP_TST conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_PARAM_H"

# Check for nonexistent header files.
# - If not found, define NO_HEADER symbol.
# FIX_ME: Remove langinfo.h after all NO_LANGINFO uses removed.
echo 'Check for nonexistent header files'.
INCLS='stdlib.h stddef.h stdint.h unistd.h fcntl.h string.h langinfo.h'
for inc in $INCLS
do
  echo "#include <$inc>" > conftest.c
  $CPP_TST conftest.c >/dev/null 2>/dev/null
  status=$?
  [ $status -ne 0 ] && \
    CFLAGS_TST="${CFLAGS_TST} -DNO_`echo $inc | tr '[a-z]./' '[A-Z]__'`"
done

# Check for terminal I/O header file.
echo 'Check for term I/O header file'
OPT=""
INCLS='termios.h termio.h sgtty.h'
for inc in $INCLS
do
  echo "#include <$inc>" > conftest.c
  $CPP_TST conftest.c >/dev/null 2>/dev/null
  status=$?
  [ $status -eq 0 ] && OPT="-DHAVE_`echo $inc | tr '[a-z]./' '[A-Z]__'`" \
    && break
done
CFLAGS_TST="${CFLAGS_TST} ${OPT}"

# Check for MBCS header files.
echo 'Check for MBCS header files'
OPT=""
INCLS='mbstr.h mbstring.h mbctype.h'
for inc in $INCLS
do
  echo "#include <$inc>" > conftest.c
  $CPP_TST conftest.c >/dev/null 2>/dev/null
  status=$?
  [ $status -eq 0 ] && OPT="-DHAVE_`echo $inc | tr '[a-z]./' '[A-Z]__'`" \
    && break
done
CFLAGS_TST="${CFLAGS_TST} ${OPT}"

# Check for MBCS support.
echo 'Check for MBCS (multi-byte character) support'
cat > conftest.c << _EOF_
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_CTYPE_H
#  include <ctype.h>
#endif
#ifdef HAVE_WCHAR_H
#  include <wchar.h>
#endif
#ifdef HAVE_WCTYPE_H
#  include <wctype.h>
#endif
#ifdef HAVE_MBSTRING_H
#  include <mbstring.h>
#endif
int main()
{
  char *tst;
  tst = "Hallo";
  return mblen(tst, MB_CUR_MAX);
}
_EOF_
$CC_TST ${CFLAGS} -o conftest conftest.c >/dev/null 2>/dev/null
status=$?
if [ $status -ne 0 ]; then
  echo "^^ no MBCS support"
  CFLAGS_TST="${CFLAGS_TST} -DNO_MBCS"
else
  echo "-- have MBCS support"
  CFLAGS_TST="${CFLAGS_TST} -D_MBCS"

  # Realistic check for working MBCS library functions.
  # - Add FUNCTION_NAME='function_name' to flags if found.
  FUNCS='mbschr mbsrchr'
  for func in $FUNCS
  do
    echo "Check for MBCS $func"
    cat > conftest.c << _EOF_
#include <stdlib.h>
#ifdef HAVE_MBSTR_H
#  include <mbstr.h>
#endif
int main()
{
  char *tst;
  tst = $func( "abc", 'b');
  printf( "  $func(\"abc\", 'b') =  \"%s\"\n",  tst);
  return (tst == NULL);
}
_EOF_
    $CC_TST ${CFLAGS} ${CFLAGS_OPT} ${CFLAGS_TST} ${CFLAGS_USR} -o conftest \
      conftest.c >/dev/null 2>/dev/null
    status=$?
    if [ $status -eq 0 ]; then
      ./conftest >/dev/null
      status=$?
      if [ $status -eq 0 ]; then
        CFLAGS_TST="${CFLAGS_TST} -D`echo $func | tr '[a-z]' '[A-Z]'`=$func"
      fi
    fi
  done
fi

# Needed for AIX (and others ?) when mmap is used.
echo 'Check for valloc'
cat > conftest.c << _EOF_
main()
{
#ifdef MMAP
    valloc();
#endif
}
_EOF_
$CC_TST ${CFLAGS} -c conftest.c >/dev/null 2>/dev/null
status=$?
[ $status -ne 0 ] && CFLAGS_TST="${CFLAGS_TST} -DNO_VALLOC"


#------------------------------------------------------------------------------
# J) Perform OS-specific tests.
#------------------------------------------------------------------------------

# Check for OS-specific flags.
echo 'Check for OS-specific flags'
if [ -f /usr/bin/hostinfo ]; then
  if /usr/bin/hostinfo | grep NeXT >/dev/null; then
    CFLAGS_TST="${CFLAGS_TST} -posix"
    LFLAGS1="${LFLAGS1} -posix -object"
  fi
# XXX ATT6300, Cray.
elif [ -f /xenix ]; then
  if echo "${UNAME_P}" | grep 286 >/dev/null; then
    CFLAGS_TST="${CFLAGS_TST} -LARGE -Mel2 -DMEDIUM_MEM -DWSIZE=16384"
    CFLAGS_TST="${CFLAGS_TST} -DNO_VOID"
    LFLAGS1="${LFLAGS1} -LARGE -Mel2"
  fi
elif [ -n "${UNAME_X}" ]; then
# SCO shared library check.
  echo "int main() { return 0;}" > conftest.c
  $CC_TST $CFLAGS -o conftest conftest.c -lc_s -nointl >/dev/null 2>/dev/null
  status=$?
  [ $status -eq 0 ] && LFLAGS2="-lc_s -nointl"
else
  echo "int main() { return 0;}" > conftest.c
  case "${UNAME_S}" in
     OSF1|ULTRIX)
        echo 'Check for -Olimit option'
        $CC_TST ${CFLAGS} -Olimit 1000 -o conftest conftest.c \
          >/dev/null 2>/dev/null
        status=$?
        [ $status -eq 0 ] && CFLAGS_TST="${CFLAGS_TST} -Olimit 1000"
        ;;
  esac
fi


#------------------------------------------------------------------------------
# K) Configure system-specific resources.
#------------------------------------------------------------------------------

# Check for /usr/local/bin.
echo 'Check for /usr/local/bin'
[ -d /usr/local/bin ] && BINDIR='/usr/local/bin'

# Check for /usr/local/man.
echo 'Check for /usr/local/man'
[ -d /usr/man/manl ]       && MANDIR='/usr/man/manl'
[ -d /usr/local/man/manl ] && MANDIR='/usr/local/man/manl'
[ -d /usr/local/man/man1 ] && MANDIR='/usr/local/man/man1'; MANEXT='.1'

# Check for symbolic links.
echo 'Check for symbolic links'
ln -s /dev/null null >/dev/null 2>/dev/null || LN=ln

# Remove work files from configuration tests.
rm -f a.out conftest.c conftest.o conftest.err conftest.lst conftest null


#------------------------------------------------------------------------------
# L) Generate system identification.
#------------------------------------------------------------------------------

if [ -n "${UNAME_S}" ]; then
  CFLAGS_TST="${CFLAGS_TST} -DUNAME_S='\\\"${UNAME_S}\\\"'"
fi

if [ -n "${UNAME_V}" ]; then
  CFLAGS_TST="${CFLAGS_TST} -DUNAME_V='\\\"${UNAME_V}\\\"'"
fi

if [ -n "${UNAME_R}" ]; then
  CFLAGS_TST="${CFLAGS_TST} -DUNAME_R='\\\"${UNAME_R}\\\"'"
fi

if [ -n "${UNAME_O}" ]; then
  CFLAGS_TST="${CFLAGS_TST} -DUNAME_O='\\\"${UNAME_O}\\\"'"
fi

if [ -n "${UNAME_M}" ]; then
  CFLAGS_TST="${CFLAGS_TST} -DUNAME_M='\\\"${UNAME_M}\\\"'"
fi

if [ -n "${UNAME_P}" ]; then
  CFLAGS_TST="${CFLAGS_TST} -DUNAME_P='\\\"${UNAME_P}\\\"'"
fi

if [ -n "${SIZER_V}" ]; then
  CFLAGS_TST="${CFLAGS_TST} -DSIZER_V='\\\"${SIZER_V}\\\"'"
fi


#------------------------------------------------------------------------------
# Finalize the configuration values.
#------------------------------------------------------------------------------

# Add optimization (and listing) flags to initial C flags.
if [ -n "${CFLAGS_OPT}" ]; then
  CFLAGS="${CFLAGS} ${CFLAGS_OPT}"
  CFLAGS_BZ="${CFLAGS_BZ} ${CFLAGS_OPT}"
fi

# Add standard bzip2 configuration flags.
CFLAGS_BZ="${CFLAGS_BZ} -DBZ_NO_STDIO"

# Add test configuration flags.
if [ -n "${CFLAGS_TST}" ]; then
  CFLAGS="${CFLAGS} ${CFLAGS_TST}"
fi
if [ -n "${CFLAGS_TST_BZ}" ]; then
  CFLAGS_BZ="${CFLAGS_BZ} ${CFLAGS_TST_BZ}"
fi

# Add user override flags.
if [ -n "${CFLAGS_USR}" ]; then
  CFLAGS="${CFLAGS} ${CFLAGS_USR}"
fi

# Use listing-specific CC if required.
if [ -n "${CC_LIST}" ]; then
  CC="${CC_LIST}"
  CC_BZ="${CC_LIST}"
fi

# Use listing-specific LD if required.
if [ -n "${LD_LIST}" ]; then
  LD="${LD_LIST}"
fi

#------------------------------------------------------------------------------
# Emit the primary configuration values in the $PATH/flags file.
#------------------------------------------------------------------------------
echo "CC=\"${CC}\" CF=\"${CFLAGS}\"" \
     "CC_BZ=\"${CC_BZ}\" CF_BZ=\"${CFLAGS_BZ}\"" \
     "CPP=\"${CPP}\"" \
     "AS=\"${AS}\" AF=\"${AFLAGS}\"" \
     "LD=\"${LD}\" LF1=\"${LFLAGS1}\" LF2=\"${LFLAGS2}\"" \
     "AR=\"${AR}\" RANLIB=\"${RANLIB}\"" \
     "LN=\"${LN}\"" \
     "RM=\"${RM}\"" \
     "PROD=\"${PROD}\"" \
     "PORT=\"${PORT}\"" \
     "LIB_AES_WG=\"${LIB_AES_WG}\"" \
     "LIB_BZ=\"${LIB_BZ}\"" \
     "LIB_LZMA=\"${LIB_LZMA}\"" \
     "LIB_PPMD=\"${LIB_PPMD}\"" \
     "LIB_Z=\"${LIB_Z}\"" \
     "LIB_ZIP=\"${LIB_ZIP}\"" \
     "MK_BZ=\"${MK_BZ}\"" \
     "OSDEP_H=\"${OSDEP_H}\"" \
     "OSDEP_OCU=\"${OSDEP_OCU}\"" \
     "OSDEP_OCZ=\"${OSDEP_OCZ}\"" \
     "OSDEP_OSU=\"${OSDEP_OSU}\"" \
     "OSDEP_OSZ=\"${OSDEP_OSZ}\"" \
     "PGMEXT=\"${PGMEXT}\" DLLEXT=\"${DLLEXT}\"" \
     "BINDIR=\"${BINDIR}\" BINPERMS=\"${BINPERMS}\"" \
     "MANDIR=\"${MANDIR}\" MANPERMS=\"${MANPERMS}\"" \
     "MANEXT=\"${MANEXT}\""\
     >"$PROD"/flags

#------------------------------------------------------------------------------
# Emit the libbz2 configuration values in the $PATH/flags_bz file.
#------------------------------------------------------------------------------
echo "CC=\"${CC_BZ}\" CFLAGS=\"${CFLAGS_BZ}\"" \
     "AR=\"${AR_BZ}\" RANLIB=\"${RANLIB}\"" \
     "RM=\"${RM}\"" \
     >"$PROD"/flags_bz
echo ""
