tildelog/lib/do_main.sh

88 lines
2.5 KiB
Bash
Raw Permalink Normal View History

2020-12-19 19:43:21 +00:00
#!/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"
2021-08-07 22:01:47 +00:00
mkdir -p "$HOME/.tildelog"
2020-12-19 19:43:21 +00:00
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
2020-12-19 19:43:21 +00:00
global_variables_check
# Check for $EDITOR
[[ -z $EDITOR ]] &&
EDITOR=vim
2020-12-19 19:43:21 +00:00
# Check for validity of argument
[[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ]] &&
usage && return
2020-12-19 19:43:21 +00:00
[[ $1 == list ]] &&
list_posts && return
[[ $1 == tags ]] &&
list_tags "$@" && return
if [[ $1 == edit ]]; then
if (($# < 2)) || [[ ! -f ${!#} ]]; then
2021-02-10 20:48:40 +00:00
echo "Please enter a valid .md file to edit"
2020-12-19 19:43:21 +00:00
exit
fi
fi
# Test for existing html files
if ls ./*.md &>/dev/null; then
2020-12-19 19:43:21 +00:00
# We're going to back up just in case
tar -c -z -f ".backup.tar.gz" -- *.html &&
chmod 600 ".backup.tar.gz"
2021-02-10 20:48:40 +00:00
elif [[ $1 == rebuild ]]; then
echo "Can't find any html files, nothing to rebuild"
return
2020-12-19 19:43:21 +00:00
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
2020-12-19 19:43:21 +00:00
[[ $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
2020-12-19 19:43:21 +00:00
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
2021-03-12 09:13:56 +00:00
echo 'making gophermap'
2020-12-19 19:43:21 +00:00
make_gophermap
2021-03-12 09:13:56 +00:00
echo 'making geminicapsule'
2020-12-19 19:43:21 +00:00
make_gemini
2021-03-12 09:13:56 +00:00
echo 'deleting includes'
2020-12-19 19:43:21 +00:00
delete_includes
}