#!/bin/bash

set -eu
set -o pipefail

function opsys
{
	uname -s | tr '[:upper:]' '[:lower:]'
}

case "$(opsys)" in
	msys*)
		# shellcheck disable=SC2016
		hohm=$(powershell 'echo $HOME')
		# vimrc=_vimrc
		;;
	*)
		hohm=$HOME
		# vimrc=.vimrc
		;;
esac

if type gtar > /dev/null 2>&1
then
    tar=gtar
else
    tar=tar
fi

find='find'
for candidate_find in /usr/bin/find /bin/find /usr/bin/find.exe /bin/find.exe
do
    if [ -f "$candidate_find" ]
    then
        find="$candidate_find"
        break
    fi
done

(
    set -eu
    mkdir -p ~/.config/lvim
    cd config-lvim
    # shellcheck disable=SC2086
    $find . \
        \( -type f -o -type l \) -print | \
        $tar --create --no-recursion --file - --files-from - | \
        (cd "$hohm"/.config/lvim && "$tar" xvfp -) || true
)

(
    set -eu
    mkdir -p ~/.local/share/lunarvim/lvim/start
    cd start
    # shellcheck disable=SC2086
    $find . \
        \( -type f -o -type l \) -print | \
        $tar --create --no-recursion --file - --files-from - | \
        (cd "$hohm"/.local/share/lunarvim/lvim/start && "$tar" xvfp -) || true
)

# lvim --headless -c 'autocmd User PackerComplete quitall' -c 'PackerSync'
# lvim --headless +LvimCacheReset +q
lvim --headless '+Lazy sync' +q