#!/bin/bash
#/******************************************************************************
# *                      Script for melting v5                             *
# * This program   computes for a nucleotide probe, the enthalpy, the entropy  *
# * and the melting temperature of the binding to its complementary template.  *
# * Four types of hybridisation are possible: DNA/DNA, DNA/RNA, RNA/RNA and mRNA/RNA.  *
# *                 Copyright (C) Nicolas Le Novère and Marine Dumousseau 1997-2009*
# ******************************************************************************/
# 
#/*    This program is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; either version 2 of the License, or
#      (at your option) any later version.
#
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#
#      You should have received a copy of the GNU General Public License
#      along with this program; if not, write to the Free Software
#      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#      Nicolas Le Novère, Marine Dumousseau and William John Gowers
#      Computational Neurobiology, EMBL-EBI, Genome Campus
#      Hinxton CB10 1SD United-Kingdom. e-mail: n.lenovere@gmail.com
#*/


default_prefix=/usr/local
default_bindir='${prefix}/bin'
default_mandir=/usr/share/man
default_docdir='${prefix}/doc/MELTING'
default_srcdir='${prefix}/MELTING/src'
default_NNDIR='${prefix}/share/MELTING/Data'


#get values from arguments
while true
do
  case $# in 0) break ;; esac
  case $1 in
    --prefix) shift; prefix=$1; shift ;;
    --bindir) shift; bindir=$1; shift ;;
    --mandir) shift; mandir=$1 ;;
    --docdir) shift; docdir=$1 ;;
    --nndir) shift; NN_PATH=$1 ;;
    -h|--help) cat <<______EOF >&2; exit 0 ;;
Available options:
    --prefix directory (defaults to $default_prefix)
    --bindir directory (defaults to $default_bindir)
    --mandir directory (defaults to $default_mandir)
    --docdir directory (defaults to $default_docdir)
    --srcdir directory (defaults to $default_srcdir)
    --nndir directory (defaults to $default_NNDIR)
    --help
______EOF
    -*) cat <<______EOF >&2; exit 2 ;;
Invalid option $1.  Try $0 --help to see available options.
______EOF
    *) break ;;
  esac
done

# where to find the files containing the sets of nearest_neighbor parameters.
# This one is to initialise the default value of melting.
NN_PATH=${NN_PATH:=${prefix:-"/usr/local/share/MELTING"}"/Data"}

# MUST EXIST
prefix=${prefix:="/usr/local"}
# where to put the executables
bindir=${bindir:=${prefix}"/bin"}
mandir=${mandir:="/usr/share/man"}
docdir=${docdir:=${prefix}"/doc/MELTING"}
srcdir=${srcdir:=${prefix}"/MELTING/src"}


for d in $mandir $bindir $docdir $srcdir $NN_PATH $mandir/man1 ; do
  if [ -d $d ] && [ ! -w $d ] ; then
    echo "Can't write to" $d": Permission denied" ; exit 2;
  fi ;
done

for d in $mandir/man1 $bindir $docdir $srcdir $NN_PATH ; do
  mkdir -p $d || exit 2 ;
done

melting_execs="executable/melting executable/melting4 executable/melting5GUI executable/melting-batch"
chmod +x ${melting_execs}
cp -f ${melting_execs} ${bindir}/ || exit 2
cp -f executable/melting5.jar ${bindir}/ || exit 2
cp -f doc/melting.1 ${mandir}/man1 || exit 2
cp -i Data/* ${NN_PATH}/ || exit 2
if [ $(id -u) -eq 0 ] ; then
  profile=/etc/profile ;
else if [ -f ~/.bash_profile ] ; then
  profile=~/.bash_profile ;
else 
  profile=~/.profile ;
fi
fi

echo "" >> $profile ;
echo "export NN_PATH="$NN_PATH >> $profile ;

export NN_PATH
echo "Don't forget to set environment variable NN_PATH to" $NN_PATH "in your current shell (it is however set in $profile for your convenience)"
echo "MELTING successfully installed"



