#!/bin/sh DOCUMENTATION=$(cat < build/assets/style.css). HTML files are translated in two ways: 1. non-'index.html' files are translated into their basename/index.html (eg. a/index.html -> a/index.html, and a/cool.html -> a/cool/index.html). 2. HTML files are passed through format.sh. The program exits immediately upon a single file failing to be transformed/translated. USAGE: build.sh [OPTIONS] Where the OPTIONS are: --directory DIR Builds the files into DIR instead of the default ./build folder. DIR should NOT contain a trailing '/'. --force Force the status of each operation to be either OK or FAIL. (Remove the SKIP option.) --help Outputs this manual-style help message and immediately exits. --no-colour Strips ANSI colour codes from the output. --show-skipped Show the skipped files explicitly with a SKIP status (instead of no output). --version Outputs the script name along with its version number. Requirements: Except for a single use of -nt, which is not POSIX compliant, but is implemented in most modern shells, this script is POSIX compliant. SPDX-FileCopyrightText: Copyright (c) 2022, Theodore Preduta. SPDX-License-Identifier: BSD-2-Clause END_OF_DOCUMENTATION ) usage() { [ -n "$1" ] && echo "$1" && echo echo "Usage: build.sh [OPTIONS]" echo "Try running with --help for more information." exit 1 } build_dir="./build" force="false" explicit_skip="false" status_ok_string="\e[32mOK\e[0m\n" status_skip_string="\e[36mSKIP\e[0m\n" status_fail_string="\e[31mFAIL\e[0m\n" while [ ! "$*" = "" ]; do case "$1" in --base-url) base_url="$2" shift 2;; --directory) build_dir="$2" shift 2;; --force) force="true" shift 1;; --help) echo "$DOCUMENTATION" exit 0;; --no-colour) status_ok_string="OK\n" status_skip_string="SKIP\n" status_fail_string="FAIL\n" shift 1;; --show-skipped) explicit_skip=true shift 1;; --version) echo "$DOCUMENTATION" | head -n 1 exit 0;; *) usage "Unkown argument: $1";; esac done status_start() { printf "%s '%s' -> '%s'..." "$1" "$2" "$3"; } status_ok() { printf "$status_ok_string"; } status_skip() { printf "$status_skip_string"; } status_fail() { printf "$status_fail_string"; } process_file() { from="$1" to_dir="" to_name="" action="" location="" case "$from" in index.html|*/index.html) action="Formatting" to_name="index.html" to_dir="$build_dir/${from%/index.html}";; *.html) action="Formatting" to_name="index.html" to_dir="$build_dir/${from%.html}";; *) action="Copying" to_name="$(basename "$from")" to_dir="$build_dir/${from%/$to_name}";; esac if [ "$to_dir/$to_name" -nt "$from" ] && [ "$force" != "true" ]; then if [ "$explicit_skip" != "false" ]; then status_start "$action" "$from" "$to_dir/$to_name" status_skip fi return 0 fi status_start "$action" "$from" "$to_dir/$to_name" out="$(mkdir -p "$to_dir" 2>&1)" if [ ! "$out" = "" ]; then status_fail echo "ERROR in $out" exit 1 fi case "$to_name" in *.html) ./format.sh --location "${to_dir#$build_dir/.}/" < "$from" > "$to_dir/$to_name" code="$?" case "$code" in 0) status_ok;; 1) status_fail; echo "ERROR in format.sh: catastrophic failure."; exit 1;; 2) status_ok; echo "WARNING in format.sh: File just copied.";; 3) status_fail; echo "ERROR in format.sh: Open meta block."; exit 1;; 4) status_fail; echo "ERROR in format.sh: Unkown meta block option."; exit 1;; *) status_fail; echo "ERROR in format.sh: Unknown error."; exit 1;; esac;; *) out="$(cp "$from" "$to_dir/$to_name" 2>&1)" if [ ! "$out" = "" ]; then status_fail echo "ERROR in $out" exit 1 else status_ok fi;; esac } find . \( -path "$build_dir" -prune \) \ -o \( -type f -print \) | while read -r file do process_file "$file" done