scripts/bread/bin/bread

100 lines
1.7 KiB
Plaintext
Raw Normal View History

2020-04-11 19:56:00 +00:00
#!/bin/bash
# bread: do stuff on breadpunk
# by breadw
: "${BREADNEWS:=/bread/news}"
: "${BREADDOCS:=/bread/docs}"
2020-04-11 20:03:15 +00:00
. /bread/site/lib.sh
2020-04-11 19:56:00 +00:00
usage()
{
cat <<-ENDOFUSAGE
🍞 let's get this 🥖 B R E A D 🥐
a tool for breadpunk.club
🍞 🥖 🥐
usage: bread [-h] COMMAND [OPTIONS]
🍞 🥖 🥐
commands:
- news: read the news
2020-04-11 20:53:03 +00:00
- docs: read documents
2020-04-11 20:21:05 +00:00
for detailed usage of each command,
run bread COMMAND -h
🍞 🥖 🥐
2020-04-11 19:56:00 +00:00
ENDOFUSAGE
exit 1
}
bread_news()
{
2020-04-11 20:21:05 +00:00
num=4
2020-04-11 20:53:03 +00:00
artcmd="find \"$BREADNEWS\" -type f | sort -nr | head -n \"\$num\""
2020-04-11 20:21:05 +00:00
while getopts hn:s:w opt; do
case "$opt" in
h)
cat <<-ENDOFHELP
bread news : read the news
usage: bread news [-n NUM] [-s QUERY] [-w]
-n NUM read NUM newest articles. default: 4
-s QUERY search for QUERY in news database
-w write a new article (admin only)
ENDOFHELP
exit
;;
n) num="$OPTARG" ;;
s)
2020-04-11 20:53:03 +00:00
cmd="grep -R \"$OPTARG\" \"$BREADNEWS\" -l"
2020-04-11 20:21:05 +00:00
;;
w)
groups "$USER" | grep -q admin || {
echo "You're not an admin!"
exit 3
}
article="$BREADNEWS/$(date +%F)@$(date +%H:%M).md"
cat <<-ENDOFARTICLE >"$article"
title
author $USER
date $(date +%F)
ENDOFARTICLE
$EDITOR "$article"
exit
;;
\?) return 2 ;;
*) return 2 ;;
esac
done
2020-04-11 20:53:03 +00:00
for art in $(eval "$artcmd"); do
2020-04-11 19:56:00 +00:00
echo " 🍞 🥖 🥐 "
2020-04-11 20:03:15 +00:00
printf '\033[34;1;4m%s\033[0m\n' "$(M title "$art")"
printf '\033[34;1m%s\033[0m\n' "$(M date "$art")"
C "$art" | pandoc -t plain
2020-04-11 19:56:00 +00:00
done
2020-04-11 20:03:15 +00:00
echo " 🍞 🥖 🥐 "
2020-04-11 19:56:00 +00:00
}
2020-04-11 20:53:03 +00:00
bread_docs()
{
true
}
2020-04-11 19:56:00 +00:00
main()
{ # entry point
while getopts h opt; do
case "$opt" in
h) usage ;;
\?) exit 2 ;;
*) exit 2 ;;
esac
done
shift "$((OPTIND - 1))"
cmd="$1"; shift
2020-04-11 20:21:05 +00:00
bread_"$cmd" "$@" || exit $?
2020-04-11 19:56:00 +00:00
}
2020-04-11 20:03:15 +00:00
main "$@"