burrow/burrow

353 lines
8.0 KiB
Bash
Executable File

#!/usr/bin/env bash
version="v1.0.0rc"
shopt -s extglob
configfiles="$HOME/.config/burrow/config $HOME/.config/burrow $HOME/.burrow"
editor=${EDITOR:-vi}
# vars from config
config_location_gopher="$HOME/gopher"
config_location_recipebox="${config_location_gopher}/recipebox"
config_location_phlog="${config_location_gopher}/phlog"
config_custom_editor=""
config_post_phlog_command=""
config_post_recipebox_command=""
config_git_commit=false
config_git_push=false
# vars from flags
flag_debug=0
# vars from args
arg_options=hvd
arg_longoptions=help,version,debug
arg_recipe=0
arg_phlog=0
arg_create_config=0
arg_update_git=0
# silence directory movements
pushd () {
command pushd "$@" 2>/dev/null 1>&2
}
popd () {
command popd 2>/dev/null 1>&2
}
function show_help() {
cat > /dev/stdout << END
burrow [options] [commands]
COMMANDS:
phlog Create new phlog entry
recipe Add new recipe to box
create-config Create a default configuration file
update-git Silently pulls gopher git repo if it exists
OPTIONAL FLAGS:
-h, --help Show this help
-v, --version Show current version info
-d, --debug Debug mode
END
}
function parse_input() {
getopt --test > /dev/null
if [[ $? -ne 4 ]]
then
echo "I'm sorry, \`getopt --test\` failed in this environment."
exit 1
fi
parsed=$(getopt --options=$arg_options --longoptions=$arg_longoptions --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then
exit 2
fi
eval set -- "$parsed"
while true; do
case "$1" in
-h|--help)
show_help
shift
;;
-v|--version)
echo "$version"
shift
;;
-d|--debug)
flag_debug=1
shift
;;
--)
shift
break
;;
*)
echo "Internal error: $1"
exit 3
;;
esac
done
if [[ $# -lt 1 ]]
then
exit 0
fi
for arg in "$@"
do
argc=${arg,,}
case $argc in
"shortlist")
echo "phlog recipe create-config update-git"
exit 0
;;
"phlog") arg_phlog=1 ;;
"recipe") arg_recipe=1 ;;
"create-config") arg_create_config=1 ;;
"update-git") arg_update_git=1 ;;
esac
done
}
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_date() {
echo "Updating gopher last modified date"
sed --in-place='' "s/.*Last\ Updated:.*/ ==== Last Updated: $(date +"%B %d$(day_suffix), %Y") ====/" "${config_location_gopher}/gophermap"
}
function check_directory() {
if [[ ! -d "$1" ]]
then
echo "Missing directory: $1 aborting."
exit 1
fi
}
function recipe_new() {
read -r -e -p "What is the name of your recipe: " title
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
post_path="${config_location_recipebox}/$title_slug.txt"
if [[ -f $post_path ]]
then
echo "$post_path already exists"
$editor "$post_path"
else
if [[ -f "${config_location_recipebox}/.template" ]]
then
cat "${config_location_recipebox}/.template" > "$post_path"
else
{
echo "Creating $post_path"
echo "----------------------------------------"
echo "$title"
echo "----------------------------------------"
echo ""
echo ""
} >> "$post_path"
fi
$editor "$post_path"
echo "Updating recipebox listing"
echo -e "0$title\t$(basename "$post_path")\n$(cat "${config_location_recipebox}/gophermap")" > "${config_location_recipebox}/gophermap"
sort -fo "${config_location_recipebox}/gophermap" "${config_location_recipebox}/gophermap"
update_gopher_date
if [[ $config_post_recipebox_command != "" ]]
then
echo "Running post-recipe command"
$config_post_recipebox_command
fi
if [[ $config_git_commit != false ]]
then
echo "Committing to git repository"
pushd "$config_location_gopher"
git add "${config_location_recipebox}/gophermap" "${post_path}" "${config_location_gopher}/gophermap"
git commit -m "Recipe: $title"
if [[ $config_git_push != false ]]
then
echo "Pushing to git remote"
git pull
git push
fi
popd
fi
fi
}
function phlog_new() {
read -r -e -p "Enter a title for your post: " title
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
post_dir="${config_location_phlog}/$(date +%Y%m%d)-$title_slug"
post_path="$post_dir/gophermap"
if [[ -f $post_path ]]
then
echo "$post_path already exists"
$editor "$post_path"
else
if [[ -f "${config_location_phlog}/.template" ]]
then
cat "${config_location_phlog}/.template" > "$post_path"
else
echo "Creating $post_path"
mkdir -p "$post_dir"
{
echo "----------------------------------------"
echo "$title"
date +"%B %d$(day_suffix), %Y"
echo "----------------------------------------"
echo ""
echo ""
echo ""
echo "Links:"
echo ""
} >> "$post_path"
fi
$editor "$post_path"
echo "Processing phlog for links and autoindenting"
links="${post_dir}/links"
sed -n '/^Links:$/,$p' "$post_path" | tail -n +2 > "$links"
sed --in-place='' '/^Links:$/,$d' "$post_path"
sed --in-place='' 's/^/ /' "$post_path"
cat "$links" >> "$post_path"
rm "$links"
echo "Updating phlog listing"
echo -e "1$(date +%Y-%m-%d) - $title\t$(basename "$post_dir")\n$(cat "${config_location_phlog}/gophermap")" > "${config_location_phlog}/gophermap"
update_gopher_date
if [[ $config_post_phlog_command != "" ]]
then
echo "Running post-phlog command"
$config_post_phlog_command
fi
if [[ $config_git_commit != false ]]
then
echo "Committing to git repository"
pushd "$config_location_gopher"
git add "${config_location_phlog}/gophermap" "${post_path}" "${config_location_gopher}/gophermap"
git commit -m "Phlog: $title"
if [[ $config_git_push != false ]]
then
echo "Pushing to git remote"
git pull
git push
fi
popd
fi
fi
}
function create_config() {
if [[ ! -f "$HOME/.config/burrow/config" ]] &&
[[ ! -f "$HOME/.config/burrow" ]] &&
[[ ! -f "$HOME/.burrow" ]]
then
config="$HOME/.config/burrow/config"
mkdir -p "$(dirname "$config")"
{
echo "config_location_gopher=\"$HOME/gopher\""
echo "config_location_phlog=\"$HOME/gopher/phlog\""
echo "config_location_recipebox=\"$HOME/gopher/recipebox\""
echo "config_git_commit=false"
echo "config_git_push=false"
echo "config_custom_editor=false"
} >> "$config"
else
echo "Configuration already exists. Abort."
fi
}
function update_git() {
pushd "$config_location_gopher"
if [[ $? -eq 0 ]]
then
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) ]]
then
git pull -q
fi
popd
fi
}
function main() {
parse_input "$@"
if [[ ${flag_debug} -gt 0 ]]
then
set -x
fi
if [[ ${arg_create_config} -gt 0 ]]
then
create_config
fi
for configfile in $configfiles
do
if [[ -f $configfile ]]
then
source "$configfile"
fi
done
if [[ $config_custom_editor != false ]]
then
editor=$config_custom_editor
fi
if [[ ${arg_update_git} -gt 0 ]]
then
update_git
fi
if [[ ${arg_recipe} -gt 0 ]]
then
check_directory "$config_location_gopher"
check_directory "$config_location_recipebox"
recipe_new
fi
if [[ ${arg_phlog} -gt 0 ]]
then
check_directory "$config_location_gopher"
check_directory "$config_location_phlog"
phlog_new
fi
}
main "$@"