scripts/bread/bin/bread

192 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
# bread: do stuff on breadpunk
# by breadw
: "${BREADNEWS:=/bread/news}"
: "${BREADDOCS:=/bread/docs}"
#shellcheck disable=1091
. /bread/site/lib.sh
groupck()
{
groups "$USER" | grep -q "$1" || {
echo "You're not in the $1 group!"
exit 3
}
}
setperms()
{
chown :bakers "$1"
chmod a+r,g+w "$1"
}
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
- docs: read documents
for detailed usage of each command,
run bread COMMAND -h
🍞 🥖 🥐
ENDOFUSAGE
exit 1
}
bread_news()
{
num=4
artcmd="find \"$BREADNEWS\" -type f | sort -nr | head -n \"\$num\""
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) #shellcheck disable=2034
num="$OPTARG" ;;
s)
cmd="grep -R \"$OPTARG\" \"$BREADNEWS\" -l --exclude-dir .git"
;;
w)
groupck admin
article="$BREADNEWS/$(date +%F)@$(date +%H:%M).md"
cat <<-ENDOFARTICLE >"$article"
title
author $USER
date $(date +%F)
ENDOFARTICLE
$EDITOR "$article"
setperms admin
exit
;;
\?) return 2 ;;
*) return 2 ;;
esac
done
for art in $(eval "$artcmd"); do
echo " 🍞 🥖 🥐 "
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
done
echo " 🍞 🥖 🥐 "
}
bread_docs()
{
while getopts hs:ne: opt; do
case "$opt" in
h)
cat <<-ENDOFHELP
bread docs : read helpful documents
usage: bread docs [-s QUERY] [-n] [-e QUERY]
-s QUERY search for QUERY in documents
-n write a new document
: if it has the same name as another,
: it'll fail
-e QUERY edit a document matching QUERY
ENDOFHELP
exit
;;
s)
grep -R "$OPTARG" "$BREADDOCS" -l --exclude-dir .git|while read -r doc; do
echo " 🍞 🥖 🥐 "
printf '\033[34;1;4m%s\033[0m\n' "$(M title "$doc")"
printf '\033[34;1m%s\033[0m\n' "$(M date "$doc")"
C "$doc" | pandoc -t plain
done
echo " 🍞 🥖 🥐 "
return
;;
n)
groupck bakers
doc="$(mktemp)"
cat <<-ENDOFDOC >"$doc"
title
author $USER
ENDOFDOC
$EDITOR "$doc"
fname="$BREADDOCS/$(M title "$doc"|slugify).md"
[[ "$fname" =~ */.md ]] && return
if [ -f "$fname" ]; then
echo "$fname already exists!"
echo "Your article is still at $doc."
exit 4
else
cp "$doc" "$fname"
fi
setperms bakers
return
;;
e)
groupck bakers
matches=( $(grep -R "$OPTARG" "$BREADDOCS" -l --exclude-dir .git) )
if [[ "${#matches[@]}" -eq 1 ]]; then
$EDITOR "${matches[0]}"
return
fi
select doc in "${matches[@]}"; do
[ "$REPLY" = q ] && return
$EDITOR "$doc"
return
done
setperms bakers
;;
\?) return 2 ;;
*) return 2 ;;
esac
done
select doc in "$BREADDOCS"/*; do
[ "$REPLY" = q ] && return
echo " 🍞 🥖 🥐 "
printf '\033[34;1;4m%s\033[0m\n' "$(M title "$doc")"
printf '\033[34;1m%s\033[0m\n' "$(M date "$doc")"
C "$doc" | pandoc -t plain
done
echo " 🍞 🥖 🥐 "
}
bread_publish()
{
groupck admin
git -C /bread/site pull
make -C /bread/site publish
}
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
[ -z "$cmd" ] && usage
bread_"$cmd" "$@" || exit $?
}
main "$@"