#!/bin/bash

echo "*---------------------------------------------------------------------*"
echo "*                                                                     *"
echo "*               BSC - Barcelona Supercomputing Center                 *"     
echo "*                          COMP Superscalar                           *"      
echo "*                                                                     *" 
echo "*                  C/C++ Applications - BUILD SCRIPT                  *"      
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 "ARCHITECTURE: $arch"
echo "SUBFOLDER: $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 -
export CC="gcc"
export CXX="g++"
$GS_HOME/bin/gsbuild build master $@
if [ $? -ne 0 ]; then
  echo " "
  echo "Building binding failed, please check errors above!"
  exit 1
fi
if [ "${WITH_OMPSS}" != "" ]; then
  # Flag is defined, check value
  if [ $WITH_OMPSS == 1 ]; then
    export CC="mcc"
    export CFLAGS="--ompss"
    export CXX="mcxx"
    export CXXFLAGS="--ompss"
  fi
fi
if [ "${WITH_CUDA}" != "" ]; then
  # Flag is defined, check value
  if [ $WITH_CUDA == 1 ]; then
    LDFLAGS="$LDFLAGS -L/opt/cuda/7.0/lib64 -lcudart"
  fi
fi

$GS_HOME/bin/gsbuild build worker $@
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 "*---------------------------------------------------------------------*"

