#!/bin/sh

#  $Id: configure,v 1.9 2005/03/26 11:21:12 alien-science Exp $ 

##### config

STANDARD_LIBDIRS='/lib /usr/lib'
DEFAULT_LIBDIRS='-L /usr/local/lib'
DEFAULT_INCDIRS='-I /usr/local/include'

##### Functions

usage()
{
   cat << EOUSE
  $0 [--options] [-I include_dir] [-L libdir]
Configures the makefile for murk.
  include_dir  Include  directory to include in search for headers
  libdir       Library directory to include in search for libraries
  The argument types  above can appear more than once

Options:
   --static    Statically link all libraries

EOUSE

   exit 1
}

verbose()
{
   echo "[config] $1" 
}

fatal()
{
   echo "A fatal error occured while: $1"
   exit 1
}

get_option()
{
   option=$1
   shift
   get_option_ret=''

   while [ -n "$1" ]
   do
      if [ "X$1" = "X$option" ]
      then
         shift
         if [ "X$get_option_ret" = 'X' ]
         then 
            get_option_ret=$1
         else
            get_option_ret="$get_option_ret $1"
         fi
      fi
      shift
   done

}

prepend_flag()
{
   flag=$1
   shift

   prepend_flag_ret=''
   for i in $*
   do
      if [ "X$prepend_flag_ret" = "X" ]
      then 
         prepend_flag_ret="${flag}$i"
      else
         prepend_flag_ret="$prepend_flag_ret ${flag}$i"
      fi
   done
}


## Find the libdir for the given library
find_lib()
{
   to_find=$1
   verbose "Looking for $to_find"

   find_lib_ret=''

   for d in $lsearch $STANDARD_LIBDIRS
   do
      matches=`ls ${d}/${to_find}* 2> /dev/null`
      if [ "X$matches" != 'X' ] 
      then
         find_lib_ret=$d
         verbose "Using $matches"
         break
      fi
   done

   if [ "X$find_lib_ret" = 'X' ] && [ "X$2" = "X" ]
   then
      verbose "Cannot find $to_find -- perhaps some arguments will help"
      usage
   fi

}

#### Options to this configure script

while [ -n "$1" ]
do
   case $1 in
      --static)
         all_static="-static"
         ;;
      --help|-h)
        usage
        ;;
      *)
         break
         ;;
   esac
   shift
done


#### Initialisation

get_option -L $* $DEFAULT_LIBDIRS
lsearch=$get_option_ret
get_option -L $* $DEFAULT_LIBDIRS
prepend_flag -L $get_option_ret
lpath=$prepend_flag_ret
get_option -I $* $DEFAULT_INCDIRS
prepend_flag -I $get_option_ret
ipath=$prepend_flag_ret

##### Bzip2

# Find the static library
find_lib 'libbz2.a'
bz2lib="${find_lib_ret}/libbz2.a"

##### OpenSSL/libcrypto

# This is sometimes only available in static form
# Attempt to find the dynamic library first
if [ "X$all_static" = 'X' ]
then
   find_lib 'libcrypto.so' ignore_errors
fi

if [ -n "$all_static"  -o "X$find_lib_ret" = 'X' ]
then
   # Try to use the static
   find_lib 'libcrypto.a'
   static_crypto="${find_lib_ret}/libcrypto.a"
else
   lib_crypto='-lcrypto'
fi

#### Create the makefile include

config/set_template.sh make.inc.tmpl << EOTMPL
BZ2LIB=${bz2lib} 
LIBPATH=$lpath
INCPATH=$ipath
STATIC=$all_static
EXTRA_STATICS=$extra_statics
STATIC_CRYPTO=$static_crypto
LIB_CRYPTO=$lib_crypto
EOTMPL

[ $? -eq 0 ] || fatal "Building makefile"


# Give instructions
cat << EOC
 Now run:
    make
    make test
EOC

