# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/misc/jailing/parse-config
# ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
# 
# 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; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-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 "[ \"\${ROCKCFG_JAILING_$y}\" = 1 ]" ; then
			# Correct eventual misconfigured jail
			# To be removed!
			if eval "[ ! \"\${ROCKCFG_JAILDIR_$y}\"  ]" ; then
				eval "export \${ROCKCFG_JAILDIR_$y}=\
					\"${ROCKCFG_BASEJAIL}/$y\""
			fi

                	eval "y_path=$root/\"\${ROCKCFG_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 ROCKCFG_JAILING_$1=\"\$ROCKCFG_JAILING_$2\""
	eval "export ROCKCFG_JAILDIR_$1=\"\$ROCKCFG_JAILDIR_$2\""
}


if [ "${ROCKCFG_JAILING}" = 1 ]
then
	[ "$pkg" = php ] && jail_samejail php apache
	
	if eval "[ \"\${ROCKCFG_JAILING_$pkg}\" = 1 ]" ; then
		. $base/misc/jailing/jail-functions
		. $base/misc/jailing/package-functions
		if eval "[ ! \"\${ROCKCFG_JAILDIR_$pkg}\"  ]" ; then
			eval "export \${ROCKCFG_JAILDIR_$pkg}=\"${ROCKCFG_BASEJAIL}/$pkg\""
		fi
		eval "export jail=\"\${ROCKCFG_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


