tildelog/lib/do_main.sh

88 lines
2.5 KiB
Bash

#!/usr/bin/env bash
# Main function
# Encapsulated on its own function for readability purposes
#
# $1 command to run
# $2 file name of a draft to continue editing (optional)
declare global_config
do_main() {
# make sure we're in the right directory
if [ "$(pwd)" != "$HOME/public_html/blog" ]; then
echo "You're not in your blog directory. Moving you there now"
mkdir -p "$HOME/.tildelog"
mkdir -p "$HOME/public_html/blog"
cd "$HOME/public_html/blog" || exit 1
fi
# Detect if using BSD date or GNU date
date_version_detect
# Load default configuration, then override settings with the config file
global_variables
# shellcheck disable=SC1090 # variable config file
[[ -f "$global_config" ]] && source "$global_config" &>/dev/null
global_variables_check
# Check for $EDITOR
[[ -z $EDITOR ]] &&
EDITOR=vim
# Check for validity of argument
[[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ]] &&
usage && return
[[ $1 == list ]] &&
list_posts && return
[[ $1 == tags ]] &&
list_tags "$@" && return
if [[ $1 == edit ]]; then
if (($# < 2)) || [[ ! -f ${!#} ]]; then
echo "Please enter a valid .md file to edit"
exit
fi
fi
# Test for existing html files
if ls ./*.md &>/dev/null; then
# We're going to back up just in case
tar -c -z -f ".backup.tar.gz" -- *.html &&
chmod 600 ".backup.tar.gz"
elif [[ $1 == rebuild ]]; then
echo "Can't find any html files, nothing to rebuild"
return
fi
# Keep first backup of this day containing yesterday's version of the blog
[[ ! -f .yesterday.tar.gz || $(date -r .yesterday.tar.gz +'%d') != "$(date +'%d')" ]] &&
cp .backup.tar.gz .yesterday.tar.gz &>/dev/null
[[ $1 == reset ]] &&
reset && return
create_css
create_includes
[[ $1 == post ]] && write_entry "$@"
[[ $1 == rebuild ]] && rebuild_all_entries && rebuild_tags
[[ $1 == delete ]] && rm "$2" &>/dev/null && rebuild_tags
if [[ $1 == edit ]]; then
if [[ $2 == -n ]]; then
edit "$3"
elif [[ $2 == -f ]]; then
edit "$3" full
else
edit "$2" keep
fi
fi
rebuild_index
all_posts
all_tags
make_rss
echo 'making gophermap'
make_gophermap
echo 'making geminicapsule'
make_gemini
echo 'deleting includes'
delete_includes
}