#!/bin/bash
set -ex
 
install_jupyter=false
 
# get input parameters
while [ $# -gt 0 ]; do
    case "$1" in
    --jupyter)
      install_jupyter=true
      ;;
    -*)
      error_msg "unrecognized option: $1"
      ;;
    *)
      break;
      ;;
    esac
    shift
done
 
platform=`uname`
s3_utils='https://s3.amazonaws.com/ty-emr/XRR/utils'
build_vim=false
 
if [ "$USER" == "root" ]; then
  util_path="/root/.utils"
  home="/root"
else
  if [ "$platform" == "Darwin" ]; then
    users_dir="Users"
  fi
  if [ "$platform" == "Linux" ]; then
    users_dir="home"
  fi
  util_path="/$users_dir/$USER/.utils"
fi
vim_check=`vim --version`
if [[ $vim_check != *"+lua"* ]]; then
  build_vim=true
fi
mkdir -p $util_path
 
release=`cat /etc/*release* | tr '[:upper:]' '[:lower:]'`
if [[ $release != *"smartos"* ]]; then
  if [[ $release == *"red hat"* ]]; then
    sudo yum groupinstall -y "Development Tools"
    sudo yum install -y \
    tmux \
    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
 
    sudo chkconfig atd on
    sudo service atd start
 
    if [[ $build_vim == true ]]; then
      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
 
      sudo rm /usr/bin/vi
      sudo ln -s /usr/local/bin/vim /usr/bin/vi
      rm -rf /tmp/vim
    fi
  fi
  if [[ $release == *"debian"* ]]; then
    sudo apt-get update
    sudo apt-get install -y \
    tmux \
    htop \
    mlocate \
    git \
    rake \
    zsh \
    jq \
    at \
    dnsutils \
    strace \
    libncurses5-dev \
    libncursesw5-dev \
    python-dev \
    ruby-dev \
    lua5.1 \
    lua5.1-dev \
    luajit \
    libluajit-5.1 \
    libperl-dev \
    build-essential
 
    if [[ $build_vim == true ]]; then
      sudo ln -sf /usr/include/lua5.1 /usr/include/lua5.1/include
      sudo ln -sf /usr/lib/x86_64-linux-gnu/liblua5.1.so /usr/local/lib/liblua.so
      cd /tmp
      git clone https://github.com/vim/vim.git
      cd vim
      ./configure \
      --with-features=huge \
      --enable-cscope \
      --enable-pythoninterp=yes \
      --enable-rubyinterp=yes \
      --with-python-config-dir=/usr/lib/python2.7/config-x86_64-linux-gnu \
      --enable-multibyte \
      --enable-fontset \
      --disable-gui \
      --disable-netbeans \
      --enable-luainterp=yes \
      --with-luajit \
      --with-lua-prefix=/usr/include/lua5.1 \
      --enable-largefile
 
      make
      sudo make install
 
      sudo rm /usr/bin/vi
      sudo ln -s /usr/local/bin/vim /usr/bin/vi
      rm -rf /tmp/vim
    fi
  fi
 
  curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python2.7
  sudo pip install awscli
  sh -c "`curl -fksSL https://raw.githubusercontent.com/o0beaner/dotfiles/master/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
else
  BOOTSTRAP_TAR="bootstrap-2017Q1-x86_64.tar.gz"
  curl -Ok https://pkgsrc.joyent.com/packages/SmartOS/bootstrap/${BOOTSTRAP_TAR}
  tar -zxpf ${BOOTSTRAP_TAR} -C /
  rm -f boots*
  PATH=/opt/local/sbin:/opt/local/bin:$PATH
  MANPATH=/opt/local/man:$MANPATH
  pkgin -y in jq tmux git ruby22-rake zsh at || true
  mkdir /usbkey/root
  mv /root/.[!.]* /usbkey/root
  cd /
  rm -rf /root
  ln -s /usbkey/root /root
  sh -c "`curl -fksSL https://raw.githubusercontent.com/o0beaner/dotfiles/smartos/install.sh`"
  echo 'if [ -n "$BASH_EXECUTION_STRING" ]; then' >> ~/.bashrc
  echo '  export SHELL=/opt/local/bin/zsh' >> ~/.bashrc
  echo '    exec "$SHELL" -c "$BASH_EXECUTION_STRING"' >> ~/.bashrc
  echo 'fi' >> ~/.bashrc
  echo 'SHELL=/opt/local/bin/zsh; exec "$SHELL"' >> ~/.bashrc
fi
 
# AWS Instance customization
if [ -e /usr/bin/cloud-init ]; then
 
  # Install SSM Agent
  cd /tmp
  sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
 
  # Am I running EMR?
  instance=`aws ec2 describe-instances --instance-ids $(curl -s 169.254.169.254/latest/meta-data/instance-id)`
  tags=`echo $instance | jq -r '.Reservations[0].Instances[0].Tags[]'`
  cluster_id=`echo $tags | jq -r '. | select(.Key=="aws:elasticmapreduce:job-flow-id") | .Value'`
  if [ -n "$cluster_id" ]; then
    echo "$cluster_id" > ~/.cluster_id
    role=`echo $tags | jq -r '. | select(.Key=="aws:elasticmapreduce:instance-group-role") | .Value'`
 
    if [ "$role" == "MASTER" ]; then
      # ToDo: Incorporate Hue?
 
      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]
    fi
 
    # install jupyter
    if [ $install_jupyter == true ]; then
      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
    fi
  fi
fi
 
touch ~/.zsh.prompts
echo "prompt agnoster" > ~/.zsh.after/prompt.zsh
