You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.4 KiB
Bash

#!/bin/sh
# Exit on failures
set -e
SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
# Load all necessary functions
# shellcheck source=./subscripts/messages.sh
. "$SCRIPT_DIR/subscripts/messages.sh"
# shellcheck source=./subscripts/build.sh
. "$SCRIPT_DIR/subscripts/build.sh"
# shellcheck source=./subscripts/setters.sh
. "$SCRIPT_DIR/subscripts/setters.sh"
# shellcheck source=./subscripts/formatters.sh
. "$SCRIPT_DIR/subscripts/formatters.sh"
# shellcheck source=./subscripts/init.sh
. "$SCRIPT_DIR/subscripts/init.sh"
# Returns a path in the script directory
path() {
echo "$SCRIPT_DIR/$1"
}
show_help() {
echo "usage: $0 [-h | --help] <command> [OPTIONS]"
echo "blogsh is software to build and deploy statically generated blogs written in markdown with"
echo "a bit of HTML templating. Markdown files are compiled using pandoc, which is a hard"
echo "dependency. There is also some functionality that uses notify-send, but that's optional"
echo "and isn't required as long as notifications aren't used."
echo " Commands:"
echo " init:"
echo " Prepare a new blog by creating a new directory, initializing a git repo and copying"
echo " all template files there. Requires an additinal argument: path to the new blog dir."
echo " clean:"
echo " Remove all built files."
echo " build:"
echo " Go over the directory and build all files"
echo " deploy:"
echo " Copy the files to the specified environment"
echo " If no action is passed, then 'build' is assumed."
echo ""
echo " Options:"
echo " [ -e | --env=<env> ] Specify the target environment of the script. Proper environments"
echo " are specified by filenames in the 'configs' dir (except for 'default'). If left"
echo " empty, 'local' is assumed."
echo " [ -h | --help ] Show this help message"
echo " [ -n | --notify ] Show a desktop notification using notify-send when the script ends"
echo ""
echo " Environment files are meant to ease testing and deploying the blog to different"
echo " targets like production and testing ones. The 'default' file contains variables that are"
echo " valid for all targets and is read every time. Env files can contain any of the following"
echo " variables:"
echo " BLOG_TITLE: Blog's title. By default used as <h1> on the home page, tabs title and in"
echo " SEO tags in various places. "
echo " BLOG_FEED_TITLE: Blog's title for feeds."
echo " BLOG_DESCRIPTION: Description of the blog. By default it is used in rss/atom feeds."
echo " DATE_FORMAT_INDEX: Optional. Article creation dates format that are on index pages."
echo " If omitted, %F is used."
echo " DATE_FORMAT: Optional. Creation and last edit dates format at the end of the article."
echo " If omitted, DATE_FORMAT_INDEX is used."
echo " USE_HTML_IN_LINKS: Optional. Whether to add .html to links for the blog."
echo " If omitted, true is used."
echo " RSYNC_OPTIONS: Optional. Additional options for the rsync command for deployment."
echo " If omitted, '-rLtvz' is used."
echo ""
echo " In addition, the following variables can be specified in the 'default' config file (but"
echo " only there. Don't add them to other env files, as this can make the script behave in"
echo " very weird ways):"
echo " ENV: Override the default target for deployment."
echo " ACTION: Override the default script action."
echo " SEND_NOTIFICATION: Whether by default the script should use notifications."
echo " DEFAULT_RSYNC_OPTIONS: Default options for rsync used in all rsync commands regardless"
echo " of the env."
exit 0
}
deploy() {
rsync "$DEFAULT_RSYNC_OPTIONS" "$RSYNC_OPTIONS" "$(path blog/)" "$(path data/)" "$BLOG_REMOTE"
}
clean() {
rm -rf "$(path blog)" "$(path build)"
}
# shellcheck source=./configs/default
. "$(path configs/default)"
export BLOG_TITLE
export BLOG_FEED_TITLE
export BLOG_DESCRIPTION
export BLOG_URL_ROOT
# Get the command line arguments and assign them to appropriate variables.
while test $# -gt 0; do
case "$1" in
-h|--help)
show_help
;;
-e)
shift
test $# -eq 0 || test -z "$1" && no_env
ENV=$1
shift
;;
--env=*)
ENV="$(echo "$1" | sed -e 's/^[^=]*=//g')"
test -z "$ENV" && no_env
shift
;;
clean|build|deploy)
ACTION="$1"
shift
;;
init)
ACTION="$1"
shift
INIT_DIR="$1"
test -z "$INIT_DIR" && no_init_dir
shift
;;
-n|--notify)
shift
SEND_NOTIFICATION=true
;;
--)
shift
break
;;
*)
unknown_arg "$1"
;;
esac
done
test "$ACTION" = "init" && init_blog
# Define all the defaults
# First the most crucial ones…
test -z "$ENV" && ENV=local
test -z "$ACTION" && ACTION=build
test -z "$SEND_NOTIFICATION" && SEND_NOTIFICATION=false
# …and now those that aren't as crucial.
test -z "$DATE_FORMAT_INDEX" && DATE_FORMAT_INDEX=%F
test -z "$DATE_FORMAT" && DATE_FORMAT="$DATE_FORMAT_INDEX"
test -z "$USE_HTML_IN_LINKS" && USE_HTML_IN_LINKS=true
test -z "$RSYNC_OPTIONS" && RSYNC_OPTIONS="-rLtvz"
test -z "$MAIN_STYLESHEET_PATH" && MAIN_STYLESHEET_PATH=""
ENV_FILE="$(path configs/"$ENV")"
# Check existence and readability of the passed env file
test -f "$ENV_FILE" || env_file_doesnt_exist
test -r "$ENV_FILE" || env_file_not_readable
# Check if passed env isn't set to 'default'
test "$ENV" = "default" && env_file_default
# Since I don't know what'll end up sourced, I don't want this warning. It's up to the user to make
# the file valid ¯\_(ツ)_/¯
# shellcheck disable=SC1090
. "$ENV_FILE"
# Check if notify-send is needed and available. If not, return an error.
test "$SEND_NOTIFICATION" = "true" && (command -v notify-send || notify_send_missing) 1>/dev/null
case "$ACTION" in
build)
check_pandoc "31"
build
success "Blog built"
;;
clean)
clean
check_pandoc
success "Blog cleaned"
;;
deploy)
deploy
check_pandoc
success "Blog deployed to $ENV"
;;
esac
exit 0