#!/usr/bin/bash
#shellcheck disable=SC1090

. "$(sfpath)" || exit 3

shellfu import pretty
shellfu import saturnin

shellfu import bmo_clip


usage() {
    mkusage "$@" \
            "[ls]" \
            "save [-l] [-1|-2|-c|-]" \
            "load [-l] [-1|-2|-c]" \
            "open" \
            "cat [-l] [-1|-2|-c]" \
            "rm" \
            "clean"
}

main() {
    local action        # action what to do
    local clipname      # name of system clipboard to use
    local fixnewline    # add final newline if missing
    local source        # where to get data from
    action=list_nice
    source=xclip
    clipname=primary
    fixnewline=false
    #shellcheck disable=SC2209
    while true; do case "$1" in
        save)   action=save;        shift ;;
        load)   action=load;        shift ;;
        open)   action=open;        shift ;;
        cat)    action=cat;         shift ;;
        ls)     action=list;        shift ;;
        lsh)    action=list_nice    shift ;;
        rm)     action=remove;      shift ;;
        clean)  action=clean;       shift ;;
        -1)     clipname=primary;   shift ;;
        -2)     clipname=secondary; shift ;;
        -c)     clipname=clipboard; shift ;;
        -l)     fixnewline=true;    shift ;;
        -)      source=stdin;       shift ;;
        "")     break                     ;;
        *)      usage -w "unknown argument: $1" ;;
    esac done
    debug "\$*='$*'"
    debug -v source clipname action
    #shellcheck disable=SC2034
    {
        BMO_CLIP__FIX_NEWLINE=$fixnewline
        BMO_CLIP__SOURCE=$source
        BMO_CLIP__CLIPNAME=$clipname
        BMO_CLIP__STORAGE="$(saturnin__get data-home)/clips"
    }
    case $action in
        save)       bmo_clip__save ;;
        load)       bmo_clip__load ;;
        open)       bmo_clip__open ;;
        cat)        bmo_clip__cat ;;
        remove)     bmo_clip__rm ;;
        clean)      bmo_clip__rm_all ;;
        list)       bmo_clip__lspaths ;;
        list_nice)  bmo_clip__ls ;;
    esac
}

main "$@"
