#!/bin/bash -
#===============================================================================
#
#         FILE: generate_tag_reports
#
#        USAGE: ./generate_tag_reports
#
#  DESCRIPTION: Runs 'report_missing_tags' and 'make tags' to generate the
#               pages for the HPR website: report_missing_tags.php and
#               tags.php
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.5
#      CREATED: 2021-01-02 13:02:47
#     REVISION: 2022-09-09 08:05:47
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}
VERSION="0.0.5"

BASEDIR="$HOME/HPR/Database"
cd "$BASEDIR" || { echo "$SCRIPT: Failed to cd to $BASEDIR"; exit 1; }

REPORT="$BASEDIR/make_tag_index"
LIVECFG="$BASEDIR/.hpr_livedb.cfg"    # soft link
# PHPREP="$BASEDIR/report_missing_tags.php"
# CSVREP1="$BASEDIR/tag_summary_actions.csv"
# CSVREP2="$BASEDIR/tags_shows.csv"
# JSONREP="$BASEDIR/tag_data.json"
# HTMLTAGS="$BASEDIR/tags.html"
PHPTAGS="$BASEDIR/tags.php"

#
# Sanity checks
#
[ -e "$REPORT" ] || { echo "$SCRIPT: missing script $REPORT"; exit 1; }
[ -e "$LIVECFG" ] || { echo "$SCRIPT: missing file $LIVECFG"; exit 1; }

if ! tunnel_is_open; then
    echo "Open the tunnel to run this script"
    exit 1
fi

#
# Run the main report with the default template (make_tag_index.tpl)
#
if $REPORT -config="$LIVECFG" -out="$PHPTAGS"; then
    echo "$SCRIPT v$VERSION"
    [ -e "$PHPTAGS" ] && echo "Generated $PHPTAGS"
else
    echo "$SCRIPT: failed to run $REPORT"
    exit 1
fi

#
# Generate the standalone tag list from the output of the report script
#
# if [[ -e $HTMLTAGS ]]; then
#     make tags
#     [ -e "$PHPTAGS" ] && echo "Generated $PHPTAGS"
# else
#     echo "$SCRIPT: missing file $HTMLTAGS"
# fi

exit

# vim: syntax=sh:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21

