burrow/burrow

250 lines
5.2 KiB
Plaintext
Raw Normal View History

2018-01-20 06:23:53 +00:00
#!/usr/bin/env bash
2018-01-20 08:53:36 +00:00
# v0.1.0
2018-01-20 06:23:53 +00:00
shopt -s extglob
2018-01-20 07:57:41 +00:00
configfiles="$HOME/.config/burrow/config $HOME/.config/burrow $HOME/.burrow"
2018-01-20 07:26:43 +00:00
editor=${EDITOR:-vi}
2018-01-20 07:57:41 +00:00
location_gopher="$HOME/gopher"
2018-01-20 07:26:43 +00:00
location_recipebox="${location_gopher}/recipebox"
location_phlog="${location_gopher}/phlog"
custom_editor=""
2018-01-20 08:53:36 +00:00
post_phlog_command=""
post_recipebox_command=""
2018-01-20 07:26:43 +00:00
git_commit=0
git_push=0
verbose=0
recipe=0
phlog=0
2018-01-20 06:23:53 +00:00
function show_help() {
cat > /dev/stdout << END
2018-01-20 07:26:43 +00:00
${0} [-h] [phlog|-p] [recipe|-r]
2018-01-20 06:23:53 +00:00
OPTIONAL ARGS:
2018-01-20 07:26:43 +00:00
help [-h] - show this help
verbose [-v] - verbose logging
recipe [-r] - add or edit a recipe
phlog [-p] - post to phlog
2018-01-20 06:23:53 +00:00
END
}
function parseopts() {
2018-01-20 07:46:21 +00:00
while getopts ":hvrp" opt
2018-01-20 06:23:53 +00:00
do
case "${opt}" in
h)
show_help
exit 0
;;
2018-01-20 07:26:43 +00:00
v) verbose=1 ;;
r) recipe=1 ;;
2018-01-20 08:53:36 +00:00
p) phlog=1 ;;
2018-01-20 06:23:53 +00:00
*)
echo "Invalid option. Try -h for help."
exit 1
esac
done
return $OPTIND
}
function parseargs() {
2018-01-20 07:26:43 +00:00
for arg in "$@"
2018-01-20 06:23:53 +00:00
do
2018-01-20 07:26:43 +00:00
argc=${arg^^}
case $argc in
"HELP")
show_help
exit 0
;;
"VERBOSE") verbose=1 ;;
"PHLOG") phlog=1 ;;
"RECIPE") recipe=1 ;;
esac
2018-01-20 06:23:53 +00:00
done
}
2018-01-20 07:26:43 +00:00
function day_suffix() {
case $(date +%d) in
01|1|21|31) echo "st";;
02|2|22) echo "nd";;
03|3|23) echo "rd";;
*) echo "th";;
esac
}
function update_gopher() {
sed --in-place='' "s/.*Last\ Updated:.*/ ==== Last Updated: $(date +"%B %d$(day_suffix), %Y") ====/" "${location_gopher}/gophermap"
}
2018-01-20 08:53:36 +00:00
function check_directory() {
if [[ ! -d "$1" ]]
then
echo "Missing directory: $1 aborting."
exit 1
fi
}
2018-01-20 07:26:43 +00:00
function recipe_new() {
2018-01-20 08:53:36 +00:00
read -r -e -p "What is the name of your recipe: " title
2018-01-20 07:26:43 +00:00
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
2018-01-20 08:53:36 +00:00
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
2018-01-20 07:26:43 +00:00
post_path="${location_recipebox}/$title_slug.txt"
if [[ -f $post_path ]]
then
echo "$post_path already exists"
2018-01-20 08:53:36 +00:00
$editor "$post_path"
2018-01-20 07:26:43 +00:00
else
if [[ -f "${location_recipebox}/.template" ]]
then
cat "${location_recipebox}/.template" > "$post_path"
else
2018-01-20 08:53:36 +00:00
{
echo "Creating $post_path"
echo "----------------------------------------"
echo "$title"
echo "----------------------------------------"
echo ""
echo ""
} >> "$post_path"
2018-01-20 07:26:43 +00:00
fi
2018-01-20 08:53:36 +00:00
$editor "$post_path"
echo -e "0$title\t$(basename "$post_path")\n$(cat "${location_recipebox}/gophermap")" > "${location_recipebox}/gophermap"
2018-01-20 07:26:43 +00:00
sort -fo "${location_recipebox}/gophermap" "${location_recipebox}/gophermap"
update_gopher
2018-01-20 08:42:38 +00:00
if [[ $post_recipebox_command != "" ]]
then
$post_recipebox_command
fi
2018-01-20 07:26:43 +00:00
if [[ $git_commit -gt 0 ]]
then
pushd "$location_gopher"
git add "${location_recipebox}/gophermap" "${post_path}" "${location_gopher}/gophermap"
git commit -m "Recipe: $title"
if [[ $git_push -gt 0 ]]
then
git pull
git push
fi
popd
fi
fi
}
2018-01-20 07:45:25 +00:00
function phlog_new() {
2018-01-20 08:53:36 +00:00
read -r -e -p "Enter a title for your post: " title
2018-01-20 07:26:43 +00:00
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
2018-01-20 08:53:36 +00:00
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
2018-01-20 07:26:43 +00:00
post_dir="${location_phlog}/$(date +%Y%m%d)-$title_slug"
post_path="$post_dir/gophermap"
if [[ -f $post_path ]]
then
echo "$post_path already exists"
2018-01-20 08:53:36 +00:00
$editor "$post_path"
2018-01-20 07:26:43 +00:00
else
if [[ -f "${location_phlog}/.template" ]]
then
cat "${location_phlog}/.template" > "$post_path"
else
echo "Creating $post_path"
mkdir -p "$post_dir"
2018-01-20 08:53:36 +00:00
{
echo "----------------------------------------"
echo "$title"
date +"%B %d$(day_suffix), %Y"
echo "----------------------------------------"
echo ""
echo ""
echo ""
echo "Links:"
echo ""
} >> "$post_path"
2018-01-20 07:26:43 +00:00
fi
2018-01-20 08:53:36 +00:00
$editor "$post_path"
2018-01-20 07:26:43 +00:00
links="$post_dir/links"
2018-01-20 08:53:36 +00:00
sed -n '/^Links:$/,$p' "$post_path" | tail -n +2 > "$links"
sed -i '/^Links:$/,$d' "$post_path"
sed -i 's/^/ /' "$post_path"
cat "$links" >> "$post_path"
2018-01-20 07:26:43 +00:00
rm "$links"
2018-01-20 08:53:36 +00:00
echo -e "1$(date +%Y-%m-%d) - $title\t$(basename "$post_dir")\n$(cat "${location_phlog}/gophermap")" > "${location_phlog}/gophermap"
2018-01-20 07:26:43 +00:00
update_gopher
2018-01-20 08:42:38 +00:00
if [[ $post_phlog_command != "" ]]
then
$post_phlog_command
fi
2018-01-20 07:26:43 +00:00
if [[ $git_commit -gt 0 ]]
then
pushd "$location_gopher"
git add "${location_phlog}/gophermap" "${post_path}" "${location_gopher}/gophermap"
git commit -m "Phlog: $title"
if [[ $git_push -gt 0 ]]
then
git pull
git push
fi
popd
fi
fi
}
2018-01-20 06:23:53 +00:00
function main() {
parseopts "$@"
parseargs "${@:$?}"
2018-01-20 07:26:43 +00:00
if [[ ${verbose} -gt 0 ]]
2018-01-20 06:23:53 +00:00
then
set -x
fi
2018-01-20 07:26:43 +00:00
2018-01-20 07:57:41 +00:00
for configfile in $configfiles
do
if [[ -f $configfile ]]
then
source "$configfile"
2018-01-20 07:57:41 +00:00
fi
done
if [[ $custom_editor ]]
then
editor=$custom_editor
fi
2018-01-20 08:53:36 +00:00
check_directory "$location_gopher"
2018-01-20 07:26:43 +00:00
if [[ ${recipe} -gt 0 ]]
then
2018-01-20 08:53:36 +00:00
check_directory "$location_recipebox"
2018-01-20 07:26:43 +00:00
recipe_new
fi
if [[ ${phlog} -gt 0 ]]
then
2018-01-20 08:53:36 +00:00
check_directory "$location_phlog"
2018-01-20 07:26:43 +00:00
phlog_new
fi
2018-01-20 06:23:53 +00:00
}
main "$@"