#!/bin/sh

# This script is intended to bootstrap a development environment for CCP4MG on Linux.
#
# The script will attempt to check that you have all the required OS package dependenices
# installed on Fedora/Red Hat/CentOS and Ubuntu.
#
# It does not check for these on any other OS, but it will do very soon. SJM 21/10/2011

check_ubuntu_deps(){
DEPS=""

# Dependencies for compiling
DEPS="$DEPS gcc g++ gfortran m4 flex bison yasm"

# Dependencies for Qt and Python
DEPS="$DEPS zlib1g-dev"

# Dependencies for lxml
DEPS="$DEPS libxml2-dev libxslt1-dev"

# Dependencies for ImageMagick
DEPS="$DEPS liblqr-1-0 libmagickcore3 libmagickwand3 imagemagick"

# Dependencies for Qt
DEPS="$DEPS libpng12-dev libjpeg62-dev libtiff4-dev libgif-dev"
DEPS="$DEPS libgl1-mesa-dev libglu1-mesa-dev"
DEPS="$DEPS libglib2.0-dev"
DEPS="$DEPS libdbus-1-dev"
DEPS="$DEPS libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev"
DEPS="$DEPS libsm-dev libxext-dev libxrender-dev libfreetype6-dev libfontconfig1-dev"

# Dependency for downloading
DEPS="$DEPS wget cvs openssh-client"

echo "Checking for required system packages:"
echo $DEPS
echo

UNINST=""
for dep in $DEPS; do
  if dpkg -s $dep >/dev/null 2>/dev/null; then
    true
  else
    UNINST="$UNINST $dep"
  fi
done

echo
if [ -z  "$UNINST" ]; then
  echo "All known required packages installed"
  return 0
else
  echo
  echo "The following required packages are not installed:"
  echo "$UNINST"
  echo
  echo "You should install them with the command:"
  echo "sudo apt-get install $UNINST"
  return 1
fi
}

check_fedora_deps(){
DEPS=""

# Dependencies for compiling
DEPS="$DEPS gcc gcc-c++ gcc-gfortran gcc-g77 m4 flex bison yasm" 

# Dependencies for Qt and Python
DEPS="$DEPS zlib-devel"

# Dependencies for lxml
DEPS="$DEPS libxml2-devel libxslt-devel"

# Dependencies for Qt
DEPS="$DEPS libpng libpng-devel libjpeg libjpeg-devel libtiff libtiff-devel giflib giflib-devel"
DEPS="$DEPS mesa-libGL-devel mesa-libGLU-devel"
DEPS="$DEPS glib2-devel"
DEPS="$DEPS dbus-devel dbus-glib-devel"
DEPS="$DEPS gstreamer-devel gstreamer-plugins-base-devel"
DEPS="$DEPS libSM-devel libXext-devel xorg-x11-proto-devel libXrender-devel freetype-devel fontconfig-devel"

# Dependency for downloading
DEPS="$DEPS wget cvs openssh-clients"

echo "Checking for required system packages:"
echo $DEPS
echo

UNINST=""
for dep in $DEPS; do
  if rpm -q $dep >/dev/null 2>/dev/null; then
    true
  else
    if yum list $dep >/dev/null 2>/dev/null; then
      UNINST="$UNINST $dep"
    else
      # Package does not exist, e.g. we only need one of gcc-g77 and gcc-gfortran
      if test x"$dep" != x"gcc-g77" -a x"$dep" != x"gcc-gfortran"; then
        echo "Package $dep does not exist, this may be a problem."
      fi
      if test x"$dep" = x"yasm"; then
        echo "You can try to get a package from:"
        echo "http://pkgs.repoforge.org/yasm/"
        echo
      fi
      true
    fi
  fi
done

echo
if [ -z  "$UNINST" ]; then
  echo "All known required packages installed"
  return 0
else
  echo
  echo "The following required packages are not installed:"
  echo "$UNINST"
  echo
  echo "You should install them with the command:"
  echo "sudo yum install $UNINST"
  return 1
fi
}

# A fairly recent version of gcc is required for Qt 4.7.x. Centos 4 has 3.4.6 so will only compile Qt 4.6.2.

QTVER=4.6.2
GCC_VER=`gcc -v 2>&1 | awk '/^gcc version/{print $3}'`
GCC_MAJOR_VER=`echo $GCC_VER | awk -F'.' '{print $1}'`
GCC_MINOR_VER=`echo $GCC_VER | awk -F'.' '{print $2}'`
if [ $GCC_MAJOR_VER -gt 3 ]; then
  if [ $GCC_MINOR_VER -gt 0 ]; then
    QTVER=4.7.4
  fi
fi
CCP4VER=6.2.0
PYVER=2.7.2
SIPVER=4.13
PYQTVER=4.8.6

echo "Using CCP4 version $CCP4VER"
echo "Using Qt version $QTVER"
echo "Using Python version $PYVER"
echo "Using PyQt version $PYQTVER"

usage() {
  echo "$0 [-c|-q|-p|-s|-m|-x|-i|-k|-g|-d|-a|--ccp4|--qt|--python|--pyqt|--mg|--pixie|--pisa|--magick|--ffmpeg|--distrib|--all]"
  echo
  echo "One of the following options must be given to this program:"
  echo
  echo "-c|--ccp4 Download (if necessary) and build the CCP4 libraries"
  echo "-q|--qt Download (if necessary) and build the Qt libraries"
  echo "-p|--python Download (if necessary) and build Python"
  echo "-s|--pyqt Download (if necessary) and build PyQt"
  echo "-m|--mg CVS check out and build CCP4MG"
  echo "-k|--magick Copy ImageMagick binaries into MG build tree"
  echo "-x|--pixie Download and build Pixie"
  echo "-g|--ffmpeg Download and build ffmpeg"
  echo "-i|--pisa Download (if necessary) and build the slightly modified"
  echo "          version of PISA for CCP4MG"
  echo "-d|--distrib Copy Qt libs into MG build tree and then do make distrib."
  echo "-a|--all Do all of the above."
  echo
  echo "You will want to perform the steps in the order above or specify -a|--all."
  echo "This script knows nothing about any existing copies of CCP4, Qt, Python"
  echo "that you may have, and will almost certainly not work with them."
  echo "If you wish to use existing copies of such software you should follow"
  echo "the instructions at"
  echo "./MG/ccp4mg_help2/linux-build.html"
}

set -- $(getopt -o cqpsmxifkgda -l ccp4,qt,python,pyqt,mg,mgcvs,pixie,pisa,force,magick,ffmpeg,distrib,all -- "$@")
while [ $# -gt 0 ]
do
    case "$1" in
    -c|--ccp4) ccp4_flag=yes;;
    -q|--qt) qt_flag=yes;;
    -p|--python) python_flag=yes;;
    -s|--pyqt) pyqt_flag=yes;;
    -m|--mg) mg_flag=yes;;
    --mgcvs) mgcvs_flag=yes; mg_flag=yes;;
    -k|--magick) magick_flag=yes;;
    -x|--pixie) pixie_flag=yes;;
    -g|--ffmpeg) ffmpeg_flag=yes;;
    -i|--pisa) pisa_flag=yes;;
    -d|--distrib) distrib_flag=yes;;
    -f|--force) force_flag=yes;;
    -a|--all) ffmpeg_flag=yes distrib_flag=yes magick_flag=yes pisa_flag=yes  pixie_flag=yes mg_flag=yes pyqt_flag=yes python_flag=yes qt_flag=yes ccp4_flag=yes;;
    (--) shift; break;;
    (-*) echo "$0: error - unrecognized option $1" 1>&2; usage; exit 1;;
    (*)  break;;
    esac
    shift
done

if [ -r /etc/debian_version ]; then
  check_ubuntu_deps
  rc=$?
elif [ -r /etc/redhat-release ]; then
  check_fedora_deps
  rc=$?
elif [ -r /etc/SuSE-release ]; then
  echo "I cannot check pacakge dependencies because"
  echo "I have not written the checks for SUSE, yet."
  echo 
  echo "Nothing is guaranteed to work from here on you should probably"
  echo "and adapt the instructions at:"
  echo 
  echo "./MG/ccp4mg_help2/linux-build.html"
  echo 
  echo "until this script is updated."
  rc=1
elif [ -r /etc/mandrake-release ]; then
  echo "I cannot check pacakge dependencies because"
  echo "I have not written the checks for Mandriva, yet."
  echo 
  echo "Nothing is guaranteed to work from here on you should probably"
  echo "and adapt the instructions at:"
  echo 
  echo "./MG/ccp4mg_help2/linux-build.html"
  rc=1
else
  echo "I cannot check pacakge dependencies because"
  echo "I do not know what type of Linux you are on. You will probably"
  echo "have to email ccp4mg@ysbl.york.ac.uk with the name/verson of"
  echo "your Linux distro so that this script can be updated."
  echo 
  echo "Nothing is guaranteed to work from here on you should probably"
  echo "and adapt the instructions at:"
  echo 
  echo "./MG/ccp4mg_help2/linux-build.html"
  echo 
  echo "until this script is updated."
  rc=1
fi

if test x"$rc" = x"1" -a x"$force_flag" != x"yes" ; then
  echo "There was a problem with OS package dependencies, or"
  echo "they cannot be checked with this script"
  echo 
  echo "Run this script with -f if you wish to try compiling CCP4MG anyway"
  exit
fi

if test x"$ffmpeg_flag" != x"yes" -a x"$distrib_flag" != x"yes" -a x"$magick_flag" != x"yes" -a x"$ccp4_flag" != x"yes" -a x"$qt_flag" != x"yes" -a x"$python_flag" != x"yes"  -a x"$pyqt_flag" != x"yes" -a x"$mg_flag" != x"yes" -a x"$pixie_flag" != x"yes" -a x"$pisa_flag" != x"yes"; then
 usage
 exit 1
fi

if [ x"$ccp4_flag" = x"yes" ]; then
  # BUILD CCP4
  if [ ! -r ccp4-$CCP4VER-core-src.tar.gz ]; then
    wget ftp://ftp.ccp4.ac.uk/ccp4/$CCP4VER/ccp4-$CCP4VER-core-src.tar.gz
  fi
  
  rm -fr ccp4-$CCP4VER
  tar zxvf ccp4-$CCP4VER-core-src.tar.gz
  
  cd ccp4-$CCP4VER
  
  CCP4=`pwd`
  CCP4_SCR=/tmp/$USER
  BINSORT_SCR=$CCP4_SCR
  CCP4_OPEN=UNKNOWN
  CLIB=$CCP4/lib
  CLIBD=$CCP4/lib/data
  CLIBS=$CCP4/lib/src
  CINCL=$CCP4/include
  CDOC=$CCP4/doc
  CHTML=$CCP4/html
  PUBLIC_FONT84=$CLIB/font84.dat
  export CCP4 CCP4_SCR BINSORT_SCR CCP4_OPEN CLIB CLIBD CLIBS CINCL PUBLIC_FONT84 CDOC CHTML
  
  if [ x`uname -m` = x"x86_64" ]; then
    ./configure --onlylibs --with-shared-libs
  else
    ./configure --onlylibs --non_shared
  fi
  
  make onlylib
  cd -
fi

if [ x"$python_flag" = x"yes" ]; then
  # BUILD PYTHON
  if [ ! -r Python-$PYVER.tgz ]; then
    wget http://www.python.org/ftp/python/$PYVER/Python-$PYVER.tgz
  fi
  rm -fr Python-$PYVER
  tar zxvf Python-$PYVER.tgz
  cd Python-$PYVER
  ./configure --prefix=`pwd`/../ccp4mg/pythondist
  make
  make install
  SHORTPYVER=`echo $PYVER | awk -F'.' '{print $1"."$2}'`
  wget http://pypi.python.org/packages/${SHORTPYVER}/s/setuptools/setuptools-0.6c11-py${SHORTPYVER}.egg
  PATH=`pwd`/../ccp4mg/pythondist/bin:$PATH sh setuptools-0.6c11-py${SHORTPYVER}.egg
  PATH=`pwd`/../ccp4mg/pythondist/bin:$PATH easy_install lxml
  PATH=`pwd`/../ccp4mg/pythondist/bin:$PATH easy_install ZODB3
  PATH=`pwd`/../ccp4mg/pythondist/bin:$PATH easy_install suds
  PATH=`pwd`/../ccp4mg/pythondist/bin:$PATH easy_install numpy
  PATH=`pwd`/../ccp4mg/pythondist/bin:$PATH easy_install matplotlib
  cd -
fi

if [ x"$qt_flag" = x"yes" ]; then
  # BUILD QT
  if [ ! -r qt-everywhere-opensource-src-${QTVER}.tar.gz ]; then
    wget ftp://ftp.trolltech.com/qt/source/qt-everywhere-opensource-src-${QTVER}.tar.gz
  fi
  rm -fr qt-everywhere-opensource-src-${QTVER}
  tar zxvf qt-everywhere-opensource-src-${QTVER}.tar.gz
  cd qt-everywhere-opensource-src-${QTVER}
  # phonon should be tested for. Not possible to do on all systems.
  ./configure --prefix=`pwd`/../Qt-${QTVER} -confirm-license -opensource -webkit -phonon -opengl -svg
  make
  make install
  cd -
fi

if [ x"$pyqt_flag" = x"yes" ]; then
  # BUILD PYQT
  if [ ! -r sip-$SIPVER.tar.gz ]; then
    wget http://www.riverbankcomputing.co.uk/static/Downloads/sip4/sip-$SIPVER.tar.gz
  fi
  if [ ! -r PyQt-x11-gpl-$PYQTVER.tar.gz ]; then
    wget http://www.riverbankcomputing.co.uk/static/Downloads/PyQt4/PyQt-x11-gpl-$PYQTVER.tar.gz
  fi
  if [ -z $QTDIR ]; then
    QTDIR=`pwd`/Qt-${QTVER}
  else
    echo "Using user specified Qt directory $QTDIR"
  fi
  tar zxvf sip-$SIPVER.tar.gz
  PATH=$QTDIR/bin:$PATH
  export QTDIR
  cd sip-$SIPVER
  `pwd`/../ccp4mg/pythondist/bin/python configure.py
  make
  make install
  cd -
  tar zxvf PyQt-x11-gpl-$PYQTVER.tar.gz
  cd PyQt-x11-gpl-$PYQTVER
  `pwd`/../ccp4mg/pythondist/bin/python configure.py --confirm-license
  make
  make install
  cd -
fi

if [ x"$mg_flag" = x"yes" ]; then
  # Python *must* have been installed into ccp4mg/pythondist before we can continue
  # I need a check for this.
  # BUILD CCP4MG
  if [ -z $QTDIR ]; then
    QTDIR=`pwd`/Qt-${QTVER}
    export QTDIR
  else
    echo "Using user specified Qt directory $QTDIR"
  fi
  mkdir -p ccp4mg
  if [ x"$mgcvs_flag" = x"yes" ]; then
    true
  else
    rm -f ccp4mg-latest_nightly_source.tar.gz
    wget -O ccp4mg-latest_nightly_source.tar.gz http://www.ysbl.york.ac.uk/~ccp4mg/nightly/latest_nightly_source.php
    MGDIRNAME=`tar ztf ccp4mg-latest_nightly_source.tar.gz | head -1| awk -F'/' '{print $1}'`
    ln -s ccp4mg $MGDIRNAME
    tar zxf ccp4mg-latest_nightly_source.tar.gz 
  fi
  cd ccp4mg
  if [ -r clipper ]; then
    true
  else
    ln -s ../ccp4-$CCP4VER clipper
  fi
  if [ x"$mgcvs_flag" = x"yes" ]; then
    CVS_RSH=ssh CVSROOT=mgdev@cvs.ccp4.ac.uk:/public/ccp4mg/MG_CVS cvs -z9 co -r Rel_2_B .
  fi
  PATH=`pwd`/../ccp4mg/pythondist/bin:$QTDIR/bin:$PATH ./configure
  make
  if [ -r data/monomer_library ]; then
    true
  else
    cp -a clipper/lib/data/monomers data/monomer_library
  fi
  mkdir -p ccp4_suite/lib/data
  cp clipper/lib/data/syminfo.lib ccp4_suite/lib/data
  cp clipper/lib/data/symop.lib ccp4_suite/lib/data
  cd -
fi

if [ x"$pisa_flag" = x"yes" ]; then
  rm -f ccp4mg-2.5.1-pisa-patch.tar.gz
  wget ./MG//ccp4mg-2.5.1-pisa-patch.tar.gz
  if [ -d ccp4-$CCP4VER ]; then
    echo "OK"
    cd ccp4-$CCP4VER/src/pisa
    tar zxvf ../../../ccp4mg-2.5.1-pisa-patch.tar.gz
    make pisa
    cd -
    mkdir -p ccp4mg/ccp4_suite/bin
    cp ccp4-$CCP4VER/src/pisa/pisa ccp4mg/ccp4_suite/bin/
    mkdir -p ccp4mg/ccp4_suite/share/pisa
    if [ -d ccp4mg/ccp4_suite/share/pisa/molref ]; then
      true
    else
      cp -a ccp4-$CCP4VER/src/pisa/molref ccp4mg/ccp4_suite/share/pisa
    fi
    if [ -d ccp4mg/ccp4_suite/share/pisa/pisastore ]; then
      true
    else
      cp -a ccp4-$CCP4VER/src/pisa/pisastore ccp4mg/ccp4_suite/share/pisa
     fi
    cp -a ccp4-$CCP4VER/src/pisa/pisa.cfg ccp4mg/ccp4_suite/share/pisa
    cp -a ccp4-$CCP4VER/src/pisa/pisa.cfg.in ccp4mg/ccp4_suite/share/pisa
    mkdir -p ccp4mg/ccp4_suite/share/sbase
    if [ -d ccp4mg/ccp4_suite/share/sbase/graph.sbase ]; then
      true
    else
      cp -a ccp4-$CCP4VER/src/pisa/sbase/graph.sbase ccp4mg/ccp4_suite/share/sbase
    fi
    if [ -d ccp4mg/ccp4_suite/share/sbase/index.sbase ]; then
      true
    else
      cp -a ccp4-$CCP4VER/src/pisa/sbase/index.sbase ccp4mg/ccp4_suite/share/sbase
    fi
    if [ -d ccp4mg/ccp4_suite/share/sbase/struct.sbase ]; then
      true
    else
      cp -a ccp4-$CCP4VER/src/pisa/sbase/struct.sbase ccp4mg/ccp4_suite/share/sbase
    fi
    find ccp4mg/ccp4_suite/ -name Makefile\* | xargs rm
  else
    echo "You need to have built CCP4 version ${CCP4VER} in this directory first."
    exit 1
  fi
fi

if [ x"$magick_flag" = x"yes" ]; then
  if [ -r /etc/debian_version ]; then
    mkdir -p ccp4mg/imagemagick
    cd ccp4mg/imagemagick
    for i in `dpkg -L liblqr-1-0 libmagickcore3 libmagickwand3 imagemagick | grep ^/usr`; do
      if [ ! -d $i ]; then
        FILES="$FILES $i"
      fi;
    done

    tar cf - $FILES | tar -x --strip-components 1 -f -
    cd -
  fi
  if [ -r /etc/redhat-release ]; then
   rm -f ImageMagick-6.7.4-0.tar.gz
   wget ftp://ftp.nluug.nl/pub/ImageMagick/ImageMagick-6.7.4-0.tar.gz
   tar zxvf ImageMagick-6.7.4-0.tar.gz
   cd ImageMagick-6.7.4-0/
   ./configure --prefix=`pwd`/../ccp4mg/imagemagick --enable-static=yes --enable-shared=no
   make
   make install
   cd -
  fi
fi

if [ x"$pixie_flag" = x"yes" ]; then
  echo "Build pixie"
  rm -f Pixie-src-2.2.6.tgz
  wget http://sourceforge.net/projects/pixie/files/pixie/Pixie%202.2.6/Pixie-src-2.2.6.tgz
  tar zxf Pixie-src-2.2.6.tgz
  cd Pixie
  ./configure --prefix=`pwd`/../ccp4mg/pixie/ --enable-selfcontained
  make
  make install 
  cd -
  cd ccp4mg/pixie/shaders
  for i in *.sl; do
    ../bin/sdrc $i
  done
  cd -
fi

if [ x"$ffmpeg_flag" = x"yes" ]; then
  rm -f ffmpeg-snapshot.tar.bz2 
  wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
  tar jxf ffmpeg-snapshot.tar.bz2
  cd ffmpeg
  ./configure --prefix=`pwd`/../ccp4mg/ffmpeg/
  make
  make install
  cd -
fi

if [ x"$distrib_flag" = x"yes" ]; then
  if [ -z $QTDIR ]; then
    QTDIR=`pwd`/Qt-${QTVER}
    export QTDIR
  else
    echo "Using user specified Qt directory $QTDIR"
  fi
  cd ccp4mg
  if [ x`uname -m` = x"x86_64" ]; then
    cp -a clipper/lib/libclipper*so* lib/
    cp -a clipper/lib/lib*fftw*so* lib/
    cp -a clipper/lib/lib*ccp4*so* lib/
    cp -a clipper/lib/libssm.so.0* lib/
  fi
  cp -a ${QTDIR}/lib/libQt*.so* lib/
  cp -a ${QTDIR}/lib/libphonon*.so* lib/
  rm -fr QtPlugins
  cp -a ${QTDIR}/plugins QtPlugins
  #cp /usr/lib/lib{png,gif,ungif,tiff,jpeg,z}.so.* lib/
  make distrib
  cd -
fi
