#!/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%s\n" "-- NOTE: Managed by shortcuts script" "---@class config.shortcuts" "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 "    g%s = \"%s\",\n" "$shortcut" "$path" >>"$nvim"
    done
}

filter() {
    input=$1
    sed 's/#.*//;/^$/d;s/ \+/ /g' "$input"
}

clean

filter "$XDG_CONFIG_HOME/directories" | write

finalize