Wednesday, July 25, 2007

Scratch - Shell Script Part 1

#!/bin/sh
set -e

# Version 0.6c, 2007-06-21
# - Updated 2007-06-21 by Eric Lannert (eric.lannert@gmail.com)
# - added openldap, openssl, and bdb
# - changed flow to package by package - I found the need to run in pieces and wanted
# to make it easier for future add-on's
# - added switch to choose whether to delete source directory as this prevents incremental installs
# and recovering from errors
# - NOTE - I do not know where oracle's ftp is for the bdb, so the script assumes you
# have downloaded via http and uploaded by hand
# - abstracted wget_source to a reusable function
# - added LD_LIBRARY_PATH, CPPFLAGS, and LDFLAGS to chk_prepare
# - Updated 2007-06-08 by Chris Shymanik (chris@chipsncheese.com)
# - Will backup and re-install any old php.ini's found (see notes in wiki about this).
# - Source file detection.
# - Allows cgi-bin to be installed in a sub-directory (EXTDIR)
# - Created steps as functions, for easier troubleshooting/modification.
# - Updated various packages and did some additional code cleanup.
# - OSSP mm (Shared Memory Allocation) and bzip2 packages added.
# - Changed which binary to use, based on the new location in PHP 5.2.3.
# - Updated 2007-01-15 by Charles Wiltgen (charles@wiltgen.net)
# - Make "nicer" to help keep it from getting killed by DreamHost
# - Make less verbose to keep signal-to-noise level high
# - Updated 2006-12-25 by Carl McDade (hiveminds.co.uk)
# - Allow memory limit and freetype

#### User Configuration Options
## Domain & Directory Configuration
# Domain to install to
export DOMAIN="events.icstars.org"
# Temporary source directory
SRCDIR=${HOME}/source
# Download temporary DIST files to which directory?
DISTDIR=${HOME}/dist
# Delete contents of DISTDIR after installation? (Default: No)
DISTDEL="No"
# Delete contents of SRCDIR after installation? (Default: No)
SRCDEL="No"
# Backup your previous php.ini files to which directory (if any)?
# Please Note: You'll need to remove this directory manually if it's used.
# (!!You MUST use a trailing slash when specifying this directive!!)
BACKUPDIR=${HOME}/php5tmp/
# Install PHP5 to which directory?
INSTALLDIR=${HOME}/php5
# Install PHP5's cgi-bin files to a directory besides home?
# ie.: /home/username/mywebsite.com/sub/level/directory
EXTYESNO="No"
# Directory under home in which the cgi-bin files will be installed to
# ie.: /sub/level/directory
EXTDIR=main
# Nice Level for Processes. (Deprecated)
# Higher is nicer, lower is less nice and could get your install process killed!
NICE=19
# Wget options
WGETOPT="-t1 -T10 -w5 -q -c"

####Setup environment
chk_prepare
chkproc_paths

####Download and install each package
#LIBICONV
FOLDER="libiconv-1.11"
FILENAME=${FOLDER}.tar.gz
MIRROR1="ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${FILENAME}"
MIRROR2="http://mirrors.usc.edu/pub/gnu/libiconv/${FILENAME}"
CONFIGURESWITCHES="--enable-extra-encodings --prefix=${INSTALLDIR}"
wget_source
cd ${SRCDIR}
echo "Extracting ${FILENAME}..."
tar xzf ${DISTDIR}/${FILENAME} > /dev/null
cd ${SRCDIR}/${FOLDER}
echo "Configuring ${FOLDER}..."
./configure ${CONFIGURESWITCHES}
echo "Making ${FOLDER}..."
nice -n ${NICE} make
echo "Installing ${FOLDER}..."
nice -n ${NICE} make install
echo "Done with ${FOLDER}!"

#LIBMCRYPT
FOLDER="libmcrypt-2.5.8"
FILENAME=${FOLDER}.tar.gz
MIRROR1="ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${FILENAME}"
MIRROR2="http://umn.dl.sourceforge.net/sourceforge/mcrypt/${FILENAME}"
CONFIGURESWITCHES="--enable-extra-encodings --prefix=${INSTALLDIR}"
wget_source
cd ${SRCDIR}
echo "Extracting ${FILENAME}..."
tar xzf ${DISTDIR}/${FILENAME} > /dev/null
cd ${SRCDIR}/${FOLDER}
echo "Configuring ${FOLDER}..."
./configure ${CONFIGURESWITCHES}
echo "Making ${FOLDER}..."
nice -n ${NICE} make
echo "Installing ${FOLDER}..."
nice -n ${NICE} make install
echo "Done with ${FOLDER}!"

#LIBXML2
FOLDER="libxml2-2.6.28"
FILENAME=${FOLDER}.tar.gz
MIRROR1="ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${FILENAME}"
MIRROR2="ftp://xmlsoft.org/libxml2/${FILENAME}"
CONFIGURESWITCHES="--enable-extra-encodings --prefix=${INSTALLDIR}"
wget_source
cd ${SRCDIR}
echo "Extracting ${FILENAME}..."
tar xzf ${DISTDIR}/${FILENAME} > /dev/null
cd ${SRCDIR}/${FOLDER}
echo "Configuring ${FOLDER}..."
./configure ${CONFIGURESWITCHES}
echo "Making ${FOLDER}..."
nice -n ${NICE} make
echo "Installing ${FOLDER}..."
nice -n ${NICE} make install
echo "Done with ${FOLDER}!"

#LIBXSLT
FOLDER="libxslt-1.1.20"
FILENAME=${FOLDER}.tar.gz
MIRROR1="ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${FILENAME}"
MIRROR2="ftp://xmlsoft.org/libxml2/${FILENAME}"
CONFIGURESWITCHES=" \
--prefix=${INSTALLDIR} \
--with-libxml-prefix=${INSTALLDIR} \
--with-libxml-include-prefix=${INSTALLDIR}/include/ \
--with-libxml-libs-prefix=${INSTALLDIR}/lib/"
wget_source
cd ${SRCDIR}
echo "Extracting ${FILENAME}..."
tar xzf ${DISTDIR}/${FILENAME} > /dev/null
cd ${SRCDIR}/${FOLDER}
echo "Configuring ${FOLDER}..."
./configure ${CONFIGURESWITCHES}
echo "Making ${FOLDER}..."
nice -n ${NICE} make
echo "Installing ${FOLDER}..."
nice -n ${NICE} make install
echo "Done with ${FOLDER}!"

#MHASH
FOLDER="mhash-0.9.9"
FILENAME=${FOLDER}.tar.gz
MIRROR1="ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${FILENAME}"
MIRROR2="http://umn.dl.sourceforge.net/sourceforge/mhash/${FILENAME}"
CONFIGURESWITCHES="\
--prefix=${INSTALLDIR}"
wget_source
cd ${SRCDIR}
echo "Extracting ${FILENAME}..."
tar xzf ${DISTDIR}/${FILENAME} > /dev/null
cd ${SRCDIR}/${FOLDER}
echo "Configuring ${FOLDER}..."
./configure ${CONFIGURESWITCHES}
echo "Making ${FOLDER}..."
nice -n ${NICE} make
echo "Installing ${FOLDER}..."
nice -n ${NICE} make install
echo "Done with ${FOLDER}!"

#ZLIB
FOLDER="zlib-1.2.3"
FILENAME=${FOLDER}.tar.bz2
MIRROR1="ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${FILENAME}"
MIRROR2="http://www.zlib.net/${FILENAME}"
CONFIGURESWITCHES="\
--shared --prefix=${INSTALLDIR}"
wget_source
cd ${SRCDIR}
echo "Extracting ${FILENAME}..."
tar xzf ${DISTDIR}/${FILENAME} > /dev/null
cd ${SRCDIR}/${FOLDER}
echo "Configuring ${FOLDER}..."
./configure ${CONFIGURESWITCHES}
echo "Making ${FOLDER}..."
nice -n ${NICE} make
echo "Installing ${FOLDER}..."
nice -n ${NICE} make install
echo "Done with ${FOLDER}!"

###########################################
# SUPPORT FUNCTIONS

###########################################
## Function to check for an existing install and kill if exists.
function check_prepare()
{
# Push the install dir's bin directory into the path
export PATH=${INSTALLDIR}/bin:$PATH

# set the install dir's lib and include directories for LD_LIBRARY_PATH, CPPFLAGS and LDFLAGS
export LD_LIBRARY_PATH=${INSTALLDIR}/lib
export CPPFLAGS=-I${INSTALLDIR}/include
export LDFLAGS=-L${INSTALLDIR}/lib

# Indicate the time at which the install started.
echo "Installation commencing..." `date +%r`
echo ""

# Backup previous php.ini file(s) if exist.
if [ -d ${INSTALLDIR}/etc/php5 ]; then
echo "--- Backing up php.ini file..."
mkdir -p ${BACKUPDIR}
cp -R ${INSTALLDIR}/etc/php5/* ${BACKUPDIR}/
echo "Done."
else
echo "No php.ini will be backed-up during this update!"
fi
# Check for existing php5 install, kill any running processes from it, and remove.
# Note: This is only for fast-cgi processes. This should probably be modified
# to detect other custom php5 processes for a fully functional script.
if [ -d ${INSTALLDIR} ]
then
# Check for a running process
# Note: This is only a very basic detect & kill system.
if [ "$(ps aux | grep -q php5.fcgi)" == "php5.fcgi" ]
then
while [ "$(ps aux | grep -q php5.fcgi)" == "php5.fcgi" ]
do
kill -9 php5.fcgi
sleep 5s
done
fi
echo "Install directory exists! Killing active processes and clearing it..."
rm -rf ${INSTALLDIR}
else
echo "Install directory check complete."
fi
# Remove the existing cgi-bin directory.
if [ -d ${HOME}/${DOMAIN}/cgi-bin ]; then
echo "cgi-bin directory exists! Clearing contents..."
rm -rf ${HOME}/${DOMAIN}/cgi-bin
else
echo "cgi-bin directory check complete."
fi
}

###########################################
## Function to check for and setup the appropriate paths.
function chkproc_paths()
{
# Detect how many processors the system has (for more optimal compliation).
cores=2 # the number of cores/procs to use when building
if [ $cores -a $cores -gt 1 ]; then
j="-j$cores "
fi
OS=`uname -s`
if [ "Darwin" = $OS ]; then
sed=gnused
makefile=makefile.macosx
else
makefile=makefile.linux_x86_ppc_alpha
sed=sed
fi
for i in $sed wget; do
$i --version >/dev/null 2>&1
done

# Clear and/or create the source directory.
if [ -d ${SRCDIR} ]; then
echo ""; echo "Source directory already exists!"; echo "Clean it?"
if [ ${SRCDEL} == "Yes" ]
then
echo ""; echo "Yes!"; echo "Cleaning now..."; echo ""
rm -rf $SRCDIR/*
else
echo ""; echo "No!"; echo "Leaving the source directory intact."; echo ""
fi
else
echo "Creating source directory..."
mkdir -p ${SRCDIR}
fi
# Create the dist files directory if it doesn't exist
# optionally cleaning it if it does exist already.
if [ -d ${DISTDIR} ]; then
echo ""; echo "Distribution directory already exists!"; echo "Clean it?"
if [ ${DISTDEL} == "Yes" ]
then
echo ""; echo "Yes!"; echo "Cleaning now..."; echo ""
rm -rf $DISTDIR/*
else
echo ""; echo "No!"; echo "Leaving the distribution directory intact."; echo ""
fi
else
echo "Creating distribution directory..."
mkdir -p ${DISTDIR}
fi
}
###########################################
## Function to wget the current package
function wget_source()
{
echo "*************************"
if [ -a ${DISTDIR}/${FILENAME} ]; then
echo "Skipping wget of ${FILENAME}"
else
echo "Getting MIRROR1: ${MIRROR1}"
wget $WGETOPT $MIRROR1
# If primary mirror fails, use the alternative mirror.
if [ -a ${DISTDIR}/${FILENAME}]; then
echo "Got ${FILENAME}"
else
echo "Failed: Getting from MIRROR2: ${MIRROR2}"
wget $WGETOPT $MIRROR2
# Check to make sure the alternative mirror worked.
if [ -a ${DISTDIR}/${FILENAME} ]; then
echo "Got ${FILENAME}"
else
echo "Failed to get ${FILENAME}. Aborting install!"
exit 0
fi
fi
fi
}

No comments: