#!/bin/sh

zsh=${ZDOTDIR}/configs/hashes
lf=${XDG_CONFIG_HOME}/lf/shortcuts
nvim=${XDG_CONFIG_HOME}/nvim/lua/config/shortcuts.lua

clean() {
    echo "# NOTE: Managed by shortcuts script" >"$zsh"
    echo "# NOTE: Managed by shortcuts script" >"$lf"
    printf "%s\n%s\n" "-- NOTE: Managed by shortcuts script" "return {" >"$nvim"
}

finalize() {
    echo "}" >>"$nvim"
}

write() {
    while IFS= read -r line; do
        shortcut=$(echo "$line" | cut -d' ' -f1)
        path=$(echo "$line" | cut -d' ' -f2)
        path=$(eval "echo $path")

        printf "hash -d %s=%s\n" "$shortcut" "$path" >>"$zsh"
        printf "map g%s cd %s\n" "$shortcut" "$path" >>"$lf"
        printf "    [\"<c-a>%s\"] = \"%s\",\n" "$shortcut" "$path" >>"$nvim"
    done
}

clean

sed 's/#.*//;/^$/d;s/ \+/ /g' <"${XDG_CONFIG_HOME}/directories" | write

finalize