mirror of https://git.envs.net/envs/burrow.git
329 lines
7.4 KiB
Bash
Executable File
329 lines
7.4 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_recipe=0
|
|
arg_phlog=0
|
|
arg_create_config=0
|
|
arg_update_git=0
|
|
|
|
function show_help() {
|
|
cat > /dev/stdout << END
|
|
${0} [-hv] <arguments>
|
|
|
|
ARGUMENTS (multiple allowed):
|
|
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 - show this help
|
|
-v - show current version info
|
|
-d - debug mode
|
|
|
|
END
|
|
}
|
|
|
|
pushd () {
|
|
command pushd "$@" 2>/dev/null 1>&2
|
|
}
|
|
|
|
popd () {
|
|
command popd 2>/dev/null 1>&2
|
|
}
|
|
|
|
function parseopts() {
|
|
while getopts ":hvd" opt
|
|
do
|
|
case "${opt}" in
|
|
h)
|
|
show_help
|
|
exit 0
|
|
;;
|
|
d) flag_debug=1 ;;
|
|
v) echo "$version" ;;
|
|
*)
|
|
echo "Invalid option. Try -h for help."
|
|
exit 1
|
|
esac
|
|
done
|
|
return $OPTIND
|
|
}
|
|
|
|
function parseargs() {
|
|
for arg in "$@"
|
|
do
|
|
argc=${arg^^}
|
|
case $argc in
|
|
"HELP")
|
|
show_help
|
|
exit 0
|
|
;;
|
|
"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() {
|
|
parseopts "$@"
|
|
parseargs "${@:$?}"
|
|
|
|
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 "$@"
|