forked from HPR/hpr-tools
		
	
		
			
				
	
	
		
			303 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			303 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
| #!/bin/bash -
 | |
| #===============================================================================
 | |
| #
 | |
| #         FILE: do_state_change
 | |
| #
 | |
| #        USAGE: ./do_state_change [-h] [-D] [-m] [-s] [-Y] <epno>
 | |
| #
 | |
| #  DESCRIPTION: Script to be invoked via symlinks to perform related tasks
 | |
| #               relating to show states.
 | |
| #
 | |
| #               - When called as 'do_reserve' toggles a '.reserved' marker.
 | |
| #               - When called as 'do_block' creates the directory if necessary
 | |
| #                 and makes a dummy 'shownotes.json' as well as a file called
 | |
| #                 '.dummy'. Doesn't delete any of this since other software
 | |
| #                 will tidy things.
 | |
| #
 | |
| #      OPTIONS: ---
 | |
| # REQUIREMENTS: ---
 | |
| #         BUGS: ---
 | |
| #        NOTES: ---
 | |
| #       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
 | |
| #      VERSION: 0.0.6
 | |
| #      CREATED: 2021-06-05 22:04:26
 | |
| #     REVISION: 2024-02-06 15:36:02
 | |
| #
 | |
| #===============================================================================
 | |
| 
 | |
| set -o nounset                              # Treat unset variables as an error
 | |
| 
 | |
| SCRIPT=${0##*/}
 | |
| #DIR=${0%/*}
 | |
| 
 | |
| # shellcheck disable=SC2034
 | |
| VERSION="0.0.6"
 | |
| 
 | |
| STDOUT="/dev/fd/2"
 | |
| 
 | |
| #
 | |
| # Load library functions
 | |
| #
 | |
| 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"
 | |
| 
 | |
| #
 | |
| # Colour codes
 | |
| #
 | |
| define_colours
 | |
| 
 | |
| #===  FUNCTION  ================================================================
 | |
| #         NAME: _usage
 | |
| #  DESCRIPTION: Report usage
 | |
| #   PARAMETERS: None
 | |
| #      RETURNS: Nothing
 | |
| #===============================================================================
 | |
| _usage () {
 | |
|     cat >$STDOUT <<-endusage
 | |
| Usage: ./${SCRIPT} [-h] [-D] [-m] [-s] [-Y] shownumber
 | |
| 
 | |
| Version: $VERSION
 | |
| 
 | |
| Script to be invoked via symlinks to perform related tasks relating to show states
 | |
| 
 | |
| Options:
 | |
|   -h                    Print this help
 | |
|   -D                    Run in debug mode where a lot more information is
 | |
|                         reported
 | |
|   -m                    Monochrome mode - no colours
 | |
|   -s                    Silent mode, output less text about actions
 | |
|   -Y                    Assume 'Yes' in answer to the prompt
 | |
| 
 | |
| Arguments:
 | |
|     shownumber
 | |
| 
 | |
| Examples
 | |
|   ./${SCRIPT} -h
 | |
|   ./${SCRIPT} -m 3112
 | |
|   ./${SCRIPT} -D 3112
 | |
|   ./${SCRIPT} 3112
 | |
|   ./${SCRIPT} -Y 3112
 | |
| 
 | |
| endusage
 | |
|     exit
 | |
| }
 | |
| 
 | |
| #===  FUNCTION  ================================================================
 | |
| #         NAME: exists
 | |
| #  DESCRIPTION: Determines wheher all paths given as arguments exist
 | |
| #   PARAMETERS: List of paths
 | |
| #      RETURNS: True if they all exist, otherwise false
 | |
| #===============================================================================
 | |
| exists () {
 | |
| 
 | |
|     for path in "$@"; do
 | |
|         if [[ ! -e $path ]]; then
 | |
|             return 1
 | |
|         fi
 | |
|     done
 | |
| 
 | |
|     return
 | |
| }
 | |
| 
 | |
| #===  FUNCTION  ================================================================
 | |
| #         NAME: _DEBUG
 | |
| #  DESCRIPTION: Writes one or more messages if in DEBUG mode. Each argument is
 | |
| #               seen as a message and is written on a separate line.
 | |
| #               References the global variable 'DEBUG' which is expected to be
 | |
| #               True if debug output is wanted.
 | |
| #   PARAMETERS: List of messages
 | |
| #      RETURNS: Nothing
 | |
| #===============================================================================
 | |
| _DEBUG () {
 | |
|     [ "$DEBUG" == 0 ] && return
 | |
|     for msg in "$@"; do
 | |
|         printf 'D> %s\n' "$msg"
 | |
|     done
 | |
| }
 | |
| 
 | |
| #===  FUNCTION  ================================================================
 | |
| #         NAME: _verbose
 | |
| #  DESCRIPTION: Writes a message in verbose mode
 | |
| #   PARAMETERS: $1      message
 | |
| #      RETURNS: Nothing
 | |
| #===============================================================================
 | |
| _verbose () {
 | |
|     [ "$VERBOSE" -eq 0 ] && return
 | |
|     for msg in "$@"; do
 | |
|         printf '%s\n' "$msg"
 | |
|     done
 | |
| }
 | |
| 
 | |
| #===  FUNCTION  ================================================================
 | |
| #         NAME: _silent
 | |
| #  DESCRIPTION: Writes a message unless in silent mode
 | |
| #   PARAMETERS: $1      message
 | |
| #      RETURNS: Nothing
 | |
| #===============================================================================
 | |
| _silent () {
 | |
|     [ "$SILENT" -eq 1 ] && return
 | |
|     for msg in "$@"; do
 | |
|         printf '%s\n' "$msg"
 | |
|     done
 | |
| }
 | |
| 
 | |
| #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | |
| 
 | |
| #
 | |
| # Paths to files
 | |
| #
 | |
| BASENAME="$HOME/HPR/Show_Submission"
 | |
| TPL="$BASENAME/shownotes/hpr%d/%s"
 | |
| 
 | |
| #
 | |
| # Option defaults
 | |
| #
 | |
| COLOUR=1 # use colours by default
 | |
| YES=0    # prompt for yes/no by default
 | |
| TOGGLE=0 # marker can't be removed by default
 | |
| 
 | |
| #
 | |
| # Process options
 | |
| #
 | |
| while getopts :hDmsY opt
 | |
| do
 | |
|     case "${opt}" in
 | |
|         h) _usage;;
 | |
|         D) DEBUG=1;;
 | |
|         m) COLOUR=0;;
 | |
|         s) SILENT=1;;
 | |
|         Y) YES=1;;
 | |
|         ?) echo "$SCRIPT: Invalid option; aborting"; exit 1;;
 | |
|     esac
 | |
| done
 | |
| shift $((OPTIND - 1))
 | |
| 
 | |
| DEBUG=${DEBUG:-0}
 | |
| SILENT=${SILENT:-0}
 | |
| 
 | |
| #
 | |
| # Cancel colours if requested
 | |
| #
 | |
| if [[ $COLOUR -eq 0 ]]; then
 | |
|     undefine_colours
 | |
| fi
 | |
| 
 | |
| #
 | |
| # Determine actions depending on how the script was called. Save filenames in
 | |
| # an array.
 | |
| #
 | |
| declare -a MK MFILES
 | |
| case "${SCRIPT#*/}" in
 | |
|     do_reserve)
 | |
|         MK+=('.reserved')
 | |
|         state='reserved'
 | |
|         TOGGLE=1
 | |
|         ;;
 | |
|     do_block)
 | |
|         echo "Blocking of pending shows is not needed any more."
 | |
|         echo "See Journal for details (2024-02-06)"
 | |
|         exit
 | |
| #         MK+=('shownotes.json')
 | |
| #         MK+=('.dummy')
 | |
| #         state='blocked'
 | |
| #         TOGGLE=0
 | |
|         ;;
 | |
|     *)
 | |
|         echo "Don't call this script directly; use one of its soft links"
 | |
|         echo "D> $0 $*"
 | |
|         exit 1
 | |
|         ;;
 | |
| esac
 | |
| 
 | |
| #
 | |
| # Check the argument after any options
 | |
| #
 | |
| if [[ $# -ne 1 ]]; then
 | |
|     echo "$SCRIPT: ${red}Usage: $SCRIPT shownumber${reset}"
 | |
|     exit
 | |
| fi
 | |
| show="$1"
 | |
| 
 | |
| #
 | |
| # I used 'hpr4065' by mistake today, so we need to check the show specification
 | |
| #
 | |
| if [[ $show =~ ^(hpr)?([0-9]+)$ ]]; then
 | |
|     printf -v show '%04d' "${BASH_REMATCH[2]}"
 | |
| else
 | |
|     echo "$SCRIPT: ${red}Invalid show specification: $show${reset}"
 | |
|     exit
 | |
| fi
 | |
| 
 | |
| #
 | |
| # Build the path(s) to the marker(s) in an array
 | |
| #
 | |
| # shellcheck disable=SC2059
 | |
| for fn in "${MK[@]}"; do
 | |
|     printf -v path "$TPL" "$show" "$fn"
 | |
|     MFILES+=("$path")
 | |
| done
 | |
| 
 | |
| #
 | |
| # Path to the show directory
 | |
| #
 | |
| SHOWDIR="$BASENAME/shownotes/hpr${show}"
 | |
| 
 | |
| #
 | |
| # Report the settings in debug mode
 | |
| #
 | |
| _DEBUG "Called as = $0" \
 | |
|        "Show number = $show" \
 | |
|        "MK = ${MK[*]}" \
 | |
|        "state = $state" \
 | |
|        "TOGGLE = $TOGGLE" \
 | |
|        "MFILES = ${MFILES[*]}" \
 | |
|        "SHOWDIR = $SHOWDIR"
 | |
| 
 | |
| #
 | |
| # We need a show directory. If it doesn't exist then we'll create it because
 | |
| # other scripts will use it.
 | |
| #
 | |
| if [[ ! -d $SHOWDIR ]]; then
 | |
|     echo "${red}There is no directory for show $show${reset}"
 | |
| 
 | |
|     #
 | |
|     # If the -Y option was not chosen ask with 'yes_no'. It -Y was chosen
 | |
|     # we're to go ahead regardless. This relies on the fact that Bash
 | |
|     # "short-circuits" logical expressions like this.
 | |
|     #
 | |
|     if [[ $YES -eq 1 ]] || yes_no 'Create directory? %s ' 'N'; then
 | |
|         mkdir "$SHOWDIR"
 | |
|         _silent "${green}Directory created for show $show${reset}"
 | |
|     else
 | |
|         _silent "${yellow}Not changed${reset}"
 | |
|     fi
 | |
| fi
 | |
| 
 | |
| #
 | |
| # If the marker exists and we can toggle it, we'll do so. If no toggle, we
 | |
| # report the marker presence or set it as appropriate.
 | |
| #
 | |
| if exists "${MFILES[@]}"; then
 | |
|     if [[ $TOGGLE -eq 1 ]]; then
 | |
|         _silent "${yellow}Show $show has a '$state' marker${reset}"
 | |
|         if [[ $YES -eq 1 ]] || yes_no 'Remove marker? %s ' 'N'; then
 | |
|             rm -f "${MFILES[@]}"
 | |
|             _silent "${green}Removed '$state' marker for show $show${reset}"
 | |
|         else
 | |
|             _silent "${yellow}Not changed${reset}"
 | |
|         fi
 | |
|     else
 | |
|         echo "${red}Show $show is already marked '$state'${reset}"
 | |
|     fi
 | |
| else
 | |
|     _silent "${yellow}Show $show has no '$state' marker${reset}"
 | |
|     touch "${MFILES[@]}"
 | |
|     _silent "${green}Marked show $show as '$state'${reset}"
 | |
| fi
 | |
| 
 | |
| exit
 | |
| 
 | |
| # vim: syntax=sh:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21
 |