#!/bin/bash
#genArise installer
ROOT_UID=0  # Only users with $UID 0 have root privileges.
NOTROOT=67 # Non root exit error.

# Run as root, of course. 
if [ "$UID"  -ne "$ROOT_UID" ] 
then 
    echo "Must be root to run this script."
    exit $NOTROOT 
fi

echo "INSTALLATION IN PROGRESS. PLEASE WAIT..."

curl -L -O http://www.ifc.unam.mx/genarise/software/mac.tgz
echo "Extracting all files from mac.tgz"
tar -xzf mac.tgz
rm -f mac.tgz
cd mac
DIR=`pwd`
cd requirements    

IS_R=`which R | awk '{print $1}'`
VERSION=`R --version | head -1 | awk -F"." '{print $2}'`
if [ $IS_R == "no" ] 
then
    for files in `ls`
    do 
        hdiutil attach $files -verbose
    done
    echo "Installing R for Mac OS X 10.3.9"
    echo "or higher on the PowerPC platform" 
    cd /Volumes/R-[0-9]\.[0-9]\.[0-9]
    sudo installer -pkg R.mpkg -target /
else
    if [ "$VERSION" -lt 1 ]
    then
	echo "Your R version is too old. We recommend you to update it."
    fi
    hdiutil attach ActiveTcl[0-9]\.[0-9].* -verbose
fi  
echo "Installing ActiveTcl for Mac OS X" 
cd /Volumes/ActiveTcl-[0-9]\.[0-9]
sudo installer -pkg ActiveTcl-[0-9]\.[0-9].pkg -target /

cd $DIR
cd packages  
echo "Installing the packages needed for genArise"         
echo "install.packages(c(\"akima\",\"locfit\",\"xtable\"),repos=\"http://cran.r-project.org\")" > tmp.R
R --vanilla --slave < tmp.R
rm -f tmp.R

for files in `ls`
do
    R CMD INSTALL $files
done

echo "   ******************************************************"
echo "   * Ready. genArise is now installed in your system!!! * "
echo "   ******************************************************"

