hpr-tools/Show_Submission/do_change_format

175 lines
4.5 KiB
Bash
Executable File

#!/bin/bash -
#===============================================================================
#
# FILE: do_change_format
#
# USAGE: ./do_change_format <epno>
#
# DESCRIPTION: Changes the declared format of a show. Mainly useful to change
# 'markdown_standard' (largely useless) to 'Markdown_Pandoc',
# but can be used to override notes declared as 'html5' when
# they are 'plain_text'.
# 2022-12-22: Now using only shownotes.json, writing the new
# value to the .format file and writing status changes to the
# .status file. DOES NOT update the JSON.
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
# VERSION: 0.0.4
# CREATED: 2018-12-06 11:11:30
# REVISION: 2024-02-13 20:33:42
#
#===============================================================================
set -o nounset # Treat unset variables as an error
SCRIPT=${0##*/}
#DIR=${0%/*}
VERSION='0.0.4'
#
# Load library functions (make_file_list, range_parse, cleanup_temp)
#
LIB="$HOME/bin/function_lib.sh"
[ -e "$LIB" ] || { echo "$SCRIPT: Unable to source functions"; exit 1; }
# shellcheck source=/home/cendjm/bin/function_lib.sh
source "$LIB"
#
# Check the argument after any options
#
if [[ $# -ne 1 ]]; then
echo "$SCRIPT ($VERSION): Usage $SCRIPT shownumber"
exit
fi
#
# Paths to files
#
BASENAME="$HOME/HPR/Show_Submission"
SHOWDIR="$BASENAME/shownotes/hpr${1}"
FORMAT="$SHOWDIR/.format"
FROM="$SHOWDIR/shownotes.json"
STATUSFILE="$SHOWDIR/.status"
JSONNOTES="$SHOWDIR/shownotes.json"
JSONORIG="$SHOWDIR/shownotes.json.orig"
RELEASE="$SHOWDIR/.release"
ORIGIN="$SHOWDIR/.origin"
DEST="hpr@hackerpublicradio.org:upload"
SSHPORT=22
#
# Collect the place to write results and the release date
#
upload_dir=$(cat "$ORIGIN")
release_date=$(cat "$RELEASE")
#
# The permitted formats as defined in the web form
#
declare -A formats
formats[plain_text]='FALSE'
formats[html5]='FALSE'
formats[markdown_standard]='FALSE'
formats[Markdown_GitHub]='FALSE'
formats[Markdown_Pandoc]='FALSE'
formats[restructured_text]='FALSE'
formats[txt2tags]='FALSE'
#
# Check that we actually have notes for this show, and they aren't empty
#
if [[ -e $FROM ]]; then
if [[ ! -s $FROM ]]; then
echo "File $FROM is empty"
exit 1
fi
else
echo "$SCRIPT: File not found: $FROM"
exit 1
fi
#
# Make temporary files and set traps to delete them.
#
# TMP1 - HTML header
#
TMP1=$(mktemp) || {
echo "$SCRIPT: creation of temporary file failed!"
exit 1
}
trap 'cleanup_temp $TMP1' SIGHUP SIGINT SIGPIPE SIGTERM EXIT
#
# Record the current format
#
FFORMAT="$(cat "$FORMAT")"
formats[$FFORMAT]='TRUE'
# for k in $(printf '%s\n' "${!formats[@]}" | sort); do
# printf 'D> %18s: %s\n' "$k" "${formats[$k]}"
# done
#
# Generate a Zenity list box with radio buttons. Show current setting and
# allow any other to be selected to replace it.
#
newfmt=$(for k in $(printf '%s\n' "${!formats[@]}" | sort); do
printf '%s\n%s\n' "${formats[$k]}" "$k"; done |\
zenity --list --radiolist --height=300 --width=300 \
--column=Choice --title="Change format of notes" \
--text="Choose format" --column=Format 2> /dev/null) ||\
{ echo "Cancelled"; exit; }
# echo "D> $newfmt"
#
# If there was a change save the new value
#
if [[ $newfmt != "$FFORMAT" ]]; then
echo "Changing format to '$newfmt'"
#
# Update .format
#
echo "$newfmt" > "$FORMAT"
#
# Update the JSON
#
cp "$FROM" "${FROM}.orig"
cp "$FROM" "$TMP1"
jq -c --arg new "$newfmt" \
'(.metadata.POST.shownotes_format,.metadata.Shownotes_Format) |= $new' \
"$TMP1" > "$FROM"
#
# Upload updated JSON to the server, but only if the release date is in
# the future
#
rd=$(date -d "$release_date" +%s)
now=$(date +%s)
if [[ $((rd - now)) -gt 0 ]]; then
echo "Copying $JSONNOTES to upload/$upload_dir/shownotes.json on the HPR server"
echo "Copying $JSONORIG to upload/$upload_dir/shownotes.json.orig on the HPR server"
scp -P $SSHPORT "$JSONNOTES" "$JSONORIG" "$DEST/$upload_dir/"
else
echo "JSON files not uploaded to the server"
echo "The release date $release_date is in the past!"
fi
#
# Update the status file
#
echo "format" >> "$STATUSFILE" || \
{ echo "Failed to update $STATUSFILE"; exit 1; }
fi
exit
# vim: syntax=sh:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21