#!/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
#

# ${CM_LOCAL_SRC_DIR}  - src directory
# ${CM_INSTALL_OBJ_DIR}  - obj directory
# ${CM_INSTALL_DIR}  - install dir
# ${CM_CODE_ENV_FILE}  - file that sets environment
# ${CM_CODE_UID}  - cM code UID
# ${CM_PROCESSOR_BITS}  - number of bits for the OS (32 or 64)
# ${CM_OS_LIB_DIR} - lib extension for this OS
# ${CM_PARALLEL_JOB_NUMBER} - parallel jobs if supported

# Prepare environment
echo ""
read -p "Enter installation directory of NVIDIA GPU computing toolkit 5.0: " main_dir
read -p "Enter path to libOpenCL.so: " opencl_dir

echo "" >> ${CM_CODE_ENV_FILE}
echo "# Environment variables" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_INSTALL=${main_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}_SRC=${CM_LOCAL_SRC_DIR}" >> ${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:$opencl_dir:\$LD_LIBRARY_PATH" >> ${CM_CODE_ENV_FILE}
echo "export CUDA_PATH=\$CM_${CM_CODE_UID}_INSTALL" >> ${CM_CODE_ENV_FILE}
echo "export CUDA_PATH_V5_0=\$CM_${CM_CODE_UID}_INSTALL" >> ${CM_CODE_ENV_FILE}

echo "" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_CXX=nvcc" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_OBJ_EXT=.o" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_CC=nvcc" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_FLAGS_OUTPUT=\"-o \"" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_DLL_EXT=.so" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_LIB_EXT=.a" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_FLAGS_CREATE_OBJ=-c" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_EXE_EXT=.out" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_LD_FLAGS_EXTRA=\"-lm -ldl\"" >> ${CM_CODE_ENV_FILE}

echo "" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_MAKE=make" >> ${CM_CODE_ENV_FILE%}
echo "export CM_ACC_OBJDUMP=\"cudaobjdump -d\"" >> ${CM_CODE_ENV_FILE}

echo "" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_OPENCL_INCLUDE=-I\$CM_${CM_CODE_UID}_INCLUDE" >> ${CM_CODE_ENV_FILE}
echo "export CM_ACC_OPENCL_LIB=-lOpenCL" >> ${CM_CODE_ENV_FILE}

chmod 755 ${CM_CODE_ENV_FILE}

