#!/bin/bash

# Installer script for espam
# Author: Mohamed Bamakhrama (mohamed@liacs.nl)
# Copyrights (c) 2012, Leiden University. All rights reserved


##################################################################
# 
#  Packages
#
##################################################################

GMP_PACKAGE_URL=http://daedalus.liacs.nl/espam/release_20120710/gmp-4.3.1.tar.gz
GMP_PACKAGE=`basename $GMP_PACKAGE_URL`
GMP_DIR_NAME=gmp-4.3.1

POLYLIB_PACKAGE_URL=http://daedalus.liacs.nl/espam/release_20120710/polylib-5.22.4.tar.gz
POLYLIB_PACKAGE=`basename $POLYLIB_PACKAGE_URL`
POLYLIB_DIR_NAME=polylib-5.22.4

ESPAM_PACKAGE_URL=http://daedalus.liacs.nl/espam/release_20120710/espam-1.9.0.tgz
ESPAM_PACKAGE=`basename $ESPAM_PACKAGE_URL`
ESPAM_DIR_NAME=espam-1.9.0

##################################################################
# 
#  Functions
#
##################################################################

# Takes two parameters. The response from a command and error message
# If the response is not zero, print the error message and exit
check() {
    if [ $1 -ne 0 ]
    then
        echo "ERROR: $2"
        exit 1
    fi
}


# This function downloads and extracts espam components. Requires Internet access
download_extract_packages() {

    # Download phase
    echo "wget -q -nc $GMP_PACKAGE_URL"
    wget -q -nc $GMP_PACKAGE_URL
    check $? "can not download $GMP_PACKAGE"

    echo "wget -q -nc $POLYLIB_PACKAGE_URL"
    wget -q -nc $POLYLIB_PACKAGE_URL
    check $? "can not download $POLYLIB_PACKAGE"    

    echo "wget -q -nc $ESPAM_PACKAGE_URL"
    wget -q -nc $ESPAM_PACKAGE_URL
    check $? "can not download $ESPAM_PACKAGE"

    # Extraction phase
    tar -xvzf $GMP_PACKAGE
    check $? "can not extract $GMP_PACKAGE"  

    tar -xvzf $POLYLIB_PACKAGE
    check $? "can not extract $POLYLIB_PACKAGE"      
    
    tar -xvzf $ESPAM_PACKAGE
    check $? "can not extract $ESPAM_PACKAGE"    
    
}


install_gmp() {
	echo "cd $GMP_DIR_NAME"
	cd $GMP_DIR_NAME
	check $? "can not access $GMP_DIR_NAME"
	
	echo "./configure --prefix=$ESPAM_DIR/$ESPAM_DIR_NAME"
	./configure --prefix=$ESPAM_DIR/$ESPAM_DIR_NAME
	check $? "problem encountered while executing ./configure"
	
	echo "make"
	make -j $NUM_OF_PROCESSORS
	check $? "problem encountered while executing make"
	
	make install
	check $? "problem encountered while executing make install"
	
	cd ..
}


install_polylib() {
	echo "cd $POLYLIB_DIR_NAME"
	cd $POLYLIB_DIR_NAME
	check $? "can not access $POLYLIB_DIR_NAME"
	
	echo "./configure --prefix=$ESPAM_DIR/$ESPAM_DIR_NAME --enable-longlongint-lib --with-libgmp=$ESPAM_DIR/$ESPAM_DIR_NAME"
	./configure --prefix=$ESPAM_DIR/$ESPAM_DIR_NAME --enable-longlongint-lib --with-libgmp=$ESPAM_DIR/$ESPAM_DIR_NAME
	check $? "problem encountered while executing ./configure"
	
	echo "make"
	make -j $NUM_OF_PROCESSORS
	check $? "problem encountered while executing make"
	
	make install
	check $? "problem encountered while executing make install"
	
	cd ..
}

install_espam() {

    echo "cd $ESPAM_DIR_NAME"
    cd $ESPAM_DIR_NAME
    check $? "can not access $ESPAM_DIR_NAME"

    echo "./configure --prefix=`pwd` --with-java=$JAVA_SDK_PATH"
    ./configure --prefix=`pwd` --with-java=$JAVA_SDK_PATH
    check $? "problem encountered while executing ./configure"

    echo "make"
    make
    check $? "problem encountered while executing make"
    
	cd src_lib
	check $? "can not access src_lib"
	
	echo "./configure --prefix=$ESPAM_DIR/$ESPAM_DIR_NAME --with-espam=$ESPAM_DIR/$ESPAM_DIR_NAME --with-polylib=$ESPAM_DIR/$ESPAM_DIR_NAME --with-java=$JAVA_SDK_PATH"
	./configure --prefix=$ESPAM_DIR/$ESPAM_DIR_NAME --with-espam=$ESPAM_DIR/$ESPAM_DIR_NAME --with-polylib=$ESPAM_DIR/$ESPAM_DIR_NAME --with-java=$JAVA_SDK_PATH
	check $? "problem encountered while executing ./configure"
	
	make
	check $? "problem encountered while executing make"
	
	cp lib/libpandapolylib.so ../lib/
	check $? "can not copy lib/libpandapolylib.so to ../lib"

    echo "cd .."
    cd ..
}



#############################################################
#
# Start of the setup procedure
#
#############################################################

if [ "$1" = "clean" ] 
then
    # Clean existing directories
    rm -f $ESPAM_PACKAGE   
    rm -rf $ESPAM_DIR_NAME
        
    echo "All generated files removed"
    exit 0
fi

if [ -d "$1" ]
then
	JAVA_SDK_PATH=$1
else
	JAVA_SDK_PATH=$(readlink -f /usr/bin/javac | sed "s:bin/javac::")
fi

ARCH_TYPE=`uname -m`

# Used to speed up the build and check processes
NUM_OF_PROCESSORS=`cat /proc/cpuinfo | grep processor | wc -l`

ESPAM_DIR=`pwd`

download_extract_packages

install_gmp

install_polylib

install_espam

echo "espam installed successfully!"
exit 0

