#!/bin/bash
shopt -s expand_aliases
set -x
origin="$PWD"
script_location="$origin/experimental/emiddleton/covidash"
identifying_string="CONFIG PROVIDED FOR COVIDASH"

echo "Creating tmp directory..."
tmp_dir=$(mktemp -d -t covidash-XXXXXX)
echo "$tmp_dir"


echo "-----------------------"
echo "Update apt"
echo "-----------------------"
echo 
#update yoself
sudo apt-get update -qq > /dev/null

#install libglade2-0
sudo apt-get install libglade2-0 -qq > /dev/null

#install vim
sudo apt-get install vim -qq > /dev/null

#install tmux 2.3
echo "-----------------------"
echo "Installing tmux 2.3 ..."
echo "-----------------------"
echo 
sudo apt-get install -y libevent-dev libncurses-dev make -qq > /dev/null
cd $tmp_dir
wget https://github.com/tmux/tmux/releases/download/2.3/tmux-2.3.tar.gz
tar xvzf tmux-2.3.tar.gz > /dev/null
cd tmux-2.3/
./configure > tmux_configure.log
make > tmux_make.log
sudo make install > tmux_make_install.log
cd $origin

#setup tmux.conf
echo "-----------------------"
echo "Setup tmux.conf"
echo "-----------------------"
echo 
if [ grep -Fq "$identifying_string" ~/.tmux.conf ]; then
echo "tmux.conf looks familiar, NOT COPYING FILE!"
else
cp $script_location/tmux.conf ~/.tmux.conf
fi

#get pathogen (wow, how ridiculously ironic)
echo "-----------------------"
echo "Setup pathogen"
echo "-----------------------"
echo 
mkdir -p ~/.vim
mkdir -p ~/.vim/autoload
cd $tmp_dir
wget https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim
#copy it over, don't overwrite if it exists. We're assuming no one is messing with this pathogen file
cp -n pathogen.vim ~/.vim/autoload
cd $origin

#install darcula theme
echo "-----------------------"
echo "Install themes"
echo "-----------------------"
echo 
cd $tmp_dir
git clone https://github.com/blueshirts/darcula.git
mkdir -p ~/.vim/colors
cp -n darcula/colors/darcula.vim ~/.vim/colors/
cd $origin

#install nerdtree
echo "-----------------------"
echo "Install nerdtree"
echo "-----------------------"
echo 
cd $tmp_dir
mkdir -p ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git
cp -an nerdtree ~/.vim/bundle/nerdtree
cd $origin

#setup vimrc
echo "-----------------------"
echo "Install vimrc"
echo "-----------------------"
echo 
if [ grep -Fq "$identifying_strin" ~/.vimrc ]; then
echo "vimrc looks familiar, NOT COPYING FILE!"
else
cp $script_location/vimrc ~/.vimrc
fi

#install tmuxp and setup venv
echo "-----------------------"
echo "Setup virtualenv"
echo "-----------------------"
echo 
cd $tmp_dir
pip3.6 install virtualenv --user > virtualenv_install.log
#this one needs sudo. I don't know why, but it is true and I burned an hour getting to this point
sudo pip3 uninstall virtualenvwrapper
sudo pip3 install virtualenvwrapper > virtualenvwrapper_install.log

if [ grep -Fq "virtualenvwrapper.sh" ~/.bashrc ]; then
#bash was complaining about inverting the logic here. I am unwilling to dump time into figuring out why
echo ""
else
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6" >> ~/.bashrc
echo "export WORKON_HOME=~/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
fi

#add an alias to bashrc
if [ grep -Fq "covidash" ~/.bashrc ]; then
#bash was complaining about inverting the logic here. I am unwilling to dump time into figuring out why
echo ""
else
echo "alias covidash=\"tmuxp load ~/covidash_config.yaml\"" >> ~/.bashrc
fi
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh > venv_wrapper_source.log
mkvirtualenv covidash -p python3.6 > covidash_venv.log
sudo pip3.6 install tmuxp > tmuxp_install.log

#install some zoox tools of greatness
sudo pip3.6 install --index-url https://pypi.zooxlabs.com/simple/ powbox canassist zrpath


#finally, and crucially: setup a config file for tmux
#THIS IS CONFIGURABLE
#i expect we will have several versions of this, pretty much every tester demands something different
echo "-----------------------"
echo "Setup tmux config"
echo "-----------------------"
echo 
cp -n $script_location/covidash_config.yaml ~/covidash_config.yaml 

#deactivate the venv setup earlier
deactivate > deactivate.log

echo "DONE!"
echo "PLEASE SOURCE ~/.BASHRC IN ORDER TO USE COVIDASH!"
