#!/usr/bin/env fish

# This script's purpose is to setup the dotfiles configuration for an existing
# and provisioned machine. For provisioning, see `./dotfiles-init`.

set dfp (realpath (dirname (status -f))/../..)
set lock_file $HOME/.using-lytedev-dotfiles.lock

if not test -f $lock_file
	echo This will delete existing files. Make sure you know what you\'re doing.
	read -r -p "Are you sure you want to continue? [y/N] " response
	set response (string lower $response)
	if string match $response y
		echo "agreed" > "$lock_file"
	else
		exit 1
	end
end

set -q XDG_CONFIG_HOME || set XDG_CONFIG_HOME $HOME/.config
set h $HOME; set c $XDG_CONFIG_HOME
if test -d $c/lytedev-env
	echo "Warning: no environment-specific configuration detected!"
end

function l -a dot -a target -d "Symlink a dotfile configuration file or directory"
	if test -L $target || test -f $target || test -d $target
		rm -rf "$target"
	end
	# check if the directory that will contain the link exists
	set -l d (dirname $target)
	test -d $d || mkdir -p $d
	ln -s (pwd)/$dot $target
	echo Linked $dot to $target
end

pushd $dfp
l common/tmux/conf $h/.tmux.conf
l common/fish $c/fish
l common/neovim $c/nvim
l common/colors/vim $c/nvim/colors/base16-donokai.vim
l common/weechat $h/.weechat
l common/scim/rc $h/.scimrc
l common/scim/lua $h/.scim/lua
l common/nnn $c/nnn
l common/kak $c/kak
l common/gpg/agent.conf $h/.gnupg/gpg-agent.conf
l common/htop/rc $c/htop/htoprc
l common/kitty $c/kitty
l common/mutt/rc $c/.muttrc
l common/git/config $h/.gitconfig
l common/elixir/iex.exs $h/.iex.exs
l common/data/user-dirs $c/user-dirs.dirs
popd

for s in $c/lytedev-env/*/dotfiles-setup.d.fish
	source $s $dfp $h $c
end

# execute the user's shell
set ush (getent passwd $LOGNAME | cut -d: -f7)
echo "Dotfiles Installed! Don't forget to setup environments!"
exec $ush