#!/bin/sh

if [ -f ~/.c.query ]
then
    echo -n "Open $(< ~/.c.query) again (Y/n)? "
    read
    case $REPLY in
        [Nn]) rm -f ~/.c.query; ;;
    esac
fi

[ -f ~/.c.query ] \
    && EXTRA="--query $(<~/.c.query)"

[ ! -z "$@" ] \
    && EXTRA="--query $@"

FZF_DEFAULT_COMMAND='find \
                        $HOME/.config/* \
                        $HOME/.* \
                           -maxdepth 1 \
                           -type f \
                           \(     -name "*.cfg" \
                               -o -name "*.conf" \
                               -o -name "*.lua" \
                               -o -name "*.sh" \
                               -o -name "*.toml" \
                               -o -name "*.xsession*" \
                               -o -name "*rc" \
                               -o -name ".Xresources" \
                               -o -name ".Xdefaults" \
                               -o -name ".ini" \
                               -o -name "config" \
                           \)'

export FZF_DEFAULT_COMMAND

F=$(fzf -1 -e +s $EXTRA --preview='cat {}')

[ -z "$F" ] \
    || vim "$F" && echo "$F" > ~/.c.query;

[ -z "$F" ] \
    && rm -f ~/.c.query
