#!/bin/bash

echo "*---------------------------------------------------------------------*"
echo "*                                                                     *"
echo "*               BSC - Barcelona Supercomputing Center                 *"     
echo "*                          COMP Superscalar                           *"      
echo "*                                                                     *" 
echo "*                  C/C++ Applications - BUILD SCRIPT                  *"      
echo "*                                                                     *"
echo "*                                                                     *"
echo "*  More information at COMP Superscalar Website: http://compss.bsc.es *"
echo "*                                                                     *"
echo "*  Support: support-compss@bsc.es                                     *"
echo "*                                                                     *"        
echo "*  Dependencies: csh (sudo apt-get install csh)                       *"
echo "*                                                                     *"
echo "*---------------------------------------------------------------------*"
echo ""

# Machine Arch Calculation
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 "ARCH: $arch"
echo "SUBFLDR: $subfolder"
export ARCH=$arch
export SUBFLDR=$subfolder

# GS HOME
if [ -z $GS_HOME ]; then
	if [ -z $IT_HOME ]; then
		export GS_HOME=/opt/COMPSs/Bindings/c
	else
		export GS_HOME=$IT_HOME/../Bindings/c
	fi
fi
export GSSTUBGEN=$GS_HOME/bin/gsstubgen

# Application src compilation
if [ ! -d "./src" ]; then
    echo " "
    echo "No application source directory found! (./src)"
    echo "This script must be executed at the root directory of your application."
    exit 1
fi
cd ./src
make
if [ $? -ne 0 ]; then 
    echo " "
    echo "Building user application failed, please check errors above!"
    exit 1
fi

# Building Master and Worker
cd -
$GS_HOME/bin/gsbuild build all $@
if [ $? -ne 0 ]; then 
    echo " "
    echo "Building binding failed, please check errors above!"
    exit 1
fi

# End
echo ""
echo "*---------------------------------------------------------------------*"
echo "*                                                                     *"
echo "*  Application successfully built!!!                                  *"
echo "*                                                                     *"
echo "*  More information at COMP Superscalar website: http://compss.bsc.es *"
echo "*                                                                     *"        
echo "*---------------------------------------------------------------------*"
