#!/usr/bin/env bash

if   [ $(uname -m) = 'x86_64' ]; then
  echo "System is x86_64"
  pkg="64"
elif [ $(uname -m) = 'i386' ]; then 
  echo "System is i386"
  pkg="32"
elif [ $(uname -m) = 'i686' ]; then
  echo "System is i686"
  pkg="32"
fi

bar="===============================================================================================>"

if [ -d "ccp4-nightly" ]; then
    echo "ccp4-nightly directory already exists"
else
    echo $bar
    echo "mkdir ccp4-nightly"
    mkdir ccp4-nightly
fi

echo $bar
echo "cd ccp4-nightly"
cd ccp4-nightly

if [ ! -e ccp4 ]; then
    if   [ $(uname) == 'Linux' ]; then
      echo "System is Linux"
      dld_base="ccp4-7.0-linux${pkg}-latest"
    elif [ $(uname) == 'Darwin' ]; then
      echo "System is MacOSX"
      dld_base="ccp4-7.0-macosx-10.6.0-latest"
    else
      echo "Can't install"
      exit 1
    fi
      
    echo $bar
    echo "Downloading most recent copy of ccp4"
    echo $bar
    bz2_file="${dld_base}.tar.bz2"
    tar_file="${dld_base}.tar"
    echo "wget http://devtools.fg.oisin.rc-harwell.ac.uk/nightly/7.0/${bz2_file}"
    wget http://devtools.fg.oisin.rc-harwell.ac.uk/nightly/7.0/${bz2_file}
    echo $bar
    echo "Unzipping the archive"
    echo "bunzip2 ${bz2_file}"
    bunzip2 ${bz2_file}
    echo $bar
    echo "Untarring the archive"
    echo "tar -xf ${tar_file}"
    tar -xf ${tar_file} 
    rm -f ${tar_file}
    echo $bar
    echo "Creating soft link to ccp4"
    echo "ln -s ccp4-7.0/ ccp4"
    ln -s ccp4-7.0/ ccp4

    echo $bar
    echo "Setting up the fresh copy of ccp4"
    echo "./ccp4/BINARY.setup"
    ./ccp4/BINARY.setup
fi

echo "source ./ccp4/bin/ccp4.setup-sh"
source ./ccp4/bin/ccp4.setup-sh

#echo $bar
#echo "Updating ccp4-python"
#echo "ccp4-python -m pip --quiet install pip --upgrade"
#ccp4-python -m pip install pip --upgrade
#echo "ccp4-python -m pip --quiet install IPython --upgrade"
#ccp4-python -m pip install IPython --upgrade
#echo "source ./ccp4/bin/ccp4.setup-sh"
#source ./ccp4/bin/ccp4.setup-sh

echo ""
echo $bar
echo ""
echo $bar
echo ""

echo "Installation complete."
echo "You will need to source the scripts below to use ccp4:"
echo $bar
echo "bash users:"
echo "source $(pwd)/ccp4/bin/ccp4.setup-sh"
echo $bar
echo "csh users:"
echo "source $(pwd)/ccp4/bin/ccp4.setup-csh"
echo $bar


