#!/bin/bash

#/*------------------------------------------------------------------------*/
#/*                                                                        */
#/*                BSC - Barcelona Supercomputing Center                   */     
#/*                           COMP Superscalar                             */      
#/*                                                                        */ 
#/*                            INSTALL SCRIPT                              */      
#/*                                                                        */
#/*   More information at COMP Superscalar Website: www.bsc.es/compss      */
#/*                                                                        */
#/*                                                                        */
#/*   Support: carlos.diaz@bsc.es                                          */
#/*                                                                        */
#/*------------------------------------------------------------------------*/

working_dir=$(pwd)/src
build_root=$1

export GS_HOME=$build_root

echo "*--------------------------------------------------------------------*"
echo "*                                                                    *"
echo "*     Installing COMP Superscalar Binding for C/C++...               *"
echo "*                                                                    *"
echo "*     For support send an e-mail to:                                 *"
echo "*     carlos.diaz@bsc.es                                             *"
echo "*                                                                    *"
echo "*                                                                    *"
echo "*     IMPORTANT: The dependencies need to be installed               *"
echo "*                before running this script.                         *"
echo "*                                                                    *"
echo "*     Dependencies: xml2 (sudo apt-get install libxml2-dev)          *"
echo "*                   bzip2 (sudo apt-get install libbz2-dev)          *"
echo "*                   libtool (sudo apt-get install libtool)           *"
echo "*                   automake (sudo apt-get install automake)         *"
echo "*                                                                    *"         
echo "*--------------------------------------------------------------------*"

if [ -z "$JAVA_HOME" ]; then
    echo " "
    echo "** INSTALLATION FAILED **";
    echo "JAVA_HOME is not defined in the environment."
    echo "Please do: export JAVA_HOME=path_to_the_java_jdk_directory"
    echo "Example: export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386"    
    exit 1;
fi

echo " "
echo "- Installation directory: $build_root"

echo " "
echo "Installing C/C++ Binding Library..."
echo " "
cd $working_dir/bindinglib

machine_arch=$(uname -p | /usr/bin/cut -c 1);
if [[ $machine_arch == i ]]; then
   arch="i386"
   subfolder="client"
fi
if [[ $machine_arch == x ]]; then
     arch="amd64"
     subfolder="server"
fi

echo "- ARCHITECTURE: $arch"
echo "- SUB_FOLDER: $subfolder"
export ARCH=$arch
export SUBFLDR=$subfolder
autoreconf -fvim
./configure --prefix=${build_root} --libdir=${build_root}/lib
make clean install
if [ $? -ne 0 ]; then 
    echo " "
    echo "BindingLib Installation failed, please check errors above!";
    exit 1;
fi

echo " "
echo "Installing GS Builder Library..."
echo " "
cd $working_dir/gsbuilder
autoreconf -fvim
./configure --prefix=${build_root} --libdir=${build_root}/lib
make clean install
if [ $? -ne 0 ]; then 
    echo " "
    echo "GSBuilder Installation failed, please check errors above!";
    exit 1;
fi

echo " "
echo "Installing GSStubGen..."
echo " "

cd $working_dir/gsstubgen
autoreconf -fvim
./configure --prefix=${build_root} --libdir=${build_root}/lib
make clean install
if [ $? -ne 0 ]; then 
    echo " "
    echo "GSStubGen Installation failed, please check errors above!";
    exit 1;
fi

echo " "
echo " "
echo "*--------------------------------------------------------------------*"
echo "*  COMP Superscalar Binding for C/C++ successfully installed!!!      *"
echo "*                                                                    *"
echo "*  More information at COMP Superscalar website: www.bsc.es/compss   *"           
echo "*--------------------------------------------------------------------*"
