#!/bin/sh


itemcache="${XDG_DATA_HOME:-$HOME/.cache}/wallabag-dmenu"

DMENU=${DMENU:-dmenu}

usage() {
    cat <<-EOF
	usage: dmenu-wallabag [-lah]
	     -l List Entries
	     -s Select Menu
	     -a Add an entry
	     -h Print help
	EOF

}

update_items(){
    [ "$(date -r "$itemcache" "+%d-%m-%Y %H")" = "$(date '+%d-%m-%Y') %H" ] ||
        wallabag list | head -n -1 | tail -n +2 > $itemcache
}

list_entries() {
    update_items
    items="$(cat $itemcache)"
    selection=$(echo -e "$items\n~SYNC~" | dmenu -l 10 -p "Choose an article:")
    if [ "$selection" = "~SYNC~" ]; then
        wallabag list | head -n -1 | tail -n +2 > $itemcache
        items="$(cat $itemcache)"
        selection=$(echo "$items" | dmenu -l 10 -p "Choose an article:")
    fi
    if [ "$selection" = "" ]; then
        exit
    fi
    selection=$(echo "$selection" | cut -d" " -f1)
    wallabag update --read $selection
    wallabag open $selection
    wallabag list | head -n -1 | tail -n +2 > $itemcache
}

add_entry() {
    url=$(echo -n "" | dmenu -p "Enter URL:")
    if [ "$url" = "" ]; then
        exit
    fi
    wallabag add $url
    wallabag list | head -n -1 | tail -n +2 > $itemcache
}

select_mode() {
    menu="Read\nAdd"
    action=$(echo -e "$menu" | dmenu -p "Select action: ")
    if [ "$action" = "" ]; then
        return
    fi
    if [ "$action" = "Read" ];then
        list_entries
    else
        add_entry
    fi
}

while getopts ':lahs' opt; do
    case "$opt" in
        l) list_entries ;;
        s) select_mode;;
        a) add_entry ;;
        h) usage && exit;;
        /?) echo "Unrecognized command: $OPTARG";;
    esac
done

