#!/bin/bash
:<<__HEADER__
      title : PHP 5.4 installer
     auther : Yukari Ayamori
    version : 1.0.0
  Recommend : Scientific Linux 6.4
    Contact : @al_aya_yuka
__HEADER__


#------------------------------------------------------------
# common variable
#------------------------------------------------------------
BASE=${HOME}/opt/brew

# Source code save path.
comp_dir=${HOME}/src
# Package name.
pack_name=php
# Version
version=-5.4.36
# Compress type.
ext=.tar.gz
# Uncompress command
tar_cmd="tar zxf"
# Binary distinaion (install path).
ins_dir=${HOME}/opt/${pack_name}/${version##-}
# conifugre options.
options="
"

# Download source URL.
test ${version} == "-5.4.31" && dl_uri="http://jp1.php.net/get/php-5.4.31.tar.gz/from/this/mirror"
test ${version} == "-5.4.36" && dl_uri="http://jp1.php.net/get/php-5.4.36.tar.gz/from/this/mirror"
# Check specified version.
if test "X${dl_uri}" == "X"
then
        echo "Specified version miss match."
        exit 1
fi


# compiling
test -d ${BASE} || mkdir -p ${BASE}
test -d ${comp_dir} || mkdir -p ${comp_dir}
cd ${comp_dir}
rm -rf ${pack_name}${version}
test -f ${pack_name}${version}${ext} \
    || wget ${dl_uri} -O ${pack_name}${version}${ext}
${tar_cmd} ${pack_name}${version}${ext}
cd ${pack_name}${version}
./configure \
    --prefix=${ins_dir} \
    ${options} \
    && nice -n 15 make -j${proc_num}\
    && make install
RC=$?
test ${RC} -ne 0 && exit ${RC}


# setting
for type in $(ls -1 ${ins_dir})
do
    for src in $(find ${ins_dir}/${type} -xtype f)
    do
        parent=`dirname ${src#*/${type}/}`
        filename=`basename ${src}`
        mkdir -p ${BASE}/${type}/${parent}
        echo "ln -fs ${src} ${BASE}/${type}/${parent}/${filename}"
        ln -fs ${src} ${BASE}/${type}/${parent}/${filename}
    done
done

grep -q ${BASE}/bin ~/.bashrc \
  || echo "export PATH=${BASE}/bin:${BASE}/sbin:"'${PATH}' >> ~/.bashrc \
  ;  source ~/.bashrc


exit 0
