|
#!/usr/bin/env bash |
|
|
|
### |
|
# Manages the available links and the active alias. |
|
## |
|
function php-cfg() |
|
{ |
|
# Get the command. |
|
local COMMAND=${1}; shift |
|
|
|
# Run the command. |
|
case ${COMMAND} in |
|
default) php-cfg_default "$@" ;; |
|
define) php-cfg_define "$@" ;; |
|
list) php-cfg_list ;; |
|
remove) php-cfg_remove "$@" ;; |
|
show) php-cfg_show "$@" ;; |
|
use) php-cfg_use "$@" ;; |
|
*) php-cfg_usage ;; |
|
esac |
|
|
|
return $? |
|
} |
|
|
|
### |
|
# Sets the default link for the alias. |
|
## |
|
function php-cfg_default() |
|
{ |
|
local NAME=${1} |
|
|
|
if [ 'default' = ${NAME} ]; then |
|
echo "The default alias can not be itself." >&2 |
|
|
|
return 1 |
|
fi |
|
|
|
if [ ! -e ${PHP_CFG_LINK}/${NAME} ]; then |
|
echo "The alias does not exist." >&2 |
|
|
|
return 1 |
|
fi |
|
|
|
if [ -e ${PHP_CFG_LINK}/default ]; then |
|
rm ${PHP_CFG_LINK}/default |
|
fi |
|
|
|
ln -s ${PHP_CFG_LINK}/${NAME} ${PHP_CFG_LINK}/default |
|
|
|
return $? |
|
} |
|
|
|
### |
|
# Defines a new link for the alias. |
|
## |
|
function php-cfg_define() |
|
{ |
|
local NAME=${1} |
|
local WHERE=${2} |
|
|
|
if [ -z ${NAME} ] || [ -z ${WHERE} ]; then |
|
cat <<'USAGE' |
|
Usage: php-cfg define NAME PATH |
|
Defines a new alias to a interpreter. |
|
|
|
ARGUMENTS |
|
|
|
NAME The name of the alias. (e.g. "5.6.29") |
|
PATH The path to the interpreter. |
|
|
|
USAGE |
|
fi |
|
|
|
if [ -e ${PHP_CFG_LINK}/${NAME} ]; then |
|
local RESPONSE= |
|
|
|
while [ 'y' != ${RESPONSE} ] && [ 'n' != ${RESPONSE} ]; do |
|
read -p 'Replace existing alias? [y/N] ' RESPONSE |
|
done |
|
|
|
if [ 'n' = ${RESPONSE} ]; then |
|
echo "The alias will not be replaced." |
|
|
|
return 0 |
|
fi |
|
fi |
|
|
|
if [ ! -f ${WHERE} ]; then |
|
echo "The interpreter does not exist or is not a file." >&2 |
|
|
|
return 1 |
|
fi |
|
|
|
ln -s ${WHERE} ${PHP_CFG_LINK}/${NAME} |
|
|
|
return $? |
|
} |
|
|
|
### |
|
# Lists the links for the alias. |
|
## |
|
function php-cfg_list() |
|
{ |
|
echo "Available interpreters:" |
|
|
|
while read AVAILABLE; do |
|
echo " - $AVAILABLE" |
|
done < <(ls -1 ${PHP_CFG_LINK}) |
|
|
|
return 0 |
|
} |
|
|
|
### |
|
# Updates the alias on directory change. |
|
## |
|
function php-cfg_prompt() |
|
{ |
|
if [ "$(pwd)" != "$PHP_CFG_LAST" ]; then |
|
if [ -f ".php" ]; then |
|
local NAME="$(cat .php)" |
|
|
|
php-cfg_use ${NAME} |
|
|
|
return $? |
|
else |
|
php-cfg_use |
|
fi |
|
fi |
|
|
|
PHP_CFG_LAST="$(pwd)" |
|
} |
|
|
|
### |
|
# Removes an existing link for the alias. |
|
## |
|
function php-cfg_remove() |
|
{ |
|
local NAME=${1} |
|
|
|
if [ -e ${PHP_CFG_LINK}/${NAME} ]; then |
|
rm ${PHP_CFG_LINK}/${NAME} |
|
|
|
return $? |
|
fi |
|
|
|
return 0 |
|
} |
|
|
|
### |
|
# Shows the real path for the alias link. |
|
## |
|
function php-cfg_show() |
|
{ |
|
local NAME=${1} |
|
|
|
if [ ! -e ${PHP_CFG_LINK}/${NAME} ]; then |
|
echo "$NAME: No such alias" >&2 |
|
|
|
return 1 |
|
fi |
|
|
|
echo -n "$NAME: " |
|
|
|
readlink ${PHP_CFG_LINK}/${NAME} |
|
|
|
return $? |
|
} |
|
|
|
### |
|
# Displays the usage information. |
|
## |
|
function php-cfg_usage() |
|
{ |
|
cat <<'USAGE' |
|
Usage: php-cfg COMMAND |
|
Manages the `php` alias for the command line. |
|
|
|
COMMAND |
|
|
|
default Sets the default alias to use. |
|
define Creates a new alias to an interpreter. |
|
remove Removes an existing alias to an interpreter. |
|
show Shows the real path of the alias to the interpreter. |
|
use Switch to a different interpreter. |
|
USAGE |
|
} |
|
|
|
### |
|
# Sets the alias to a different link. |
|
## |
|
function php-cfg_use() |
|
{ |
|
local NAME=${1} |
|
|
|
if [ '' = "$NAME" ]; then |
|
unalias php 2> /dev/null |
|
|
|
if [ -e ${PHP_CFG_LINK}/default ]; then |
|
php-cfg_use default |
|
fi |
|
|
|
return 0 |
|
fi |
|
|
|
if [ ! -e ${PHP_CFG_LINK}/${NAME} ]; then |
|
echo "$NAME: No such interpreter" >&2 |
|
|
|
return 1 |
|
fi |
|
|
|
alias php=${PHP_CFG_LINK}/${NAME} |
|
} |
|
|
|
################################################################################ |
|
|
|
# Declare constants. |
|
readonly PHP_CFG_HOME=${HOME}/.php |
|
readonly PHP_CFG_LINK=${PHP_CFG_HOME}/link |
|
|
|
# Make sure the links directory exists. |
|
if [ ! -d ${PHP_CFG_LINK} ]; then |
|
mkdir -p ${PHP_CFG_LINK} |
|
fi |
|
|
|
# Set the default alias. |
|
if [ -e ${PHP_CFG_LINK}/default ]; then |
|
php-cfg_use default |
|
fi |
|
|
|
# Update alias on directory change. |
|
PROMPT_COMMAND="php-cfg_prompt;$PROMPT_COMMAND" |