#!/bin/sh

# Run this script to install MODELLER interactively, and to install 
# the current user as a MODELLER user. 

# Function to guess Unix system architecture from 'uname' output
guess_arch () {
  U_M=`uname -m 2>/dev/null` || U_M=UNK
  U_S=`uname -s 2>/dev/null` || U_S=UNK
  U_P=`uname -p 2>/dev/null` || U_P=UNK

  MACHID=${U_M}:${U_S}:${U_P}

  DEFexectype=1
  case "${MACHID}" in
    i686:Linux:*)
      # Linux Pentium-class PC
      DEFexectype=1
      ;;
    ia64:Linux:*)
      # Linux Itanium-class (64-bit) PC
      DEFexectype=7
      ;;
    alpha:*:alpha)
      # Alpha box
      DEFexectype=4
      ;;
    *:Darwin:powerpc)
      # Darwin (Mac OS X)
      DEFexectype=6
      ;;
    i386:*BSD:*)
      # Free/Net/Open BSD running on a PC - usually Linux-compatible
      DEFexectype=1
      ;;
    *:AIX:powerpc)
      # IBM AIX box
      DEFexectype=5
      ;;
    *:IRIX:mips)
      # Silicon Graphics workstation
      DEFexectype=3
      ;;
    *:SunOS:sparc)
      # Sun Solaris running on the Sparc architecture
      DEFexectype=2
      ;;
  esac
}

# set default values:
version="7v7"
MOD=MODELLER$version
exectype1="i386-intel"
exectype2="sun4"
exectype3="iris4d"
exectype4="alpha"
exectype5="rs6000"
exectype6="mac10v2-absoft"
exectype7="ia64-intel8"

# Set DEFexectype by examining 'uname' output
guess_arch

DEFmodinstall=`(cd ; pwd)`/bin/modeller$version

clear
echo "Installation of $MOD"
echo
echo "This script will install $MOD into a specified directory"
echo "for which you have read/write permissions."
echo 
echo "To accept the default answers indicated in [...], press <Enter> only."
echo

echo "------------------------------------------------------------------------"
echo
echo "The currently supported architectures are as follows:"
echo
echo '   1) Linux PC (e.g. RedHat, SuSe) or FreeBSD with Linux compat. libs.'
echo '   2) SUN Inc. Solaris workstation.'
echo '   3) Silicon Graphics Inc. IRIX workstation.'
echo '   4) DEC Inc. Alpha OSF/1 workstation.'
echo '   5) IBM AIX OS.'
echo '   6) Apple Mac OS X 10.2.x (Jaguar) or later.'
echo '   7) IA64 (Itanium 2) Linux PC.'
echo
echo -n "Select the type of your computer from the list above [$DEFexectype]: "
read ans
if [ x$ans != x ] ; then
  exectype=$ans
else
  exectype=$DEFexectype
fi
if [ $exectype = 1 ] ; then EXECUTABLE_TYPE7v7=$exectype1 ; fi
if [ $exectype = 2 ] ; then EXECUTABLE_TYPE7v7=$exectype2 ; fi
if [ $exectype = 3 ] ; then EXECUTABLE_TYPE7v7=$exectype3 ; fi
if [ $exectype = 4 ] ; then EXECUTABLE_TYPE7v7=$exectype4 ; fi
if [ $exectype = 5 ] ; then EXECUTABLE_TYPE7v7=$exectype5 ; fi
if [ $exectype = 6 ] ; then EXECUTABLE_TYPE7v7=$exectype6 ; fi
if [ $exectype = 7 ] ; then EXECUTABLE_TYPE7v7=$exectype7 ; fi


echo
echo
echo "Full directory name for the installed $MOD"
echo -n "[$DEFmodinstall]: "
read ans
if [ x$ans != x ] ; then 
   MODINSTALL7v7=$ans
else
   MODINSTALL7v7=$DEFmodinstall
fi
if [ ! -d $MODINSTALL7v7 ] ; then
  echo "There is no such directory; it will be created."
fi


echo 
echo
echo "KEY_$MOD, obtained from our academic license server at"
echo -n "http://salilab.org/modeller/registration.shtml: "
read KEY_MODELLER


echo
echo "------------------------------------------------------------------------"
echo
echo "The following settings will be used: "
echo
echo "The type of this machine          : $EXECUTABLE_TYPE7v7"
echo "Destination installation directory: $MODINSTALL7v7"
echo "KEY_$MOD                   : $KEY_MODELLER"
echo
echo "If they are incorrect, abort installation (ctrl-c) and run Install again."
echo
echo -n "Press <Enter> to begin the installation: "
read ans
echo
echo "------------------------------------------------------------------------"
echo

echo "Installing Modeller files ..."

mkdir -p ${MODINSTALL7v7}
cp -pR README INSTALLATION doc examples modlib ${MODINSTALL7v7}
mkdir -p ${MODINSTALL7v7}/bin
cp -pR bin/*.top bin/mod7v7_${EXECUTABLE_TYPE7v7} ${MODINSTALL7v7}/bin

echo
echo "------------------------------------------------------------------------"
echo

echo "Creating Modeller startup script ..."

sed -e "s;EXECUTABLE_TYPE7v7=xxx;EXECUTABLE_TYPE7v7=$EXECUTABLE_TYPE7v7;" \
    -e "s;MODINSTALL7v7=xxx;MODINSTALL7v7=$MODINSTALL7v7;" \
    -e "s;KEY_MODELLER7v7=xxx;KEY_MODELLER7v7=$KEY_MODELLER;" \
    bin/modscript > ${MODINSTALL7v7}/bin/mod7v7
chmod a+x ${MODINSTALL7v7}/bin/mod7v7

echo
echo "------------------------------------------------------------------------"
echo
echo "Use the ${MODINSTALL7v7}/bin/mod7v7 script to run Modeller."
echo "You probably want to add this directory to your PATH variable, or to"
echo "create a symlink to some directory which is in the PATH, e.g. with"
echo "the command:"
echo
echo "ln -s ${MODINSTALL7v7}/bin/mod7v7 /usr/bin/"
echo
echo "------------------------------------------------------------------------"
echo

echo
echo "------------------------------------------------------------------------"
echo
echo "If you have problems with installation, please see the INSTALLATION file."
echo
echo "$MOD documentation in the PDF and HTML formats can be found in the"
echo "$MODINSTALL7v7/doc directory."
echo
echo "A simple homology modeling test case is in examples/tutorial-model/."
echo 
echo "------------------------------------------------------------------------"
echo
echo "Happy modeling!"
echo "MODELLER Team, 2004"
echo
