#!/bin/sh
# sfeed-reader: update the feeds and read them in sfeed_curses. First run copies the example config.
#   sfeed-reader          update, then read
#   sfeed-reader -n       read without updating
mkdir -p "$HOME/.sfeed/feeds"
if [ ! -f "$HOME/.sfeed/sfeedrc" ]; then
    cp /opt/sfeed/share/doc/sfeed/sfeedrc.example "$HOME/.sfeed/sfeedrc"
    echo "Created ~/.sfeed/sfeedrc (edit it to add your feeds)."
fi
if [ "$1" != "-n" ]; then
    sfeed_update "$HOME/.sfeed/sfeedrc"
fi

# The terminal window closes when this script ends: on an error wait for Enter so that the message can be read.
pause() { printf '\n%s\nPress Enter to close. ' "$1"; read dummy; exit 1; }
set -- "$HOME/.sfeed/feeds/"*
[ -f "$1" ] || pause "No feed was downloaded (check the network and the feeds in ~/.sfeed/sfeedrc; see the FAIL lines above)."
sfeed_curses "$@" || pause "sfeed_curses ended with an error."
