# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: misc/jailing/parse-config
# Copyright (C) 2004 - 2005 The T2 SDE Project
# Copyright (C) 1998 - 2003 ROCK Linux Project
# 
# 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.
# --- T2-COPYRIGHT-NOTE-END ---


# Revwrite --with-<option>=<pkg_path> in confopt
# to correct <path> when <option> is relative to
# a jailed package.
#
jail_confopt_paths_rewrite() {
	# This list will grow
	for y in mysql postgresql openldap apache; do
		if eval "[ \"\${SDECFG_JAILING_$y}\" = 1 ]" ; then
			# Correct eventual misconfigured jail
			# To be removed!
			if eval "[ ! \"\${SDECFG_JAILDIR_$y}\"  ]" ; then
				eval "export \${SDECFG_JAILDIR_$y}=\
					\"${SDECFG_BASEJAIL}/$y\""
			fi

                	eval "y_path=$root/\"\${SDECFG_JAILDIR_$y}/usr\""

			var_append LDFLAGS ' ' "-L/${y_path}/lib"

			# Set correct optname and y_path
			# This will grow too
			case "$y" in
				postgresql) optname=pgsql ;;
				openldap) optname=ldap ;;
				apache) optname=apxs2
					y_path=${y_path}/sbin/apxs ;;
				*) optname=$y ;;
			esac

			# Rewrite
			confopt=$(eval "echo $confopt" | xargs -n1 | sed " \
				s|^--with-${optname}=shared.*$|--with-${optname}=shared,${y_path}|g; \
				s|^--with-${optname}=[^,]*$|--with-${optname}=${y_path}|g; \
				" | xargs)

			# Sometime pkg has internal support for <optname> so
			# if there's no =... or =shared it means internal
			# support want to be used.
			# Only mysql in php is using this ... for now
			if [ ! "`echo ${has_internal} | grep $optname`" ] ; then
				confopt=$(echo $confopt | xargs -n1 | sed " \
					s|^--with-${optname}$|--with-${optname}=${y_path}|g \
					" | xargs)
			fi
		fi
	done
	export LDFLAGS
	unset y optname y_path
}


jail_samejail() {
	eval "export SDECFG_JAILING_$1=\"\$SDECFG_JAILING_$2\""
	eval "export SDECFG_JAILDIR_$1=\"\$SDECFG_JAILDIR_$2\""
}


if [ "${SDECFG_JAILING}" = 1 ] ; then
	[ "$pkg" = php ] && jail_samejail php apache
	[ "$pkg" = subversion ] && jail_samejail subversion apache
	
	if eval "[ \"\${SDECFG_JAILING_$pkg}\" = 1 ]" ; then
		. $base/misc/jailing/jail-functions
		. $base/misc/jailing/package-functions
		if eval "[ ! \"\${SDECFG_JAILDIR_$pkg}\"  ]" ; then
			eval "export \${SDECFG_JAILDIR_$pkg}=\"${SDECFG_BASEJAIL}/$pkg\""
		fi
		eval "export jail=\"\${SDECFG_JAILDIR_$pkg}\""

		case "$pkg" in
			# Setting destvar 
			postfix) destvar='install_root' ;;
			php) destvar='INSTALL_ROOT' ;;
			*) destvar='DESTDIR' ;;
		esac

		[ -e $base/misc/jailing/$pkg.patch ] && var_append patchfiles ' ' $base/misc/jailing/$pkg.patch
		for i in `ls $base/misc/jailing/$pkg-*.patch 2>/dev/null` ; do
			var_append patchfiles ' ' "$i"
		done

		case "$pkg" in
			# Special package settings
			apache) postmake='jail_pkg_apache_postmake' ;;
			postfix) postmake='jail_pkg_postfix_postmake' ;;
			openldap) postmake='jail_ensure_users ldap' ;;
			mysql) var_append extraconfopt " " "--with-mysql-user=$pkg"
				inmake='jail_ensure_users mysql'
				postmake='jail_pkg_mysql_postmake' ;;
			php) has_internal='mysql' ;;
		esac

		createprefix='0'
		createdocs='0'
		hook_add prepare 7 jail_set_confopt
		hook_add preconf 1 jail_create
		hook_add inmake 9 '[ "$destvar" ] && var_append makeinstopt " " "$destvar=$root/$jail"'
		hook_add postmake 9 jail_copy_needed_libs
		var_append flistroot " " "$jail"

		echo_status "Jailing support activated to directory "'$root/'"$jail"
	fi
	hook_add preconf 6 'jail_confopt_paths_rewrite'
fi


