#!/bin/bash

# Installs the SBC/CBR/DBB membrane protein topology predictor suite including
# - OCTOPUS
# - SPOCTOPUS
# - SCAMPI-single
# - SCAMPI-multi
# - S-/PRO-/PRODIV-TMHMM
# - TOPCONS
# and the auxilliary software
# - MODHMM

# Requirements:
# CMAKE version 2.6 or higher
# gengetopt
# subversion
# xsltproc
# Various standard unix utilities (cat, echo, grep ...)


set -e

# Configurable paths
# prefix_path=/usr/local;
prefix_path=/home/aronh/topology_predictors_download;
modhmm_install_dir=$prefix_path/modhmm;
topology_predictors_install_dir=$prefix_path/topology_predictors;
tmpdir=/tmp/topology_predictors_download;
cmake=`which cmake`; # Have cmake in PATH or specify path to binary here
gengetopt=`which gengetopt`; # Have gengetopt in path
gengetopt_install_dir=`dirname $gengetopt`; # or specify its location directly

# Check for correct version of cmake
cmake_version=`$cmake --version | cut -b 15-`;
cmp_number=`echo $cmake_version | cut -b -3 | sed s/\\\\.//g`;
if (( $cmp_number < 26 )); then
    echo "cmake version 2.6 or newer needed. You seem to have version $cmake_version at '$cmake'";
    exit 1;
fi


[[ -d $tmpdir ]] || mkdir -pv $tmpdir;

### MODHMM ###
# Download source code
[[ -d $tmpdir/modhmm_src ]] || mkdir -pv $tmpdir/modhmm_src;
svn co https://svn.sbc.su.se/repos/cbr/modhmm-projects/trunk/modhmm $tmpdir/modhmm_src;

# Build and install
[[ -d $tmpdir/modhmm_build ]] || mkdir -pv $tmpdir/modhmm_build;
cd $tmpdir/modhmm_build;
[[ -d $modhmm_install_dir ]] || mkdir -pv $modhmm_install_dir;
cmake -D CMAKE_INSTALL_PREFIX=$modhmm_install_dir -D CMAKE_PREFIX_PATH=$gengetopt_install_dir $tmpdir/modhmm_src;
make
make install

### TOPOLOGY PREDICTORS ###
# Download source code
[[ -d $tmpdir/topology_predictors_src ]] || mkdir -pv $tmpdir/topology_predictors_src;
svn co https://svn.sbc.su.se/repos/cbr/modhmm-projects/trunk/cmdline $tmpdir/topology_predictors_src;

# Build and install
[[ -d $tmpdir/topology_predictors_build ]] || mkdir -pv $tmpdir/topology_predictors_build;
cd $tmpdir/topology_predictors_build;
[[ -d $topology_predictors_install_dir ]] || mkdir -pv $topology_predictors_install_dir;
cmake -D CMAKE_INSTALL_PREFIX=$topology_predictors_install_dir -D TARGETS="topcons;spoctopus;prodiv_tmhmm;scampi;scampi-msa" -D CMAKE_PREFIX_PATH=$modhmm_install_dir $tmpdir/topology_predictors_src
make
make install


### Clean up
rm -rf $tmpdir;
