#!/bin/bash

if [ ! -e /etc/debian_version ]; then
  echo "本批次安裝檔目前僅適用 Debian 或 Ubuntu 系列，敬請見諒!!"
  exit 1
fi

#####################################################################
# 以下為基本程式設計與生資相關自由軟體
#####################################################################
########
# 安裝基本程式語言編譯環境
########
sudo apt-get -y install gcc g++ make autoconf build-essential
########
# 安裝 JDK 6 編譯環境
########
sudo apt-get -y install sun-java6-jdk
########
# 安裝 GNU R 相關套件
# (去除 rjava 與 rmpi 套件, 因為 rmpi 必須特別設定成使用 mpich2 )
########
sudo apt-get -y install r-base
sudo apt-get -y install `apt-cache search --names-only "^r-cran" | awk '{ print $1 }' | grep -v "r-cran-rjava" | grep -v "r-cran-rmpi" `
########
# 安裝 GNU R BioConductor 套件
########
if [ ! -e biocLite.R ]; then
  sudo wget http://www.bioconductor.org/biocLite.R
fi
if [ ! -e BioConductor.R ]; then
  cat > BioConductor.R << EOF
source(file="$(pwd)/biocLite.R")
biocLite()
EOF
  sudo R --no-save < BioConductor.R
fi
########
# 安裝 GNU R ape 套件
########
sudo apt-get -f install libc6-dev g77 gfortran liblapack-dev
if [ ! -e gee_4.13-13.tar.gz ]; then
  wget http://cran.r-project.org/src/contrib/gee_4.13-13.tar.gz
  sudo R CMD INSTALL gee_4.13-13.tar.gz
fi
if [ ! -e nlme_3.1-92.tar.gz ]; then
  wget http://cran.r-project.org/src/contrib/nlme_3.1-92.tar.gz
  sudo R CMD INSTALL nlme_3.1-92.tar.gz
fi
if [ ! -e lattice_0.17-25.tar.gz ]; then
  wget http://cran.r-project.org/src/contrib/lattice_0.17-25.tar.gz
  sudo R CMD INSTALL lattice_0.17-25.tar.gz
fi
if [ ! -e ape_2.3-1.tar.gz ]; then
  wget http://cran.r-project.org/src/contrib/ape_2.3-1.tar.gz
  sudo R CMD INSTALL ape_2.3-1.tar.gz
fi
#####################################################################
# 以下為叢集環境的需求
#####################################################################
########
# 安裝 MPICH2
########
if [ ! -e mpich2-1.0.8p1.tar.gz ]; then
  wget http://www.mcs.anl.gov/research/projects/mpich2/downloads/tarballs/1.0.8p1/mpich2-1.0.8p1.tar.gz
  tar zxvf mpich2-1.0.8p1.tar.gz
  cd mpich2-1.0.8p1
  ./configure CFLAGS="-fPIC" CXXFLAGS="-fPIC" FFLAGS="-fPIC" prefix=/opt/mpich2
  sudo make
  sudo make install
  cd ..
fi
########
# 安裝 openPBS / torque
########
if [ ! -e torque-2.3.6.tar.gz ]; then
  wget wget http://www.clusterresources.com/downloads/torque/torque-2.3.6.tar.gz
  tar zxvf torque-2.3.6.tar.gz
  cd torque-2.3.6/
  ./configure --prefix=/opt/torque
  sudo make
  sudo make install
  sudo /opt/torque/sbin/pbs_server -t create
  echo "$(hostname)" > nodes
  sudo mv nodes /var/spool/torque/server_priv/nodes
  echo "$(hostname)" > server_name
  sudo mv server_name /var/spool/torque/server_name
  cat > torque_conf << EOF
create queue batch
set queue batch queue_type = Execution
set queue batch started = True
set queue batch enabled = True
set server default_queue = batch
set server resources_default.nodes = 1
set server scheduling = True
EOF
  sudo /opt/torque/bin/qmgr < torque_conf
  cat >> config << EOF
\$pbsserver    $(hostname)
\$logevent     255
EOF
  sudo mv config /var/spool/torque/mom_priv/jobs/config
  sudo /opt/torque/bin/qterm -t quick
  sudo /opt/torque/sbin/pbs_server
  sudo /opt/torque/sbin/pbs_sched
  sudo /opt/torque/sbin/pbs_mom
  sudo /opt/torque/bin/pbsnodes -a
  cd ..
fi

########
# 安裝 Lustre
########
if [ ! -e /etc/apt/sources.list.d/lustre.list ]; then
  sudo echo "deb http://www.pdsi-scidac.org/repository/debian testing main" > lustre.list
  sudo mv lustre.list /etc/apt/sources.list.d/lustre.list
  sudo apt-get update
  sudo apt-get -y --force-yes install pdsi-scidac-keyring
  sudo apt-get -y install linux-image-2.6.22.19-lustre-686 lustre-modules-2.6.22.19-lustre-686 lustre-utils 
fi
#####################################################################
# 以下為叢集版本的生資相關軟體
#####################################################################
########
# 安裝 Rmpi
########
if [ ! -e Rmpi_0.5-7.tar.gz ]; then
  wget http://www.stats.uwo.ca/faculty/yu/Rmpi/download/linux/Rmpi_0.5-7.tar.gz
  sudo R CMD INSTALL Rmpi_0.5-7.tar.gz --with-mpi=/opt/mpich2
fi
########
# 安裝 mpiBLAST
# 參考 http://debianclusters.cs.uni.edu/index.php/Installing_mpiBLAST
########
if [ ! -e mpiBLAST-1.5.0-pio.tgz ]; then
  wget http://www.mpiblast.org/downloads/files/mpiBLAST-1.5.0-pio.tgz
  tar zxvf mpiBLAST-1.5.0-pio.tgz
  cd mpiblast-1.5.0-pio
  ./ncbi/make/makedis.csh 2>&1|tee out.makedis.txt
  ./ncbi/make/makedis.csh 2>&1|tee out.makedis.txt
  ./ncbi/make/makedis.csh 2>&1|tee out.makedis.txt
  ./configure --prefix=/opt/mpiblast --with-mpi=/opt/mpich2 --with-ncbi=`pwd`/ncbi --enable-MPI_Alloc_mem
  sudo make
  sudo make install
  cd ..
fi
#####################################################################
# 電腦叢集的網頁監控機制
#####################################################################
########
# 安裝 wwwBLAST
########
if [ ! -e wwwblast-2.2.20-ia32-linux.tar.gz ]; then
  wget ftp://ftp.ncbi.nlm.nih.gov/blast/executables/LATEST/wwwblast-2.2.20-ia32-linux.tar.gz
  tar zxvf wwwblast-2.2.20-ia32-linux.tar.gz
fi
