#!/bin/bash

#
# Installation script for cM packages.
# Part of Collective Mind Infrastructure (cM).
#
# See cM LICENSE.txt for licensing details.
# See cM Copyright.txt for copyright details.
#
# Developer(s): Grigori Fursin, started on 2011.09
#

echo ""
echo "Executing local installation script ..."
echo ""

# Checking vars
if [ "$CM_INSTALL_OBJ_DIR" != "" ] ; then mkdir -p $CM_INSTALL_OBJ_DIR; fi

pj=""
if [ "${CM_PARALLEL_JOB_NUMBER}" != "" ] ; then
  pj="-j ${CM_PARALLEL_JOB_NUMBER}"
fi


if [ "${CM_SKIP_BUILD}" != "yes" ] ; then

 #
 echo ""
 echo "Configuring ..."

 echo ""
 cd ${CM_INSTALL_OBJ_DIR}
 ${CM_LOCAL_SRC_DIR}/configure --prefix=${CM_INSTALL_DIR}
 if [ "${?}" != "0" ] ; then
   echo "Error: Configuration failed in $PWD!"
   exit 1
 fi

 # Build
 echo ""
 echo "Building ..."
 echo ""
 cd ${CM_INSTALL_OBJ_DIR}
 make $pj
  if [ "${?}" != "0" ] ; then
    echo "Error: Compilation failed in $PWD!" 
    exit 1
  fi

 # Install
 echo ""
 echo "Installing ..."
 echo ""

 make install $pj
  if [ "${?}" != "0" ] ; then
    echo "Error: Compilation failed in $PWD!" 
    exit 1
  fi

fi

# Preparing environment
echo "" >> ${CM_CODE_ENV_FILE}
echo "# Environment variables" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_INSTALL=${CM_INSTALL_DIR}" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_BIN=\$CM_${CM_CODE_UID}_INSTALL/bin" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_LIB=\$CM_${CM_CODE_UID}_INSTALL/${CM_OS_LIB_DIR}" >> ${CM_CODE_ENV_FILE}
if [ "${CM_PROCESSOR_BITS}" == "64" ] ; then
  echo "export CM_${CM_CODE_UID}_LIBS=\$CM_${CM_CODE_UID}_INSTALL/${CM_OS_LIB_DIR}:\$CM_${CM_CODE_UID}_INSTALL/lib" >> ${CM_CODE_ENV_FILE}
else
  echo "export CM_${CM_CODE_UID}_LIBS=\$CM_${CM_CODE_UID}_INSTALL/${CM_OS_LIB_DIR}" >> ${CM_CODE_ENV_FILE}
fi
echo "export CM_${CM_CODE_UID}_INCLUDE=\$CM_${CM_CODE_UID}_INSTALL/include" >> ${CM_CODE_ENV_FILE}
echo "" >> ${CM_CODE_ENV_FILE}
echo "export PATH=\$CM_${CM_CODE_UID}_BIN:\$PATH" >> ${CM_CODE_ENV_FILE}
echo "export LD_LIBRARY_PATH=\$CM_${CM_CODE_UID}_LIB:\$LD_LIBRARY_PATH" >> ${CM_CODE_ENV_FILE}

echo "" >> ${CM_CODE_ENV_FILE}
echo "export CM_CXX=clang++" >> ${CM_CODE_ENV_FILE}
echo "export CM_OBJ_EXT=.o" >> ${CM_CODE_ENV_FILE}
echo "export CM_CC=clang" >> ${CM_CODE_ENV_FILE}
echo "export CM_FC=\"\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_F90=\"\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_F95=\"\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_DLL=\"-shared -fPIC\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_DLL_EXTRA=-ldl" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_OUTPUT=-o " >> ${CM_CODE_ENV_FILE}
echo "export CM_DLL_EXT=.so" >> ${CM_CODE_ENV_FILE}
echo "export CM_LIB_EXT=.a" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_CREATE_OBJ=-c" >> ${CM_CODE_ENV_FILE}
echo "export CM_LB=\"ar rcs\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_AR=ar" >> ${CM_CODE_ENV_FILE}
echo "export CM_LB_OUTPUT=" >> ${CM_CODE_ENV_FILE}
echo "export CM_EXE_EXT=.out" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_STATIC_BIN=\"-static -fPIC\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_STATIC_LIB=\"-fPIC\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_DYNAMIC_BIN=" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_CREATE_ASM=-S" >> ${CM_CODE_ENV_FILE}
echo "export CM_ASM_EXT=.s" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_CREATE_BC=\"-c -emit-llvm\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_BC_EXT=.bc" >> ${CM_CODE_ENV_FILE}
echo "export CM_AFTER_COMPILE_TO_BC=" >> ${CM_CODE_ENV_FILE}
echo "export CM_RENAME_AFTER_COMPILE_TO_BC=yes" >> ${CM_CODE_ENV_FILE}
echo "export CM_INTERMEDIATE_OPT_TOOL=opt" >> ${CM_CODE_ENV_FILE}
echo "export CM_INTERMEDIATE_OPT_TOOL_OUT=\"-o=\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_ASM_EXT=.s" >> ${CM_CODE_ENV_FILE}
echo "export CM_LD_FLAGS_EXTRA=\"-lm -ldl\"" >> ${CM_CODE_ENV_FILE}

echo "" >> ${CM_CODE_ENV_FILE}
echo "export CM_MAKE=make" >> ${CM_CODE_ENV_FILE%}
echo "export CM_OBJDUMP=\"llvm-objdump -d\"" >> ${CM_CODE_ENV_FILE}

chmod 755 ${CM_CODE_ENV_FILE}

# Cleaning directories if needed
echo ""
echo "Cleaning directories if needed ..."
echo ""

if [ "${CM_INSTALL_DELETE_SRC_DIR}" == "yes" ] ; then rm -rf ${CM_LOCAL_SRC_DIR}; fi
if [ "${CM_INSTALL_DELETE_OBJ_DIR}" == "yes" ] ; then rm -rf ${CM_INSTALL_OBJ_DIR}; fi
