#!/bin/sh

# Defaults

PREFIX=/usr/local
SYSCONFDIR="$PREFIX/etc/prosody"
DATADIR="$PREFIX/var/lib/prosody"
LUA_SUFFIX=""
LUA_DIR="/usr"
LUA_BINDIR="/usr/bin"
LUA_INCDIR="/usr/include"
LUA_LIBDIR="/usr/lib"
IDN_LIB=idn
ICU_FLAGS="-licui18n -licudata -licuuc"
OPENSSL_LIB=crypto
CC=gcc
CXX=g++
LD=gcc
RUNWITH=lua
EXCERTS=yes

CFLAGS="-fPIC -Wall"
LDFLAGS="-shared"

IDN_LIBRARY=idn
# Help

show_help() {
cat <<EOF
Configure Prosody prior to building.

--help                      This help.
--ostype=OS                 Use one of the OS presets.
                            May be one of: debian, macosx, linux, freebsd
--prefix=DIR                Prefix where Prosody should be installed.
                            Default is $PREFIX
--sysconfdir=DIR            Location where the config file should be installed.
                            Default is \$PREFIX/etc/prosody
--datadir=DIR               Location where the server data should be stored.
                            Default is \$PREFIX/var/lib/prosody
--lua-suffix=SUFFIX         Versioning suffix to use in Lua filenames.
                            Default is "$LUA_SUFFIX" (lua$LUA_SUFFIX...)
--with-lua=PREFIX           Use Lua from given prefix.
                            Default is $LUA_DIR
--runwith=BINARY            What Lua binary to set as runtime environment.
                            Default is $RUNWITH
--with-lua-include=DIR      You can also specify Lua's includes dir.
                            Default is \$LUA_DIR/include
--with-lua-lib=DIR          You can also specify Lua's libraries dir.
                            Default is \$LUA_DIR/lib
--with-idn=LIB              The name of the IDN library to link with.
                            Default is $IDN_LIB
--idn-library=(idn|icu)     Select library to use for IDNA functionality.
                            idn: use GNU libidn (default)
                            icu: use ICU from IBM
--with-ssl=LIB              The name of the SSL to link with.
                            Default is $OPENSSL_LIB
--cflags=FLAGS              Flags to pass to the compiler
                            Default is $CFLAGS
--ldflags=FLAGS             Flags to pass to the linker
                            Default is $LDFLAGS
--c-compiler=CC             The C compiler to use when building modules.
                            Default is $CC
--linker=CC                 The linker to use when building modules.
                            Default is $LD
--require-config            Will cause Prosody to refuse to run when
                            it fails to find a configuration file
--no-example-certs          Disables generation of example certificates.
EOF
}


while [ "$1" ]
do
   value="`echo $1 | sed 's/[^=]*=\(.*\)/\1/'`"
   if echo "$value" | grep -q "~"
   then
      echo
      echo '*WARNING*: the "~" sign is not expanded in flags.'
      echo 'If you mean the home directory, use $HOME instead.'
      echo
   fi
   case "$1" in
   --help)
      show_help
      exit 0
      ;;
   --prefix=*)
      PREFIX="$value"
      PREFIX_SET=yes
      ;;
   --sysconfdir=*)
      SYSCONFDIR="$value"
      SYSCONFDIR_SET=yes
      ;;
   --ostype=*)
      OSTYPE="$value"
      OSTYPE_SET=yes
      if [ "$OSTYPE" = "debian" ]; then
        LUA_SUFFIX="5.1";
      	LUA_SUFFIX_SET=yes
      	RUNWITH="lua5.1"
      	LUA_INCDIR=/usr/include/lua5.1;
      	LUA_INCDIR_SET=yes
      	CFLAGS="$CFLAGS -D_GNU_SOURCE"
    	fi
    	if [ "$OSTYPE" = "macosx" ]; then
        LUA_INCDIR=/usr/local/include;
      	LUA_INCDIR_SET=yes
      	LUA_LIBDIR=/usr/local/lib
      	LUA_LIBDIR_SET=yes
      	LDFLAGS="-bundle -undefined dynamic_lookup"
    	fi
      if [ "$OSTYPE" = "linux" ]; then
        LUA_INCDIR=/usr/local/include;
        LUA_INCDIR_SET=yes
        LUA_LIBDIR=/usr/local/lib
        LUA_LIBDIR_SET=yes
        CFLAGS="-Wall -fPIC -D_GNU_SOURCE"
        LDFLAGS="-shared"
      fi
      if [ "$OSTYPE" = "freebsd" -o "$OSTYPE" = "openbsd" ]; then
        LUA_INCDIR="/usr/local/include/lua51"
        LUA_INCDIR_SET=yes
        CFLAGS="-Wall -fPIC -I/usr/local/include"
        LDFLAGS="-I/usr/local/include -L/usr/local/lib -shared"
        LUA_SUFFIX="-5.1"
        LUA_SUFFIX_SET=yes
        LUA_DIR=/usr/local
        LUA_DIR_SET=yes
      fi
      if [ "$OSTYPE" = "openbsd" ]; then
        LUA_INCDIR="/usr/local/include";
      fi
      ;;
   --datadir=*)
   	DATADIR="$value"
   	DATADIR_SET=yes
      ;;
   --require-config)
      REQUIRE_CONFIG=yes
      ;;
   --lua-suffix=*)
      LUA_SUFFIX="$value"
      LUA_SUFFIX_SET=yes
      ;;
   --with-lua=*)
      LUA_DIR="$value"
      LUA_DIR_SET=yes
      ;;
   --with-lua-include=*)
      LUA_INCDIR="$value"
      LUA_INCDIR_SET=yes
      ;;
   --with-lua-lib=*)
      LUA_LIBDIR="$value" LUA_LIBDIR_SET=yes
      ;;
   --with-idn=*)
      IDN_LIB="$value"
      ;;
	--idn-library=*)
		IDN_LIBRARY="$value"
		;;
   --with-ssl=*)
      OPENSSL_LIB="$value"
      ;;
   --cflags=*)
      CFLAGS="$value"
      ;;
   --ldflags=*)
      LDFLAGS="$value"
      ;;
   --c-compiler=*)
      CC="$value"
      ;;
   --linker=*)
      LD="$value"
      ;;
   --runwith=*)
      RUNWITH="$value"
      ;;
    --no-example-certs)
      EXCERTS=
      ;;
   *)
      echo "Error: Unknown flag: $1"
      exit 1
      ;;
   esac
   shift
done

if [ "$PREFIX_SET" = "yes" -a ! "$SYSCONFDIR_SET" = "yes" ]
then
   if [ "$PREFIX" = "/usr" ]
   then SYSCONFDIR=/etc/prosody
   else SYSCONFDIR=$PREFIX/etc/prosody
   fi
fi

if [ "$PREFIX_SET" = "yes" -a ! "$DATADIR_SET" = "yes" ]
then
   if [ "$PREFIX" = "/usr" ]
   then DATADIR=/var/lib/prosody
   else DATADIR=$PREFIX/var/lib/prosody
   fi
fi

find_program() {
   path="$PATH"
   item="`echo "$path" | sed 's/\([^:]*\):.*/\1/'`"
   path="`echo "$path" | sed -n 's/[^:]*::*\(.*\)/\1/p'`"
   found="no"
   while [ "$item" ]
   do
      if [ -e "$item/$1" ]
      then
         found="yes"
         break
      fi
      item="`echo "$path" | sed 's/\([^:]*\):.*/\1/'`"
      path="`echo "$path" | sed -n 's/[^:]*::*\(.*\)/\1/p'`"
   done
   if [ "$found" = "yes" ]
   then
      echo "$item"
   else
      echo ""
   fi
}

if [ "$LUA_SUFFIX_SET" != "yes" ]
then
   for suffix in "5.1" "51" ""
   do
      LUA_SUFFIX="$suffix"
      if [ "$LUA_DIR_SET" = "yes" ]
      then
         if [ -e "$LUA_DIR/bin/lua$suffix" ]
         then
            find_lua="$LUA_DIR"
         fi
      else
         find_lua=`find_program lua$suffix`
      fi
      if [ "$find_lua" ]
      then
         echo "Lua interpreter found: $find_lua/lua$suffix..."
         break
      fi
   done
fi

if ! [ "$LUA_DIR_SET" = "yes" ]
then
   echo -n "Looking for Lua... "
   if [ ! "$find_lua" ]
   then
      find_lua=`find_program lua$LUA_SUFFIX`
      echo "lua$LUA_SUFFIX found in \$PATH: $find_lua"
   fi
   if [ "$find_lua" ]
   then
      LUA_DIR=`dirname $find_lua`
      LUA_BINDIR="$find_lua"
   else
      echo "lua$LUA_SUFFIX not found in \$PATH."
      echo "You may want to use the flags --with-lua and/or --lua-suffix. See --help."
      exit 1
   fi
fi

if ! [ "$LUA_INCDIR_SET" = "yes" ]
then
   LUA_INCDIR="$LUA_DIR/include"
fi

if ! [ "$LUA_LIBDIR_SET" = "yes" ]
then
   LUA_LIBDIR="$LUA_DIR/lib"
fi

if [ "$LUA_DIR_SET" = "yes" ]
then
   LUA_BINDIR="$LUA_DIR/bin"
fi

if [ "$IDN_LIBRARY" = "icu" ]
then
	IDNA_LIBS="$ICU_FLAGS"
	CFLAGS="$CFLAGS -DUSE_STRINGPREP_ICU"
fi
if [ "$IDN_LIBRARY" = "idn" ]
then
	IDNA_LIBS="-l$IDN_LIB"
fi

echo -n "Checking Lua includes... "
lua_h="$LUA_INCDIR/lua.h"
if [ -e "$lua_h" ]
then
   echo "lua.h found in $lua_h"
else
   echo "lua.h not found (looked in $lua_h)"
   echo "You may want to use the flag --with-lua-include. See --help."
   exit 1
fi

find_helper() {
   explanation="$1"
   shift
   tried="$*"
   while [ "$1" ]
   do
      found=`find_program "$1"`
      if [ "$found" ]
      then
         echo "$1 found at $found"
         HELPER=$1
         return
      fi
      shift
   done
   echo "Could not find a $explanation. Tried: $tried."
   echo "Make sure one of them is installed and available in your PATH."
   exit 1
}

# Write config

echo "Writing configuration..."
echo

cat <<EOF > config.unix
# This file was automatically generated by the configure script.
# Run "./configure --help" for details.

PREFIX=$PREFIX
SYSCONFDIR=$SYSCONFDIR
DATADIR=$DATADIR
LUA_SUFFIX=$LUA_SUFFIX
LUA_DIR=$LUA_DIR
LUA_INCDIR=$LUA_INCDIR
LUA_LIBDIR=$LUA_LIBDIR
LUA_BINDIR=$LUA_BINDIR
REQUIRE_CONFIG=$REQUIRE_CONFIG
IDN_LIB=$IDN_LIB
IDNA_LIBS=$IDNA_LIBS
OPENSSL_LIB=$OPENSSL_LIB
CFLAGS=$CFLAGS
LDFLAGS=$LDFLAGS
CC=$CC
CXX=$CXX
LD=$LD
RUNWITH=$RUNWITH
EXCERTS=$EXCERTS

EOF

echo "Installation prefix: $PREFIX"
echo "Prosody configuration directory: $SYSCONFDIR"
echo "Using Lua from: $LUA_DIR"

make clean > /dev/null 2> /dev/null

echo
echo "Done. You can now run 'make' to build."
echo