#!/usr/bin/env python

import os
import time
import shutil

# Assuming we are in dotfiles/bin.
script_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
home_dir   = os.path.expanduser('~')
script_name = os.path.basename(__file__)

##################
# Oh My Fish fix #
##################

# oh my fish for some reason on install moves the fish config file and puts in a default one. WHY!
# Move the created .config to a backup.
config_dir = os.path.join(home_dir, '.config')
new_config_dir = config_dir + str(round(time.time() * 1000)) + ".bak"
try:
    os.rename(config_dir, new_config_dir)
except:
    pass

# Install fish shell pkg management
os.system('curl -L https://github.com/oh-my-fish/oh-my-fish/raw/master/bin/install | fish')

# Delete the newly created config dir.
shutil.rmtree(config_dir, ignore_errors=True)

#################################
# Create symlinks to this repo. #
#################################

links_to_make = [f for f in os.listdir(script_dir) if not f[0] == '.']

target = [os.path.join(home_dir, '.' + f) for f in links_to_make]
source = [os.path.join(script_dir, f) for f in links_to_make]

for t, s in zip(target, source):
    try:
        os.symlink(os.path.relpath(s, home_dir), t)
        print(os.path.relpath(s, home_dir), '->', t)
    except:
        pass

# Install necessary tools.
print("Installing tools")

# Tmux pkg manager
os.system('git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm')

# Install emacs pkg management
os.system('curl -fsSL https://raw.githubusercontent.com/cask/cask/master/go | python')

# Install vim pkg management
os.system('curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim')
os.system('curl -fLo ~/.config/nvim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim')

# Install base16 themes for fish
os.system('git clone https://github.com/chriskempson/base16-shell.git ~/.config/base16-shell')

# Install fzf - with fish integration...
os.system('git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf')
os.system('~/.fzf/install')
os.system('curl -Lo ~/.config/fish/functions/fisher.fish --create-dirs git.io/fisher')

# TODO doesn't appear to work correctly.
os.system('fish -c fisher fzf')
