Skip to content

Instantly share code, notes, and snippets.

@mermshaus
Created November 4, 2012 22:32
Show Gist options
  • Save mermshaus/4014068 to your computer and use it in GitHub Desktop.
Save mermshaus/4014068 to your computer and use it in GitHub Desktop.
phermin (PHP Version Manager)
#!/bin/bash
##
# phermin -- PHP Version Manager
#
# *** USE AT OWN RISK! ***
#
# Usage: phermin [<task>] [<args>]
# Author: Marc Ermshaus <marc@ermshaus.org>
# License: MIT <http://opensource.org/licenses/mit-license.php>
# Version: 0.1.0-dev 2012-Sep-15
##
glo_retstate=0
glo_phermin_path="${HOME}/.phermin"
glo_phermin_builds_path="${glo_phermin_path}/builds"
#
#
#
phermin_echo()
{
echo "[phermin] ${1}"
}
#
#
#
phermin_task_help()
{
local content=$(cat <<EOF
help
Prints this help.
install <version>
Tries to download and install the specified PHP version.
purge
Removes ~/.phermin directory.
status
Prints status information about the phermin environment.
use <version>
Changes the target of symlink /usr/bin/php to specified version.
use-reset
Resets the target of symlink /usr/bin/php to /etc/alternatives/php.
version
Prints the phermin version number string.
EOF
)
echo "${content}"
glo_retstate=0
}
#
#
#
phermin_task_version()
{
echo "phermin 0.1.0-dev"
glo_retstate=0
}
#
#
#
phermin_task_install()
{
local version=${1}
local prefix=${glo_phermin_builds_path}
local cwd=$(pwd)
local url="http://php.net/get/php-${version}.tar.gz/from/de.php.net/mirror"
local tmpfile=$(mktemp)
local tmpdir=$(mktemp -d)
phermin_echo "Downloading ${url} to ${tmpfile}"
wget -O ${tmpfile} ${url}
if [ $? -ne 0 ];
then
# Try to grab from museum.php.net
url="http://museum.php.net/php5/php-${version}.tar.gz"
wget -O ${tmpfile} ${url} || (cd ${cwd} && exit 1)
fi
phermin_echo "Unpacking ${tmpfile} to ${tmpdir}"
tar xzpf ${tmpfile} -C ${tmpdir} || (cd ${cwd} && exit 1)
phermin_echo "Configure"
cd "${tmpdir}/php-${version}" || (cd ${cwd} && exit 1)
./configure \
--prefix="${prefix}/${version}" \
--with-libdir=/lib64 \
--with-gd \
--with-jpeg-dir \
--enable-mbstring \
--with-curl \
--with-kerberos \
--enable-bcmath \
--with-zlib \
--with-freetype-dir=/usr \
--with-gettext \
--with-openssl \
--enable-soap \
--enable-pcntl \
--with-xsl \
--with-pspell \
--enable-zip \
--with-xmlrpc \
--enable-sockets \
--enable-exif \
--enable-calendar || (cd ${cwd} && exit 1)
phermin_echo "Make"
make || (cd ${cwd} && exit 1)
phermin_echo "Make install"
make install || (cd ${cwd} && exit 1)
phermin_echo "Moving php.ini"
cp "${tmpdir}/php-${version}/php.ini-development" "${prefix}/${version}/lib/php.ini"
phermin_echo "Test"
${prefix}/${version}/bin/php --version || (cd ${cwd} && exit 1)
phermin_echo "Success"
phermin_echo "Cleaning up"
rm -rf ${tmpdir}
rm ${tmpfile}
phermin_echo "Resetting cwd"
cd ${cwd}
glo_retstate=0
}
#
#
#
phermin_task_purge()
{
phermin_task_use_reset
rm -rf ${glo_phermin_path}
phermin_echo "Purged."
glo_retstate=0
}
#
#
#
phermin_task_status()
{
echo "phermin directory: ${glo_phermin_path}"
echo "phermin builds directory: ${glo_phermin_builds_path}"
local versions=$(ls -1 ${glo_phermin_builds_path} | tr "\\n" " ")
echo "Installed versions: ${versions}"
echo -n "Symlink: "
readlink /usr/bin/php
glo_retstate=0
}
#
#
#
phermin_task_use()
{
local version=${1}
sudo rm /usr/bin/php
sudo ln -s "${glo_phermin_builds_path}/${version}/bin/php" /usr/bin/php
php --version
phermin_echo "PHP version changed to ${version}."
glo_retstate=0
}
#
#
#
phermin_task_use_reset()
{
sudo rm /usr/bin/php
sudo ln -s /etc/alternatives/php /usr/bin/php
php --version
phermin_echo "PHP version reset."
glo_retstate=0
}
mkdir -p "${glo_phermin_path}" || exit 1
mkdir -p "${glo_phermin_builds_path}" || exit 1
task="help"
if [ "$#" -ne "0" ];
then
task=${1}
fi
case ${task} in
"help")
phermin_task_help
;;
"install")
version=${2:?"missing version"}
phermin_task_install ${version}
;;
"purge")
phermin_task_purge
;;
"status")
phermin_task_status
;;
"use")
version=${2:?"missing version"}
phermin_task_use ${version}
;;
"use-reset")
phermin_task_use_reset
;;
"version")
phermin_task_version
;;
*)
phermin_echo "Unknown task \"${task}\". See \$ phermin help for list of tasks."
phermin_echo "Exiting."
glo_retstate=1
;;
esac
exit ${glo_retstate}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment