#!/bin/bash # # /usr/sbin/php-select # Tool for managing PHP symlinks on Gentoo Linux # # Author Stuart Herbert # (stuart@gentoo.org) # # Copyright (c) 2005 Gentoo Foundation, Inc. # Released under v2 of the GNU General Public License # # ======================================================================== # # USAGE # ----- # # php-select [] # # where # is one of # php|php-cgi|php-devel|apache|apache2 # # is the directory under /usr/lib where PHP is installed # # php-select with no arguments will list all components for each installed # php-version # # php-select will list the current php-version in use for that # component # # php-select will reconfigure Gentoo to use # of . This normally involves replacing symlinks # on the system # ======================================================================== G_ACTION= G_COMPONENT= G_COPYRIGHT[0]="Copyright (c) 2005 Gentoo Foundation, Inc." G_COPYRIGHT[1]="Released under v2 of the GNU General Public License" G_EXITSTATUS=0 G_LIBDIR[0]="/usr/lib64" G_LIBDIR[1]="/usr/lib32" G_LIBDIR[2]="/usr/lib" G_MODULE_PATH="/usr/share/php-select" G_NEED_COMPONENT=0 G_NEED_PHPVERSION=0 G_PN="$0" G_PN_BASE="`basename $G_PN`" G_PVR="1.0.1" G_PHPVERSION= G_STATUS_OK=0 G_STATUS_USAGE=1 G_STATUS_NOTINSTALLED=2 G_STATUS_NOTCOMPATIBLE=3 G_STATUS_USINGOTHER=4 G_STATUS_USINGNONE=5 # ======================================================================== # The Main Actions # ======================================================================== # ------------------------------------------------------------------------ # doCopyright() - output the copyright info # # No arguments doCopyright () { local i for (( i = 0 ; i < ${#G_COPYRIGHT[@]} ; i = i + 1 )) ; do echo ${G_COPYRIGHT[$i]} done } # ------------------------------------------------------------------------ # doHelp() - show full help # # No arguments doHelp () { doVerboseVersion echo doUsageShort echo echo " $G_PN_BASE " echo " Show which version of PHP is used for " echo echo " $G_PN_BASE -t " echo " Test if uses or not" echo echo " $G_PN_BASE " echo " Set which version of PHP is used for " echo echo " $G_PN_BASE -v" echo " Show version number" echo echo " $G_PN_BASE -h" echo " Show this help" echo echo "where is one of:" echo echo " apache1 - Apache webserver v1.x" echo " apache2 - Apache webserver v2.x" echo " php - /usr/bin/php (the CLI SAPI)" echo " php-cgi - /usr/bin/php-cgi (the CGI SAPI)" echo " php-devel - the scripts used to build PECL extensions" echo echo "and where is one of:" echo echo " php4 - php 4.x" echo " php5 - php 5.x" echo echo "NOTE: none of these settings affect how Portage installs PHP or PEAR" echo " packages or PECL extensions." echo echo "Exit status is one of:" echo echo " 0 == ok" echo " 1 == usage error" echo " 2 == no compatible PHP version found" echo " 3 == incompatible PHP version specified" echo " 4 == component uses a different PHP version" echo " 5 == component not configured to use PHP" echo # doManPageDetails } # ------------------------------------------------------------------------ # doManPageDetails() - show man page details # # No arguments doManPageDetails () { echo "See 'man $G_PN_BASE' for full details" } # ------------------------------------------------------------------------ # doSetComponent() - set a component to use a particular php version # # $1: component to set # $2: php version to set the component to doSetComponent () { loadComponent $1 || exit 1 actionSet $2 } # ------------------------------------------------------------------------ # doShowComponent() - show which version of php a particular component is # currently using # # $1: component to show doShowComponent () { loadComponent $1 || exit 1 actionShow } # ------------------------------------------------------------------------ # doTest() - check to see if a particular component is using a specified # version or not # # $1: component to test doTest () { loadComponent $1 || exit 1 actionTest } # ------------------------------------------------------------------------ # doUsage() - tell the user to stop being a muppet :) # # No arguments doUsage () { doUsageShort echo "use $G_PN_BASE --help for full details" G_EXITSTATUS=1 } # ------------------------------------------------------------------------ # doUsageShort() - print out just the line to tell the user what to do # # No arguments doUsageShort () { echo "usage: $G_PN_BASE [-htv] [ [] ]" } # ------------------------------------------------------------------------ # doVerboseVersion() - show more version info # # No arguments doVerboseVersion () { doVersion doCopyright } # ------------------------------------------------------------------------ # doVersion() - show version info # # No arguments doVersion () { echo "$G_PN_BASE v$G_PVR" } # ======================================================================== # Helping Functions # ======================================================================== # ------------------------------------------------------------------------ # loadComponent() - load a component's code into the script # # $1: component to load loadComponent () { # only load one component, and only do it once if [[ -n $G_COMPONENT_LOADED ]] ; then return fi local l_module="$G_MODULE_PATH/$1.sh" if [[ ! -f $l_module ]] ; then echo "Error: module $l_module not found" return 1 fi if [[ ! -r $l_module ]] ; then echo "Error: module $l_module not readable" return 1 fi . "$l_module" && G_COMPONENT_LOADED=1 [[ $? != 0 ]] && echo "Error: component $l_module not loadable" return 0 } # ------------------------------------------------------------------------ # missingComponent() - check to see if the user did not specify which # component to work on # # No arguments missingComponent () { if [[ -z $G_COMPONENT && $G_NEED_COMPONENT == 1 ]] ; then echo "*** error: missing argument" return 0 fi return 1 } # ------------------------------------------------------------------------ # missingPhpVersion() - check to see if the user did not specify which # PHP version to use # # No arguments missingPhpVersion () { if [[ -z $G_PHPVERSION && $G_NEED_PHPVERSION == 1 ]] ; then echo "*** error: missing argument" return 0 fi return 1 } # ------------------------------------------------------------------------ # parseArgs() - work out what the user has asked us to do # # $1: a php component (optional) # $2: a php version (optional) parseArgs () { # do we have any parameters at all? if [[ -z $1 ]]; then G_ACTION=doUsage return fi # handle switches while [[ -n $1 && ${1:0:1} == '-' ]]; do case "$1" in -v|--version) G_ACTION=doVersion return ;; -V) G_ACTION=doVerboseVersion return ;; -h|-\?|--help) G_ACTION=doHelp return ;; -t|--test) G_ACTION=doTest G_NEED_COMPONENT=1 G_NEED_PHPVERSION=1 ;; *) G_ACTION=doUsage return ;; esac shift done # if we get here, we have a non-switch to examine, or we # don't have anything at all to look at if [[ -z $1 ]]; then if missingComponent || missingPhpVersion ; then G_ACTION=doUsage fi return fi # we have a component - make a note of it G_COMPONENT="$1" shift # do we have a php version too? if [[ -z $1 ]]; then if missingComponent || missingPhpVersion ; then G_ACTION=doUsage fi [[ -z $G_ACTION ]] && G_ACTION=doShowComponent return fi # yes we do - make a note of it too G_PHPVERSION="$1" [[ -z $G_ACTION ]] && G_ACTION=doSetComponent shift # we're done for now } # ------------------------------------------------------------------------ # main() - it all happens here main () { parseArgs "$@" $G_ACTION $G_COMPONENT $G_PHPVERSION exit $G_EXITSTATUS } main "$@"