# Copyright 1999-2005 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Id: php.eselect,v 1.1 2005/09/04 09:47:51 stuart Exp $ DESCRIPTION="Manage the /usr/bin/php link" MAINTAINER="stuart@gentoo.org" SVN_DATE='$Date: 2005/09/04 09:47:51 $' VERSION=$(svn_date_to_version "${SVN_DATE}" ) PHP_SYMLINK_TARGETDIR="${ROOT}/usr/bin" PHP_SYMLINK_APPLIST=( php ) PHP_VERSIONS="4 5 6" PHP_HELP="php" find_libdir() { # where has multilib put the native php binary? for x in lib64 lib32 lib ; do for y in ${PHP_VERSIONS} ; do if test -d /usr/${x}/php${y}/bin ; then libdir=${x} return fi done done # if we get here, then we can't find where PHP is installed # throw in any old default - it doesn't matter, because there's # no PHP anyway libdir=/usr/lib } find_versions() { find_libdir # how many packages are we looking for? musthave=${#PHP_SYMLINK_APPLIST[@]} for f in $PHP_VERSIONS ; do (( dohave = 0 )) for (( i = 0 ; i < ${#PHP_SYMLINK_APPLIST[@]} ; i = i + 1 )) ; do n=${PHP_SYMLINK_APPLIST[$i]} [[ -f ${ROOT}/usr/${libdir}/php${f}/bin/${n} ]] && (( dohave = $dohave + 1 )) done if (( $dohave == $musthave )) ; then echo "php${f}" fi done } # try to remove the php symlink # # $1 - symlink to remove remove_symlink() { [[ -L ${1} ]] && rm -f ${1} } # set the php symlink # # $1 - symlink to set # $2 - php version to set it to set_symlink() { PHP_BASENAME=`basename ${1}` find_libdir target="${ROOT}/usr/${libdir}/$2/bin/`basename $1`" pushd "`dirname $1`" 1>/dev/null ln -s "${target}" "${PHP_BASENAME}" popd 1>/dev/null } # work out which PHP version the user has chosen from the list # # $1: the list id that the user has chosen get_phpversion_fromlist () { version=$1 versions=( $(find_versions) ) for (( i = 0 ; i < ${#versions[@]} ; i = i + 1 )) ; do if [[ ${versions[$i]} == ${version} ]] ; then echo ${version} return fi done echo "(not-found)" } get_phpversion_fromlink () { echo $1 | sed -e 's|.*/\(php[0-9]\+\)/.*|\1|;' } get_current_phpversion () { link=$(readlink ${PHP_SYMLINK_TARGETDIR}/${PHP_SYMLINK_APPLIST[0]}) version=$(get_phpversion_fromlink $link) [[ -z "$version" ]] && version="(unset)" echo $version } # $1: basename of target app # $2: php version to use set_current_phpversion() { PHP_SYMLINK_REALTARGET="${PHP_SYMLINK_TARGETDIR}/$1" if [[ -e ${PHP_SYMLINK_REALTARGET} ]] ; then # the target file exists # question is - is it a symlink, or something else? if [[ ! -L ${PHP_SYMLINK_REALTARGET} ]] ; then # it is something else # # be cautious - do not remove it die -q "${PHP_SYMLINK_REALTARGET} exists, but is not a symlink" fi fi if [[ -L ${PHP_SYMLINK_REALTARGET} ]] ; then # existing symlink if ! remove_symlink ${PHP_SYMLINK_REALTARGET} ; then die -q "Couldn't remove existing symlink" fi fi if ! set_symlink ${PHP_SYMLINK_REALTARGET} "${2}" ; then die -q "Couldn't set a new symlink" fi } ### show action ### describe_show() { echo "Show the current PHP version for ${PHP_HELP}" } do_show() { write_list_start "Current PHP version:" version=$(get_current_phpversion) write_kv_list_entry "$version" "" } ### list action ### describe_list() { echo "List available PHP versions to link to" } do_list() { versions=( $(find_versions ) ) current=$(get_current_phpversion) write_list_start "Available PHP versions:" if [[ -n ${versions[@]} ]] ; then local i for (( i = 0 ; i < ${#versions[@]} ; i = i + 1 )) ; do linkversion=${versions[${i}]} [[ $linkversion == $current ]] && \ versions[${i}]="${versions[${i}]} $(highlight '*' )" done write_numbered_list "${versions[@]}" else write_kv_list_entry "(none found)" "" fi } ### set action ### describe_set() { echo "Set PHP version used" } do_set() { if [[ -z ${1} ]] ; then # no parameter die -q "You didn't tell me which version of PHP to use" fi version=$(get_phpversion_fromlist $1) if [[ ${version} == "(not-found)" ]] ; then die -q "I don't recognise that version of PHP" fi for (( i = 0 ; i < ${#PHP_SYMLINK_APPLIST[@]} ; i = i + 1 )) ; do set_current_phpversion ${PHP_SYMLINK_APPLIST[$i]} ${version} done } # vim: set ft=eselect :