burrow/burrow

395 lines
9.1 KiB
Plaintext
Raw Normal View History

2018-01-20 06:23:53 +00:00
#!/usr/bin/env bash
2018-03-03 16:56:50 +00:00
version="v1.2.0"
2018-01-21 02:26:33 +00:00
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}
# vars from config
config_dir_gopher="$HOME/gopher/"
config_dir_recipebox="recipebox"
config_dir_phlog="phlog"
config_dir_topics="topics"
config_gopher_server="sdf.org"
config_gopher_port="70"
config_gopher_root="/users/username/"
2018-01-20 21:28:35 +00:00
config_git_commit=false
config_git_push=false
2018-01-21 03:15:45 +00:00
config_autoindent=true
# vars from flags
flag_debug=0
flag_version=0
flag_help=0
2018-01-21 03:15:45 +00:00
flag_noautoindent=0
# vars from args
2018-01-21 02:26:33 +00:00
arg_options=hvd
2018-01-21 03:15:45 +00:00
arg_longoptions=help,version,debug,noautoindent
arg_shortlist=0
arg_recipe=0
arg_phlog=0
arg_topic=0
arg_create_config=0
arg_update_git=0
2018-01-20 06:23:53 +00:00
2018-01-21 02:26:33 +00:00
# silence directory movements
pushd () {
command pushd "$@" 2>/dev/null 1>&2
}
popd () {
command popd 2>/dev/null 1>&2
}
2018-01-20 06:23:53 +00:00
function show_help() {
cat > /dev/stdout << END
2018-01-21 02:26:33 +00:00
burrow [options] [commands]
2018-01-21 02:26:33 +00:00
COMMANDS:
phlog Create new phlog entry
recipe Add new recipe to box
topic Add or update a topic
2018-01-21 02:26:33 +00:00
create-config Create a default configuration file
update-git Silently pulls gopher git repo if it exists
2018-01-20 06:23:53 +00:00
OPTIONAL FLAGS:
2018-01-21 02:26:33 +00:00
-h, --help Show this help
-v, --version Show current version info
-d, --debug Debug mode
2018-01-20 06:23:53 +00:00
END
}
function parse_input() {
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)
flag_help=1
shift
;;
-v|--version)
flag_version=1
shift
;;
-d|--debug)
flag_debug=1
shift
;;
2018-01-21 03:15:45 +00:00
--noautoindent)
flag_noautoindent=1
shift
;;
--)
shift
break
2018-01-20 06:23:53 +00:00
;;
*)
echo "Internal error: $1"
exit 3
;;
2018-01-20 06:23:53 +00:00
esac
done
2018-01-20 07:26:43 +00:00
for arg in "$@"
2018-01-20 06:23:53 +00:00
do
2018-01-21 02:26:33 +00:00
argc=${arg,,}
2018-01-20 07:26:43 +00:00
case $argc in
"shortlist") arg_shortlist=1 ;;
2018-01-21 02:26:33 +00:00
"phlog") arg_phlog=1 ;;
"recipe") arg_recipe=1 ;;
"topic") arg_topic=1 ;;
2018-01-21 02:26:33 +00:00
"create-config") arg_create_config=1 ;;
"update-git") arg_update_git=1 ;;
*) echo "Unknown command: $arg";;
2018-01-20 07:26:43 +00:00
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
}
2018-01-20 20:02:34 +00:00
function update_gopher_date() {
2018-01-20 21:24:35 +00:00
echo "Updating gopher last modified date"
sed -i "s/.*Last\ Updated:.*/ ==== Last Updated: $(date +"%B %d$(day_suffix), %Y") ====/" "${config_dir_gopher}/gophermap"
2018-01-20 07:26:43 +00:00
}
2018-01-20 08:53:36 +00:00
function check_directory() {
if [[ ! -d "$1" ]]
then
echo "Missing directory: $1 aborting."
exit 1
fi
}
function finish {
if [[ -f "$temp_gophermap" ]]; then
rm "$temp_gophermap"
fi
if [[ -f "$temp_links" ]]; then
rm "$temp_links"
fi
}
trap finish EXIT
function make_post() {
query="$1"
type="$2"
use_gophermap="$3"
use_date="$4"
update_root="$5"
read -r -e -p "$query" title
2018-01-20 07:26:43 +00:00
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
type_gophermap="${config_dir_gopher}${type}/gophermap"
2018-01-20 07:26:43 +00:00
if $use_gophermap; then
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
if $use_date; then
post_dir="${config_dir_gopher}${type}/$(date +%Y%m%d)-$title_slug"
post_path="${config_gopher_root}${type}/$(date +%Y%m%d)-$title_slug"
else
post_dir="${config_dir_gopher}${type}/$title_slug"
post_path="${config_gopher_root}${type}/$title_slug"
fi
post_file="${post_dir}/gophermap"
post_file_path="${post_path}"
else
post_dir="${config_dir_gopher}${type}"
post_path="${config_gopher_root}${type}"
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
if $use_date; then
title_slug="$(date +%Y%m%d)-${title_slug}"
fi
post_file="${post_dir}/${title_slug}.txt"
post_file_path="${post_path}/${title_slug}.txt"
fi
if [[ -f $post_file ]]
2018-01-20 07:26:43 +00:00
then
$editor "$post_file"
2018-01-20 07:26:43 +00:00
else
mkdir -p "${post_dir}"
if [[ -f "${post_dir}/.template" ]]
2018-01-20 07:26:43 +00:00
then
cat "${post_dir}/.template" > "$post_file"
2018-01-20 07:26:43 +00:00
else
echo "Creating $post_file"
2018-01-20 08:53:36 +00:00
{
echo "----------------------------------------"
echo "$title"
if $use_date; then
date +"%B %d$(day_suffix), %Y"
fi
2018-01-20 08:53:36 +00:00
echo "----------------------------------------"
echo ""
echo ""
if $use_gophermap; then
echo "Links:"
echo ""
fi
} >> "$post_file"
2018-01-20 07:26:43 +00:00
fi
$editor "$post_file"
2018-01-20 08:42:38 +00:00
if $use_gophermap; then
if [[ $config_autoindent == true ]] && [[ $flag_noautoindent == 0 ]]
2018-01-20 07:26:43 +00:00
then
echo "Processing for links and autoindenting"
temp_links=$(mktemp -t "$(basename "$0").links.XXXXXXX") || exit 1
sed -n '/^Links:$/,$p' "$post_file" | tail -n +2 > "$temp_links"
sed -i '/^Links:$/,$d' "$post_file"
sed -i 's/^/ /' "$post_file"
cat "$temp_links" >> "$post_file"
rm "$temp_links"
2018-01-20 07:26:43 +00:00
fi
fi
echo "Updating $type"
temp_gophermap=$(mktemp -t "$(basename "$0").gophermap.XXXXXXX") || exit 1
2018-01-20 07:26:43 +00:00
if $use_gophermap; then
if $use_date; then
# if using gophermap and date
echo -e "1$(date +%Y-%m-%d) - $title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
else
# if using gophermap but not date
echo -e "1$title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
fi
2018-01-20 07:26:43 +00:00
else
if $use_date; then
# if not using gophermap but using date
echo -e "0$(date +%Y-%m-%d) - $title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
else
# if not using gophermap or date
echo -e "0$title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
fi
2018-01-20 07:26:43 +00:00
fi
cat "$type_gophermap" >> "$temp_gophermap"
cat "$temp_gophermap" > "$type_gophermap"
rm "$temp_gophermap"
2018-01-20 07:26:43 +00:00
# sort gophermap if not using date
if ! $use_date; then
sort -fo "${post_dir}/gophermap" "${post_dir}/gophermap"
2018-01-21 03:15:45 +00:00
fi
2018-01-20 07:26:43 +00:00
if $update_root; then
update_gopher_date
2018-01-20 08:42:38 +00:00
fi
2018-01-20 21:28:35 +00:00
if [[ $config_git_commit != false ]]
2018-01-20 07:26:43 +00:00
then
2018-01-20 21:24:35 +00:00
echo "Committing to git repository"
pushd "$config_dir_gopher"
git add "${post_dir}/gophermap" "${post_file}"
if $update_root; then
git add "${config_dir_gopher}/gophermap"
fi
git commit -m "$type: $title"
2018-01-20 21:28:35 +00:00
if [[ $config_git_push != false ]]
2018-01-20 07:26:43 +00:00
then
2018-01-20 21:24:35 +00:00
echo "Pushing to git remote"
2018-01-20 07:26:43 +00:00
git pull
git push
fi
popd
fi
fi
}
function topic_new() {
make_post "What topic? " "$config_dir_topics" true false true
}
function recipe_new() {
make_post "What is the name of your recipe? " "$config_dir_recipebox" false false true
}
function phlog_new() {
make_post "Enter a title for your post: " "$config_dir_phlog" true true true
}
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_dir_gopher=\"$HOME/gopher/\""
echo "config_dir_phlog=\"phlog\""
echo "config_dir_recipebox=\"recipebox\""
echo "config_dir_topics=\"topics\""
echo "config_gopher_server=\"sdf.org\""
echo "config_gopher_port=\"70\""
echo "config_gopher_root=\"/users/username/\""
echo "config_git_commit=false"
echo "config_git_push=false"
2018-01-21 03:15:45 +00:00
echo "config_autoindent=true"
} >> "$config"
else
echo "Configuration already exists. Abort."
fi
}
function update_git() {
pushd "$config_dir_gopher"
if [[ $? -eq 0 ]]
then
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) ]]
then
git pull -q
fi
popd
fi
}
2018-01-20 06:23:53 +00:00
function main() {
parse_input "$@"
2018-01-20 06:23:53 +00:00
2018-01-21 02:41:35 +00:00
if [[ $arg_shortlist -gt 0 ]]
then
echo "phlog topic recipe create-config update-git -v -h -d --version --help --debug --noautoindent"
exit 0
fi
2018-01-21 02:41:35 +00:00
if [[ $flag_version -gt 0 ]]
then
echo "$version"
fi
2018-01-21 02:41:35 +00:00
if [[ $flag_help -gt 0 ]]
then
show_help
fi
2018-01-21 02:41:35 +00:00
if [[ $flag_debug -gt 0 ]]
2018-01-20 06:23:53 +00:00
then
set -x
fi
2018-01-20 07:26:43 +00:00
2018-01-21 02:41:35 +00:00
if [[ $arg_create_config -gt 0 ]]
then
create_config
fi
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 [[ ${arg_update_git} -gt 0 ]]
then
update_git
fi
2018-01-20 08:53:36 +00:00
if [[ ${arg_recipe} -gt 0 ]]
2018-01-20 07:26:43 +00:00
then
check_directory "$config_dir_gopher"
check_directory "${config_dir_gopher}${config_dir_recipebox}"
2018-01-20 07:26:43 +00:00
recipe_new
fi
if [[ ${arg_topic} -gt 0 ]]
then
check_directory "$config_dir_gopher"
check_directory "${config_dir_gopher}${config_dir_topics}"
topic_new
fi
if [[ ${arg_phlog} -gt 0 ]]
2018-01-20 07:26:43 +00:00
then
check_directory "$config_dir_gopher"
check_directory "${config_dir_gopher}${config_dir_phlog}"
2018-01-20 07:26:43 +00:00
phlog_new
fi
2018-01-20 06:23:53 +00:00
}
main "$@"