Skip to content
Snippets Groups Projects
DCTuser_setup.sh 4.80 KiB
# Shell script to setup a DCT user account from scratch
#
# Version 003 23-11-2012 by YGuilhem
#   Some bugfixes
#
# Version 002 02-08-2012 by YGuilhem
#   Add the possibility to install the stable version
#
# Version 001 05-07-2012 by YGuilhem

# Set current version of matlab
MATLAB_VERSION="2012a"

# Store current directory in a variable
CURRENT_DIR=`pwd`

# Check which python executable to use, python version should be >= 2.6
[ `which python26` ] && PYTHON_EXE=python26 || PYTHON_EXE=python
PYTHON_VER_MAJOR=`$PYTHON_EXE -V 2>&1 | sed -r 's/Python ([0-9]*)\.([0-9]*)\.([0-9]*)/\1/'`
PYTHON_VER_MINOR=`$PYTHON_EXE -V 2>&1 | sed -r 's/Python ([0-9]*)\.([0-9]*)\.([0-9]*)/\2/'`
if [ $PYTHON_VER_MAJOR -lt 2 ] || [ $PYTHON_VER_MINOR -lt 6 ] ; then
  echo "Python version >= 2.6 is needed for setup"
  exit 1
fi 

# Ask user which version is to be installed
DCT_DIR=
while [ -z "$DCT_DIR" ] ; do
  echo "
Which version of matlabDCT do you want to install?
  1) stable_1.0 version (recommended)
  2) development version"
  read -p "Answer: " answer
  case $answer in
    1)
      DCT_BRANCH="branches/stable_1.0"
      DCT_DIR="$HOME/matlabDCT_1.0"
      DCT_SWITCH="1.0"
      ;;
    2)
      DCT_BRANCH="trunk"
      DCT_DIR="$HOME/matlabDCT"
      DCT_SWITCH=""
      ;;
    ?)
      echo -e "\033[31mWrong answer, asking again!\033[0m"
  esac
done
DCT_REPO=https://dct.svn.sourceforge.net/svnroot/dct/${DCT_BRANCH}
[ -d $DCT_DIR ] && echo "$DCT_DIR directory already exists, so please delete or rename it before."

# Export DCT_DIR in environment for installation scripts
export DCT_DIR

# Setup SVN
SVN_VERSION=`svn --version --quiet` # needed to initialize SVN setup
echo -n "Setup SVN (version $SVN_VERSION)... "
grep -qE '^\s*http-proxy-host = proxy.esrf.fr' ~/.subversion/servers || echo "
[global]
http-proxy-host = proxy.esrf.fr
http-proxy-port = 3128
store-plaintext-passwords = no
" >> ~/.subversion/servers
echo "Done."

# Do the svn checkout
echo "Checkout matlabDCT code
from: ${DCT_REPO}
to directory: ${DCT_DIR}"
svn co ${DCT_REPO} ${DCT_DIR}

# Check if .bashrc_private is sourced by .bashrc, if not, add it
if ! grep -qE '^\s*[^#].+\. ~/.bashrc_private' ~/.bashrc ; then
  echo "Adding line in in .bashrc file to source .bashrc_private file..."
  if [ -w ~/.bashrc ] ; then
    echo -e '\n[ -f ~/.bashrc_private ] && . ~/.bashrc_private' >> ~/.bashrc
  else
    echo "The .bashrc file is not writable, so adding writable bit before!"
    chmod +w ~/.bashrc
    echo -e '\n[ -f ~/.bashrc_private ] && . ~/.bashrc_private' >> ~/.bashrc
    chmod -w ~/.bashrc
  fi
fi

# Append setup lines to .bashrc_private
echo -n "Appending setup lines to your .bashrc_private file... "
cat ${DCT_DIR}/zUtil_Conf/bashrc.example >> ~/.bashrc_private
echo -e '\n# special for ID11: 
source /sware/exp/fable/bin/fable.bash' >> ~/.bashrc_private
echo -e '\n[ -f /sware/exp/fable/bin/fable.bash ] 
&& . /sware/exp/fable/bin/fable.bash' >> ~/.bashrc_private
echo "Done."

# Ask if the user wants to use this version by default
echo "Do you want to use this version by default?
(Any different answer than \"y\" is treated as no)"
read -p "Answer: " answer
[ "$answer" = "y" ] && sed -ri 's/^(\s*switchDCT)\s*([^\s{]*)$/\1 '$DCT_SWITCH'/' ~/.bashrc_private

# Get good version of startup.m for matlab
echo -n "Get matlab startup file... "
ln -sf "${DCT_DIR}/DCTuser_startup.m" ~/startup.m
echo "Done."

# Create matlabDCT configuration files
echo -n "Creating configuration files from examples... "
cd ${DCT_DIR}
${PYTHON_EXE} "zUtil_Python/update_DCT_conf.py"
cd ${CURRENT_DIR}
echo "Done."
echo "(Feel free to modify them afterwards to fit with your system)"

# Prepare mexopts.sh configuration file
MATLAB_PATH=/sware/com/matlab_${MATLAB_VERSION}
USER_MEXOPTS=~/.matlab/R${MATLAB_VERSION}/mexopts.sh
[ -f $USER_MEXOPTS ] || { echo 1 | ${MATLAB_PATH}/bin/mex -setup ; }
[ -w $USER_MEXOPTS ] || chmod +w $USER_MEXOPTS
## On rnice (centos5), set proper compilers. Not needed on crunch (debian6).
[ "${HOSTNAME:0:5}" = "rnice" ] && sed -i \
  -e "s/CC='gcc'/CC='gcc44'/" \
  -e "s/CXX='g++'/CXX='g++44'/" \
  -e "s/FC='gfortran'/FC='gfortran44'/" \
  $USER_MEXOPTS
## For any machine, set OpenMP switch for mutithreading
sed -ri \
  -e "s/COPTIMFLAGS='-O -DNDEBUG'/COPTIMFLAGS='-O -DNDEBUG -fopenmp'/" \
  -e "s/CXXOPTIMFLAGS='-O -DNDEBUG'/CXXOPTIMFLAGS='-O -DNDEBUG -fopenmp'/" \
  -e 's/(CLIBS="\$RPATH \$MLIBS -lm)"/\1 -fopenmp"/' \
  -e 's/(CXXLIBS="\$RPATH \$MLIBS -lm)"/\1 -fopenmp"/' \
  $USER_MEXOPTS

# For this session only, manually run these lines
echo -n "Setup session... "
source ${DCT_DIR}/setup_gt_env
echo "Done."

# Compile MEX and batch functions
cd ${DCT_DIR}
echo "Compiling MEX files..."
${PYTHON_EXE} "zUtil_Python/compile_mex_functions.py"
echo "Compiling batch functions... (may take several minutes)"
${PYTHON_EXE} "zUtil_Python/compile_matlab_functions.py"
cd ${CURRENT_DIR}
echo "Done."