<commit_before>#!/usr/bin/env bash

# Usage: setup.sh
#
# Guidelines
# - This script is executed once in a few months, execution speed doesn't matter much. Readability does.
# - Each install_* function should be independent.

#set -x
set -eo pipefail

SETUP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
USERNAME=$SUDO_USER
USERHOME=/home/$USERNAME
export DEBIAN_FRONTEND=noninteractive

function main() {
    
    check_root

    install_basic_tools

    install_oh_my_zh
    install_vim
    install_neovim
    install_git
    install_subversion

    install_java_tools
    install_devops_tools
    install_frontend_tools

    echo Done
}

install_oh_my_zh() {
    echo "Installing oh my zh"
    apt-get-install zsh
    chsh -s /bin/zsh
    if [ ! -d "$USERHOME/.oh-my-zsh" ]; then
        git clone "https://github.com/robbyrussell/oh-my-zsh.git" "$USERHOME/.oh-my-zsh"
    fi
    cp "$USERHOME/.oh-my-zsh/templates/zshrc.zsh-template" "$USERHOME/.zshrc"
    install_powerline_fonts
    # install powerlevel9k theme
    if [ ! -d "$USERHOME/.oh-my-zsh/custom/themes/powerlevel9k" ]; then
        git clone "https://github.com/bhilburn/powerlevel9k.git" "$USERHOME/.oh-my-zsh/custom/themes/powerlevel9k"
    fi
    # configure oh-my-zsh
    sed -i "1iPOWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(virtualenv vcs background_jobs)" "$USERHOME/.zshrc"
    sed -i "1iPOWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir)" "$USERHOME/.zshrc"
    sed -i "1iDEFAULT_USER=$USERNAME" "$USERHOME/.zshrc"
    sed -i "1iexport TERM=xterm-256color" "$USERHOME/.zshrc"
    sed -i "/^ZSH_THEME=.*/c\ZSH_THEME=powerlevel9k\/powerlevel9k" "$USERHOME/.zshrc"
    chown -R $USERNAME:$USERNAME "$USERHOME/.oh-my-zsh"
    chown $USERNAME:$USERNAME "$USERHOME/.zshrc"
}

install_subversion() {
    echo "Installing subversion"
    apt-get-install subversion
    make_symlink "$SETUP_HOME/home/.subversion/config" "$USERHOME/.subversion/config"
    chown $USERNAME:$USERNAME "$USERHOME/.subversion"
    # setup merge script
    make_symlink "$SETUP_HOME/bin/mysvnmerge" /usr/local/bin/mysvnmerge
    make_symlink "$SETUP_HOME/bin/mysvndiff" /usr/local/bin/mysvndiff
}

install_git() {
    echo "Installing git"
    # git is already installed, just configure
    # setup .gitconfig
    make_symlink "$SETUP_HOME/home/.gitconfig" "$USERHOME/.gitconfig"
    chown $USERNAME:$USERNAME "$USERHOME/.gitconfig"
    # setup merge script
    make_symlink "$SETUP_HOME/bin/mygitmerge" /usr/local/bin/mygitmerge
    apt-get-install tig
}

install_vim() {
    echo "Installing vim"
    # vim is already installed, just configure

    # install dein plugin manager
    local dein_dir="$USERHOME/.vim/dein/repos/github.com/Shougo/dein.vim"
    if [ ! -d "$dein_dir" ]; then
        mkdir -p "$dein_dir"
        git clone "https://github.com/Shougo/dein.vim.git"  "$dein_dir"
        chown -R $USERNAME:$USERNAME "$USERHOME/.vim"
    fi
    make_symlink "$SETUP_HOME/home/.vimrc" "$USERHOME/.vimrc"
    chown -h $USERNAME:$USERNAME "$USERHOME/.vimrc"
    echo "installing fonts"
    install_powerline_fonts
}

install_neovim() {
    echo "Installing neovim"
    add-apt-repository -y ppa:neovim-ppa/unstable
    # xsel enables system clipboard
    # python-dev is required to install python support in neovim, which is required by MatchTagAlways plugin
    apt-get-install neovim xsel python-dev
    # add puthon support in neovim plugins
    pip2 install neovim

    # install dein plugin manager
    local dein_dir="$USERHOME/.config/nvim/dein/repos/github.com/Shougo/dein.vim"
    if [ ! -d "$dein_dir" ]; then
        mkdir -p "$dein_dir"
        git clone "https://github.com/Shougo/dein.vim.git"  "$dein_dir"
        chown -R $USERNAME:$USERNAME "$USERHOME/.config/nvim"
    fi
    make_symlink "$SETUP_HOME/home/.config/nvim/init.vim" "$USERHOME/.config/nvim/init.vim"
    chown -h $USERNAME:$USERNAME "$USERHOME/.config/nvim/init.vim"
    echo "installing fonts"
    install_powerline_fonts
}

install_powerline_fonts() {
    # install powerline fonts
    local font_dir="$USERHOME/.local/share/fonts"
    mkdir -p $font_dir
    cd $font_dir
    # install font DejaVu Sans Mono
    wget -q --timestamping --progress=bar:force:noscroll "https://github.com/powerline/fonts/raw/master/DejaVuSansMono/DejaVu%20Sans%20Mono%20Bold%20Oblique%20for%20Powerline.ttf"
    wget -q --timestamping --progress=bar:force:noscroll "https://github.com/powerline/fonts/raw/master/DejaVuSansMono/DejaVu%20Sans%20Mono%20Bold%20for%20Powerline.ttf"
    wget -q --timestamping --progress=bar:force:noscroll "https://github.com/powerline/fonts/raw/master/DejaVuSansMono/DejaVu%20Sans%20Mono%20Oblique%20for%20Powerline.ttf"
    wget -q --timestamping --progress=bar:force:noscroll "https://github.com/powerline/fonts/raw/master/DejaVuSansMono/DejaVu%20Sans%20Mono%20for%20Powerline.ttf"
    chown -R $USERNAME:$USERNAME "$USERHOME/.local"
    fc-cache -vf $font_dir
}

install_basic_tools() {
    echo "Installing basic tools"
    apt-get-install tree python-pip python3-pip sshfs cifs-utils htop
    make_symlink "$SETUP_HOME/bin/f" "/usr/local/bin/f"
    make_symlink "$SETUP_HOME/bin/pack" "/usr/local/bin/pack"
    make_symlink "$SETUP_HOME/bin/unpack" "/usr/local/bin/unpack"
}

install_devops_tools() {
    install_ansible
    install_vagrant
    install_packer
    install_docker
}

install_java_tools() {
    echo "Installing java tools"
    install_oracle_java 8
    if ! program_exists ant; then
        apt-get-install ant ant-contrib maven
    fi
}

install_oracle_java() {
    echo "Installing java $1"
    apt-add-repository ppa:webupd8team/java -y
    echo oracle-java$1-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
    apt-get-install oracle-java$1-installer oracle-java$1-set-default
}

install_packer() {
    echo "Installing packer"
    if ! program_exists packer; then
        local PACKER_VERSION="0.10.0"
        wget --timestamping --progress=bar:force:noscroll --directory-prefix="/usr/local/bin/" "https://releases.hashicorp.com/packer/$PACKER_VERSION/packer_${PACKER_VERSION}_linux_amd64.zip"
        # -o tells unzip to overwrite existing files
        unzip -o "/usr/local/bin/packer_${PACKER_VERSION}_linux_amd64.zip" -d "/usr/local/bin/"
        rm "/usr/local/bin/packer_${PACKER_VERSION}_linux_amd64.zip"
    fi
}

install_ansible() {
    echo "Installing ansible"
    if ! program_exists ansible; then
        apt-add-repository ppa:ansible/ansible -y
        apt-get-install ansible
        ! grep --quiet "export ANSIBLE_NOCOWS" && echo "export ANSIBLE_NOCOWS=1" >> "$USERHOME/.bashrc"
    fi
}

install_vagrant() {
    echo "Installing vagrant"
    if ! program_exists vagrant; then
        apt-get-install vagrant
    fi
}

install_docker() {
    echo "Installing docker-engine"
    # install docker-engine 
    if ! program_exists docker; then
        apt-key adv --keyserver "hkp://p80.pool.sks-keyservers.net:80" --recv-keys <KEY>
        echo "deb https://apt.dockerproject.org/repo ubuntu-$(lsb_release -cs) main" > "/etc/apt/sources.list.d/docker.list"
        apt-get-install linux-image-extra-$(uname -r) docker-engine<commit_msg>Fix ansible installation<commit_after>#!/usr/bin/env bash

# Usage: setup.sh
#
# Guidelines
# - This script is executed once in a few months, execution speed doesn't matter much. Readability does.
# - Each install_* function should be independent.

#set -x
set -eo pipefail

SETUP_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
USERNAME=$SUDO_USER
USERHOME=/home/$USERNAME
export DEBIAN_FRONTEND=noninteractive

function main() {
    
    check_root

    install_basic_tools

    install_oh_my_zh
    install_vim
    install_neovim
    install_git
    install_subversion

    install_java_tools
    install_devops_tools
    install_frontend_tools

    echo Done
}

install_oh_my_zh() {
    echo "Installing oh my zh"
    apt-get-install zsh
    chsh -s /bin/zsh
    if [ ! -d "$USERHOME/.oh-my-zsh" ]; then
        git clone "https://github.com/robbyrussell/oh-my-zsh.git" "$USERHOME/.oh-my-zsh"
    fi
    cp "$USERHOME/.oh-my-zsh/templates/zshrc.zsh-template" "$USERHOME/.zshrc"
    install_powerline_fonts
    # install powerlevel9k theme
    if [ ! -d "$USERHOME/.oh-my-zsh/custom/themes/powerlevel9k" ]; then
        git clone "https://github.com/bhilburn/powerlevel9k.git" "$USERHOME/.oh-my-zsh/custom/themes/powerlevel9k"
    fi
    # configure oh-my-zsh
    sed -i "1iPOWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(virtualenv vcs background_jobs)" "$USERHOME/.zshrc"
    sed -i "1iPOWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir)" "$USERHOME/.zshrc"
    sed -i "1iDEFAULT_USER=$USERNAME" "$USERHOME/.zshrc"
    sed -i "1iexport TERM=xterm-256color" "$USERHOME/.zshrc"
    sed -i "/^ZSH_THEME=.*/c\ZSH_THEME=powerlevel9k\/powerlevel9k" "$USERHOME/.zshrc"
    chown -R $USERNAME:$USERNAME "$USERHOME/.oh-my-zsh"
    chown $USERNAME:$USERNAME "$USERHOME/.zshrc"
}

install_subversion() {
    echo "Installing subversion"
    apt-get-install subversion
    make_symlink "$SETUP_HOME/home/.subversion/config" "$USERHOME/.subversion/config"
    chown $USERNAME:$USERNAME "$USERHOME/.subversion"
    # setup merge script
    make_symlink "$SETUP_HOME/bin/mysvnmerge" /usr/local/bin/mysvnmerge
    make_symlink "$SETUP_HOME/bin/mysvndiff" /usr/local/bin/mysvndiff
}

install_git() {
    echo "Installing git"
    # git is already installed, just configure
    # setup .gitconfig
    make_symlink "$SETUP_HOME/home/.gitconfig" "$USERHOME/.gitconfig"
    chown $USERNAME:$USERNAME "$USERHOME/.gitconfig"
    # setup merge script
    make_symlink "$SETUP_HOME/bin/mygitmerge" /usr/local/bin/mygitmerge
    apt-get-install tig
}

install_vim() {
    echo "Installing vim"
    # vim is already installed, just configure

    # install dein plugin manager
    local dein_dir="$USERHOME/.vim/dein/repos/github.com/Shougo/dein.vim"
    if [ ! -d "$dein_dir" ]; then
        mkdir -p "$dein_dir"
        git clone "https://github.com/Shougo/dein.vim.git"  "$dein_dir"
        chown -R $USERNAME:$USERNAME "$USERHOME/.vim"
    fi
    make_symlink "$SETUP_HOME/home/.vimrc" "$USERHOME/.vimrc"
    chown -h $USERNAME:$USERNAME "$USERHOME/.vimrc"
    echo "installing fonts"
    install_powerline_fonts
}

install_neovim() {
    echo "Installing neovim"
    add-apt-repository -y ppa:neovim-ppa/unstable
    # xsel enables system clipboard
    # python-dev is required to install python support in neovim, which is required by MatchTagAlways plugin
    apt-get-install neovim xsel python-dev
    # add puthon support in neovim plugins
    pip2 install neovim

    # install dein plugin manager
    local dein_dir="$USERHOME/.config/nvim/dein/repos/github.com/Shougo/dein.vim"
    if [ ! -d "$dein_dir" ]; then
        mkdir -p "$dein_dir"
        git clone "https://github.com/Shougo/dein.vim.git"  "$dein_dir"
        chown -R $USERNAME:$USERNAME "$USERHOME/.config/nvim"
    fi
    make_symlink "$SETUP_HOME/home/.config/nvim/init.vim" "$USERHOME/.config/nvim/init.vim"
    chown -h $USERNAME:$USERNAME "$USERHOME/.config/nvim/init.vim"
    echo "installing fonts"
    install_powerline_fonts
}

install_powerline_fonts() {
    # install powerline fonts
    local font_dir="$USERHOME/.local/share/fonts"
    mkdir -p $font_dir
    cd $font_dir
    # install font DejaVu Sans Mono
    wget -q --timestamping --progress=bar:force:noscroll "https://github.com/powerline/fonts/raw/master/DejaVuSansMono/DejaVu%20Sans%20Mono%20Bold%20Oblique%20for%20Powerline.ttf"
    wget -q --timestamping --progress=bar:force:noscroll "https://github.com/powerline/fonts/raw/master/DejaVuSansMono/DejaVu%20Sans%20Mono%20Bold%20for%20Powerline.ttf"
    wget -q --timestamping --progress=bar:force:noscroll "https://github.com/powerline/fonts/raw/master/DejaVuSansMono/DejaVu%20Sans%20Mono%20Oblique%20for%20Powerline.ttf"
    wget -q --timestamping --progress=bar:force:noscroll "https://github.com/powerline/fonts/raw/master/DejaVuSansMono/DejaVu%20Sans%20Mono%20for%20Powerline.ttf"
    chown -R $USERNAME:$USERNAME "$USERHOME/.local"
    fc-cache -vf $font_dir
}

install_basic_tools() {
    echo "Installing basic tools"
    apt-get-install tree python-pip python3-pip sshfs cifs-utils htop
    make_symlink "$SETUP_HOME/bin/f" "/usr/local/bin/f"
    make_symlink "$SETUP_HOME/bin/pack" "/usr/local/bin/pack"
    make_symlink "$SETUP_HOME/bin/unpack" "/usr/local/bin/unpack"
}

install_devops_tools() {
    install_ansible
    install_vagrant
    install_packer
    install_docker
}

install_java_tools() {
    echo "Installing java tools"
    install_oracle_java 8
    if ! program_exists ant; then
        apt-get-install ant ant-contrib maven
    fi
}

install_oracle_java() {
    echo "Installing java $1"
    apt-add-repository ppa:webupd8team/java -y
    echo oracle-java$1-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
    apt-get-install oracle-java$1-installer oracle-java$1-set-default
}

install_packer() {
    echo "Installing packer"
    if ! program_exists packer; then
        local PACKER_VERSION="0.10.0"
        wget --timestamping --progress=bar:force:noscroll --directory-prefix="/usr/local/bin/" "https://releases.hashicorp.com/packer/$PACKER_VERSION/packer_${PACKER_VERSION}_linux_amd64.zip"
        # -o tells unzip to overwrite existing files
        unzip -o "/usr/local/bin/packer_${PACKER_VERSION}_linux_amd64.zip" -d "/usr/local/bin/"
        rm "/usr/local/bin/packer_${PACKER_VERSION}_linux_amd64.zip"
    fi
}

install_ansible() {
    echo "Installing ansible"
    if ! program_exists ansible; then
        apt-add-repository ppa:ansible/ansible -y
        apt-get-install ansible
        ! grep --quiet "export ANSIBLE_NOCOWS" "$USERHOME/.bashrc" && echo "export ANSIBLE_NOCOWS=1" >> "$USERHOME/.bashrc"
    fi
}

install_vagrant() {
    echo "Installing vagrant"
    if ! program_exists vagrant; then
        apt-get-install vagrant
    fi
}

install_docker() {
    echo "Installing docker-engine"
    # install docker-engine 
    if ! program_exists docker; then
        apt-key adv --keyserver "hkp://p80.pool.sks-keyservers.net:80" --recv-keys <KEY>
        echo "deb https://apt.dockerproject.org/repo ubuntu-$(lsb_release -cs) main" > "/etc/apt/sources.list.d/docker.list"
        apt-get-install linux-image-extra-$(uname -r) docker-engine
{'fraction_non_alphanumeric': 0.12624469048116427, 'fraction_numerical': 0.011280551493628577, 'mean_word_length': 3.454714640198511, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 20, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '993561', 'n_tokens_mistral': 5408, 'n_tokens_neox': 5146, 'n_words': 958}