#!/bin/bash

set -x

install_jupyter=false

build_vim() {
  cd /tmp
  git clone http://luajit.org/git/luajit-2.0.git
  cd luajit-2.0
  make
  sudo make install

  cd /tmp
  git clone https://github.com/vim/vim.git
  cd vim
  ./configure \
  --with-features=huge \
  --enable-cscope \
  --enable-pythoninterp \
  --enable-luainterp \
  --enable-multibyte \
  --enable-fontset \
  --disable-gui \
  --without-x \
  --disable-netbeans \
  --enable-largefile
  make
  sudo make install

  if [ -e /usr/bin/vi ]; then
    sudo rm /usr/bin/vi
  fi
  sudo ln -s /usr/local/bin/vim /usr/bin/vi
  rm -rf /tmp/vim
}

provision_packages() {
  sudo yum groupinstall -y "Development Tools"
  sudo yum install -y \
  tmux \
  wget \
  htop \
  mlocate \
  git \
  rake \
  zsh \
  jq \
  at \
  bind-utils \
  strace \
  lua \
  lua-devel \
  ncurses \
  ncurses-devel \
  gmp \
  gmp-devel \
  ctags \
  tcl-devel \
  perl \
  perl-devel \
  perl-ExtUtils-ParseXS \
  perl-ExtUtils-CBuilder \
  perl-ExtUtils-Embed
  wget https://bootstrap.pypa.io/get-pip.py
  sudo python2.7 ./get-pip.py
  sudo env "PATH=$PATH" pip install awscli
  cd ~
  wget https://raw.githubusercontent.com/o0beaner/dotfiles/master/install.sh
  chmod +x install.sh
  ./install.sh
  sudo chmod 644 /usr/bin/chsh
  sudo chmod +x /usr/bin/chsh
  sudo /usr/bin/chsh -s /bin/zsh $USER
  sudo updatedb
  cd $util_path
  wget --no-check-certificate $s3_utils/suntracker.sh
  chmod +x $util_path/suntracker.sh
  (crontab -l ; echo "0 3 * * * $util_path/suntracker.sh") | crontab -
  $util_path/suntracker.sh
  touch ~/.zsh.prompts
  mkdir ~/.zsh.after/
  echo "prompt agnoster" > ~/.zsh.after/prompt.zsh
}

install_ssm() {
  cd /tmp
  sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
}

s3ify_zeppelin() {
  cd $util_path
  wget --no-check-certificate $s3_utils/configure_zeppelin_s3.sh
  chmod +x $util_path/configure_zeppelin_s3.sh
  aws emr add-steps --cluster-id $cluster_id --steps Type=CUSTOM_JAR,Name="Configure Zeppelin for S3",Jar="command-runner.jar",Args=[$util_path/configure_zeppelin_s3.sh]
}

install_jupyter() {
  cd $util_path
  wget --no-check-certificate https://s3.amazonaws.com/aws-bigdata-blog/artifacts/aws-blog-emr-jupyter/install-jupyter-emr5.sh
  chmod +x $util_path/install-jupyter-emr5.sh
  $util_path/install-jupyter-emr5.sh \
  --r \
  --julia \
  --toree \
  --torch \
  --ruby \
  --ds-packages \
  --ml-packages \
  --python-packages ggplot nilearn \
  --port 8002 \
  --password jupyter \
  --jupyterhub \
  --jupyterhub-port 8001 \
  --cached-install \
  --notebook-dir s3://ty-emr/XRR/jupyter/notebooks/ \
  --copy-samples \
  --s3fs
}

# get input parameters
while [ $# -gt 0 ]; do
    case "$1" in
    --jupyter)
      install_jupyter=true
      ;;
    -*)
      error_msg "unrecognized option: $1"
      ;;
    *)
      break;
      ;;
    esac
    shift
done

s3_utils='https://s3.amazonaws.com/ty-emr/XRR/utils'
build_vim=false
util_path="~/.utils"

mkdir -p $util_path

provision_packages &
build_vim &
install_ssm &

is_master=false
if grep isMaster /mnt/var/lib/info/instance.json | grep true;
then
  s3ify_zeppelin &
  if [ "$install_jupyter" == true ]; then
    install_jupyter &
  fi
fi
