#!/bin/bash

set -x

TD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)"

U="user"

export HOME=/home/user
export USER=user
export LC_ALL=en_US
export LANG=en_US.UTF-8

# original home
OH="/hosthome"
# link ~/.home to original home
ln -s "/hosthome" $HOME/.home

cd $HOME && ls -tla

# /nix is already created for us and chowned to us by now:
curl -L https://nixos.org/nix/install | sh

source /home/user/.nix-profile/etc/profile.d/nix.sh

# this should install all the packages from the config.nix:
nix-env -iA nixpkgs.work
nix-env --set-flag priority 7 workpackages

# FIXME have this pull from synced dir instead
# oh-my-zsh
curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh > /tmp/installomz.sh
export ZSH="$HOME/.zsh"
export KEEP_ZSHRC=yes
zsh /tmp/installomz.sh && rm /tmp/installomz.sh

###############################################################################
##### Personal Config
###############################################################################
# I had envisioned this image as adaptable to other users but I've decided
# to abandon that hope and hardcode my own values in here.
git config --global user.email sneak@sneak.berlin
git config --global user.name sneak

mkdir -p ~/.paths
# TODO FIXME change the syncthing paths on las1 when convenient sometime
ln -s ~/.home/Library/Syncthing/folders/sync ~/.paths/sneak-sync
ln -s ~/.home/Library/Syncthing/folders/scratch ~/.paths/sneak-scratch
ln -s ~/.home/Library/Syncthing/folders/sneak-docs ~/.paths/sneak-docs
ln -s ~/.paths/sneak-docs ~/Documents
ln -s ~/.paths/sneak-scratch/dev ~/dev
ln -s ~/.paths/sneak-sync/dotfiles/weechat ~/.weechat
ln -s ~/.paths/sneak-sync/ssh/config ~/.ssh/config

# link in ssh key from original home
mkdir -p ~/.ssh
ln -s ~/.paths/sneak-sync/secrets/ssh/pixel16/id_rsa ~/.ssh/id_rsa
ln -s ~/.paths/sneak-sync/secrets/ssh/pixel16/id_rsa.pub ~/.ssh/id_rsa.pub


# vim plug preinstallation
# newline is to fix "hit ENTER" issue when it can't find the colorscheme file
# which PlugInstall is about to install
# this needs to happen after the linking above
echo "" | vim +PlugInstall +qall
vim +GoInstallBinaries +qall
vim +PlugInstall +qall

rm ~/.vimrc
ln -s ~/.paths/sneak-sync/dotfiles/vim/vimrc ~/.vimrc

mkdir -p ~/.config/nvim
ln -s ~/.paths/sneak-sync/dotfiles/vim/vimrc ~/.config/nvim/init.vim

mkdir -p ~/.local/share/terraform/plugins/registry.terraform.io/dmacvicar/libvirt/0.6.2/linux_amd64
cd ~/.local/share/terraform/plugins/registry.terraform.io/dmacvicar/libvirt/0.6.2/linux_amd64


