# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../php/php.conf
# Copyright (C) 2006 - 2012 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"

# --enable because no dependency
# soap also needs --enable-libxml and --with-openssl
for x in sockets mbstring mbregex calendar bcmath soap; do
	var_append confopt ' ' "--enable-${x}"
done

for x in zlib bzip2:bz2 curl openssl libmhash:mhash gettext \
libjpeg:jpeg-dir libpng:png-dir freetype1:ttf; do
    if pkginstalled "${x%:*}"; then
        var_append extraconfopt ' ' "--with-${x#*:}"
    fi
done

# pg82 is a separately maintained postgresql version 8.2.x
# please do not remove it
for x in postgresql:pgsql pg82:pgsql openldap:ldap libmcrypt:mcrypt \
pcre:pcre-regex; do
    if pkginstalled "${x%:*}"; then
        var_append extraconfopt " " "--with-${x#*:}=$( pkgprefix -r ${x%:*} )"
    fi
done

if pkginstalled apache; then
	pkgprefix -t apache
	var_append extraconfopt " " "--with-apxs2=$( pkgprefix bindir apache )/apxs"
fi

if pkginstalled freetype; then
	var_append confopt " " "--with-freetype-dir=$( pkgprefix -r freetype )"
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 )"
	else
		pkg_php_shared=
	fi
	var_append extraconfopt " " "--with-mysql$pkg_php_shared"

	var_append extraconfopt " " "--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd"
fi

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


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

# Since php 5.4.0 more than one SAPI can be built at one run
# so there is no reason to not force the FPM SAPI always
var_append extraconfopt " " "--enable-fpm"

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"