#!/bin/ksh
#C-
#C- DjVu Reference Library (v. 3.0)
#C- 
#C- Copyright  2000 LizardTech, Inc. All Rights Reserved.
#C- The DjVu Reference Library is protected by U.S. Pat. No.
#C- 6,058,214 and patents pending.
#C- 
#C- This software is subject to, and may be distributed under, the
#C- GNU General Public License, Version 2. The license should have
#C- accompanied the software or you may obtain a copy of the license
#C- from the Free Software Foundation at http://www.fsf.org .
#C- 
#C- The computer code originally released by LizardTech under this
#C- license and unmodified by other parties is deemed the "LizardTech
#C- Original Code."
#C- 
#C- With respect to the LizardTech Original Code ONLY, and subject
#C- to any third party intellectual property claims, LizardTech
#C- grants recipient a worldwide, royalty-free, non-exclusive license
#C- under patent claims now or hereafter owned or controlled by
#C- LizardTech that are infringed by making, using, or selling
#C- LizardTech Original Code, but solely to the extent that any such
#C- patent(s) is/are reasonably necessary to enable you to make, have
#C- made, practice, sell, or otherwise dispose of LizardTech Original
#C- Code (or portions thereof) and not to any greater extent that may
#C- be necessary to utilize further modifications or combinations.
#C- 
#C- The LizardTech Original Code is provided "AS IS" WITHOUT WARRANTY
#C- OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
#C- TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
#C- MERCHANTIBILITY OR FITNESS FOR A PARTICULAR PURPOSE.
#C- 
#
# $Id: configure,v 1.39 2000/11/09 20:15:05 jmw Exp $
# $Name: DjVu3_snapshot $

# LIBDJVU++ -- Decoding library and utilities

### ---------------------------
### Determine directories

PROGRAM="$0"
CONFIG_FILE=`dirname "$PROGRAM"`"/config/functions.sh"
conf=`basename "$PROGRAM"`
if [ ! -r "$CONFIG_FILE" ]
then
    echo 1>&2 "$conf: Cannot recognize source directory"
    echo 1>&2 "--  Check that your source directory is complete."
    exit 1
fi


### ---------------------------
### Get functions

. "$CONFIG_FILE"

### ---------------------------
### Process arguments

for arg 
do
  case $arg in 
    --with-threads=* )
      threads=`echo $arg | sed 's/[-A-Za-z_]*=//'` 
      ;; 
    --with-rpo|--frepo )
      use_rpo=yes
      ;;
    --without-jpeg )
      without_jpeg=yes
      ;;
    *)
      if process_general_option $arg
      then true
      else
        if [ "x$arg" != "x--help" ] ; then echo 1>&2 "$conf: Unrecognized option '$arg'" ; fi
        echo 1>&2 "Usage: $conf <options>"
        echo 1>&2 "Recognized options:"
        list_general_options
        echo 1>&2 "  --with-threads=(no|co|posix|dce)          choose a thread implementation."
        echo 1>&2 "  --with-rpo,--frepo                        use --frepo option in G++."
        echo 1>&2 "  --without-jpeg                            excludes  JPEG decoder (bulky)."
        exit 1 
      fi
      ;;
   esac
done

if [ ! -d "$TOPSRCDIR/src/3rd-party/libjpeg" ] 
then
  without_jpeg=true
fi

if [ -z "$without_jpeg" ] 
then
      C_DEFS=APPEND
      A_DEFS="$A_DEFS -DNEED_JPEG_DECODER"
      C_INCS=APPEND
      A_INCS="$A_INCS -I./TOPDIR/src/3rd-party/libjpeg -I./TOPDIR/SRCDIR/src/3rd-party/libjpeg"
      LIBJPEG='$(LIBPATH)/libdjpeg.a'
else
      LIBJPEG=""
fi
CONFIG_VARS=`echo LIBJPEG $CONFIG_VARS`

### ---------------------------
### Want a distinct compilation directory

if [ $TOPSRCDIR = $TOPBUILDDIR ]
then
    echo 1>&2 "$conf: Cannot compile in the source directory"
    echo 1>&2 "--  You must mkdir a compilation directory and"
    echo 1>&2 "    call $conf from there."
    exit 1
fi


### ---------------------------
### Proceed

if [ -z "$without_jpeg" ] 
then
    require_compiler c c++
else
    require_compiler c++
fi

check_debug_option $debug
check_thread_option $threads
require_library math log -lm
check_rpo_option $use_rpo
require_make_stlib


### ---------------------------
### Build makefiles

generate_makefile src/libdjvu++
generate_makefile src/libdjvu++/Test
generate_makefile src/djvutools/reference

if [ -z "$without_jpeg" ] 
then
    ## This is the version with JPEG 
    generate_makefile src/3rd-party/libjpeg
    generate_main_makefile src/3rd-party/libjpeg src/libdjvu++ <<\EOF
all: 
	( cd src/3rd-party/libjpeg; $(MAKE) lib prog)
	( cd src/libdjvu++; $(MAKE) lib )
	( cd src/djvutools/reference; $(MAKE) prog )

install:
	echo Rule not supported...
EOF

else
    ## This is the regular version
    generate_main_makefile src/libdjvu++ <<\EOF
all: 
	( cd src/libdjvu++; $(MAKE) lib )
	( cd src/djvutools/reference; $(MAKE) prog )

install:
	echo Rule not supported...
EOF
fi



### ------------------------------------------------------------------------
### Success


# --- save command line

finish_config

# --- display cool message

echo " "
echo Configuration is complete.
echo You can proceed with the compilation by typing:
if [ "$TOPBUILDDIR" != "`pwd`" ] ; then echo "    cd '$TOPBUILDDIR'" ; fi
echo "    make clean"
if [ -n "$compiler_is_gcc" ] ; then echo "    make depend" ; fi
echo "    make all"
echo " "


