#!/bin/bash - #=============================================================================== # # FILE: build_AOB # # USAGE: ./build_AOB [date] # # DESCRIPTION: Build the AOB files for a particular month # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: --- # AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com # VERSION: 0.0.12 # CREATED: 2021-04-15 17:36:22 # REVISION: 2024-03-15 09:50:02 # #=============================================================================== set -o nounset # Treat unset variables as an error SCRIPT=${0##*/} BASEDIR=${0%/*} VERSION="0.0.12" STDOUT="/dev/fd/2" # # Make sure we're in the working directory # cd "$BASEDIR" || exit 1 # # Load library functions # LIB="$HOME/bin/function_lib.sh" [ -e "$LIB" ] || { echo "$SCRIPT: Unable to source functions"; exit 1; } # shellcheck disable=SC1090 source "$LIB" # {{{ -- Functions usage and _DEBUG #=== FUNCTION ================================================================ # NAME: _usage # DESCRIPTION: Report usage # PARAMETERS: None # RETURNS: Nothing #=============================================================================== _usage () { cat >$STDOUT <<-endusage Usage: ./${SCRIPT} [-h] [-D] [date] Version: $VERSION Converts the AOB in Markdown format for a particular month to HTML and to text Options: -h Print this help -D Select debug mode (works the same; more output) Arguments (optional): date Specifies the month to build the AOB for. The default is the current month. The format can be YYYY-MM (e.g. 2022-05) or any date format that the 'date' command can parse, so 2022-04-01 or 01-Apr-2022 and so on. If the date cannot be parsed an error will be reported. Examples ./${SCRIPT} -h ./${SCRIPT} -D 01-February-2021 ./${SCRIPT} 2021-02 endusage exit } #=== FUNCTION ================================================================ # NAME: _DEBUG # DESCRIPTION: Writes a message if in DEBUG mode # PARAMETERS: List of messages # RETURNS: Nothing #=============================================================================== _DEBUG () { [ "$DEBUG" == 0 ] && return for msg in "$@"; do printf 'D> %s\n' "$msg" done } # }}} #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Base and database directories # PARENT="$HOME/HPR" BASEDIR="$PARENT/Community_News" cd "$BASEDIR" || { echo "Failed to cd to $BASEDIR"; exit 1; } # IADIR="$PARENT/InternetArchive" # # Option defaults # DEBUG=0 # # Process options # while getopts :hdD opt do case "${opt}" in h) _usage;; D) DEBUG=1;; ?) echo "$SCRIPT: Invalid option; aborting"; exit 1;; esac done shift $((OPTIND - 1)) # # Handle the optional argument # if [[ $# -eq 1 ]]; then startdate="$1" # Normalise a YYYY-MM date so 'date' will not complain if [[ $startdate =~ ^[0-9]{4}-[0-9]{2}$ ]]; then startdate+='-01' fi # Validate the date and standardise it if it's OK tmp="$(date -d "$startdate" +%Y-%m)" || { echo "Use a date such as $(date +%Y-%m)" exit 1 } startdate="$tmp" else startdate="$(date +%Y-%m)" fi _DEBUG "Date used: $startdate" # # We added a new field in 2022, 'item_last_updated' which is taken from the IA # (which we discovered was being maintained). It is a Unix date field, but the # view 'episodes_view' converts it. # # TODO: Since query3 was added it has made query1 and query2 obsolete. We # generate a per-month table with query3 which is turned into HTML using awk # and used in the AOB report. The code below that uses these queries and their # results could now be removed (or commented out). # #query1="select count(*) from episodes where id between 871 and 2429 and with_derived = 1" ##query1="select count(*) from episodes_view where id between 871 and 2429 and \ ##item_last_updated between '${startdate}-01' and \ ##date('${startdate}-01','+1 month') and with_derived = 1" ## ##query2='select count(*) from episodes where id between 871 and 2429 and with_derived = 0' ## ##query3=$(cat <"$TMP1" 7<<'ENDAWK' ##BEGIN{ ## total = 0 ## remainder = (2429 - 871 + 1) ## print "" ## print ""\ ## "" ##} ##{ ## total = total + $2 ## remainder = remainder - $2 ## printf "\n", ## $1,$2,total,remainder ##} ##END{ ## print "
MonthMonth countRunning totalRemainder
%s%s%s%s
" ##} ##ENDAWK ##cat >>"$TMP1" <Table updated: $(date --utc +'%F %T')

##ENDDATE ##_DEBUG "Table" "$(cat "$TMP1")" # # Build the output files # # if tpage --define "uploads=$UPLOADS" --define "remaining=$REMAINING" \ # --define "table=$TMP1" "$AOBMKD" |\ # pandoc -f markdown-smart -t html5 -o "${AOBMKD%mkd}html"; then # if pandoc -f markdown-smart -t html5 "$AOBMKD" -o "${AOBMKD%mkd}html"; then echo "Converted $AOBMKD to HTML" else echo "Conversion of $AOBMKD to HTML failed" fi exit # vim: syntax=sh:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21:fdm=marker