# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../php/php.conf
# Copyright (C) 2006 - 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

if [ "$prefix_auto" == 1 ]; then
	prefix=opt/php
	set_confopt
fi

# seems not to handle --libdir...
var_append confopt ' ' "--with-libdir=${libdir##*/}"

# global php.ini goes into sysconfdir
var_append confopt ' ' "--with-config-file-path=$sysconfdir"

if pkginstalled apache; then
	pkgprefix -t apache
	var_append extraconfopt " " "--with-apxs2=$( pkgprefix sbindir apache )/apxs"
else
	var_append extraconfopt " " "--enable-fastcgi"
	var_append extraconfopt " " "--enable-force-cgi-redirect"
fi

if pkginstalled zlib; then
	var_append extraconfopt " " "--with-zlib"
fi

if pkginstalled bzip2; then
	var_append extraconfopt " " "--with-bz2"
fi

if pkginstalled mysql; then
	pkgprefix -t mysql

	if [ $? -eq 0 -o "$SDECFG_PKG_PHP_FORCESHARED" = 1 ]; then
		pkg_php_shared="=$root/$( pkgprefix mysql )"
		var_append CC_WRAPPER_APPEND ' ' "-Wl,--rpath $( pkgprefix libdir mysql )/mysql"
	else
		pkg_php_shared=
	fi
	var_append extraconfopt " " "--with-mysql$pkg_php_shared"

	var_append extraconfopt " " "--enable-mysqli"
	var_append extraconfopt " " "--with-mysqli=$( pkgprefix bindir mysql )/mysql_config"
fi

if pkginstalled postgresql; then
	pkgprefix -t postgresql
	var_append extraconfopt " " "--with-pgsql=$root/$( pkgprefix postgresql )"
fi

if pkginstalled openldap; then
	pkgprefix -t openldap
	var_append extraconfopt " " "--with-ldap=$root/$( pkgprefix openldap )"
fi

pkginstalled curl && var_append extraconfopt " " "--with-curl"
pkginstalled openssl && var_append extraconfopt " " "--with-openssl"

if pkginstalled libmcrypt; then
	pkgprefix -t libmcrypt
	var_append extraconfopt " " "--with-mcrypt=$root/$( pkgprefix libmcrypt )"
fi

pkginstalled libmhash && var_append extraconfopt " " "--with-mhash"

pkginstalled gettext &&
	var_append extraconfopt " " "--with-gettext"

pkginstalled libgd &&
	var_append extraconfopt " " "--with-gd --enable-gd-native-ttf"

pkginstalled libjpeg && var_append extraconfopt " " "--with-jpeg-dir"
pkginstalled libpng && var_append extraconfopt " " "--with-png-dir"

pkginstalled freetype1 && var_append extraconfopt " " "--with-ttf"

pkginstalled pcre && var_append extraconfopt ' ' "--with-pcre-regex=$root/$( pkgprefix pcre )"

if pkginstalled freetype; then
	pkgprefix -t freetype
	var_append extraconfopt " " "--with-freetype-dir=$root/$( pkgprefix freetype )"
fi

if [ "$SDECFG_PKG_PHP_EXTRACONFOPT" ]; then
	var_append extraconfopt " " "$SDECFG_PKG_PHP_EXTRACONFOPT"
fi

hook_add postmake 5 'cp php.ini-* $root$docdir'

# HACK ALERT
fix_configure() {
	sed -i "s,lib/lib,${libdir##*/}/lib,g" configure
	for i in PHP_LDAP PHP_LDAP_SASL; do
		sed -i "s,$i/lib,$i/${libdir##*/}," configure
	done
}
[[ $libdir != *lib ]] && hook_add preconf 5 "fix_configure"

var_append makeinstopt ' ' "INSTALL_ROOT=$root"