#!/bin/bash -
#===============================================================================
#
#         FILE: collect_HPR_database
#
#        USAGE: ./collect_HPR_database
#
#  DESCRIPTION: Collects the SQL dump of the public copy of the HPR database
#               from the website and converts it to SQLite.
#
#      OPTIONS: None
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: There are dependencies on mysql2sqlite and sqlite3. The former
#               comes from https://github.com/mysql2sqlite and is expected to
#               be in the same directory as this script. The sqlite3 package
#               needs to be installed from the repository appropriate to the
#               local OS. It is assumed that wget is available. The script
#               uses auto-deleted temporary files for the MySQL dump, and the
#               dump converted for SQLite.
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.1
#      CREATED: 2025-02-22 16:52:40
#     REVISION: 2025-02-22 17:56:00
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}
BASEDIR=${0%/*}

VERSION="0.0.1"

# {{{ -- Functions: cleanup_temp
#===  FUNCTION  ================================================================
#          NAME:  cleanup_temp
#   DESCRIPTION:  Cleanup temporary files in case of a keyboard interrupt
#                 (SIGINT) or a termination signal (SIGTERM) and at script
#                 exit. Expects to be called from 'trap' so it can just exit
#                 (assuming it's all that's called)
#    PARAMETERS:  * - names of temporary files to delete
#       RETURNS:  Nothing
#===============================================================================
function cleanup_temp {
    for tmp; do
        [ -e "$tmp" ] && rm --force "$tmp"
    done
    exit 0
}
# }}}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#
# Make sure we're where the script lives
#
cd "$BASEDIR" || { echo "$SCRIPT: Failed to cd to $BASEDIR"; exit 1; }

#
# Make temporary files and set traps to delete them
#
TMP1=$(mktemp) || {
    echo "$SCRIPT: creation of temporary file failed!"
    exit 1
}
TMP2=$(mktemp) || {
    echo "$SCRIPT: creation of temporary file failed!"
    exit 1
}
trap 'cleanup_temp $TMP1 $TMP2' SIGHUP SIGINT SIGPIPE SIGTERM EXIT

#
# Definition of files
#
#-------------------------------------------------------------------------------
mysql2sqlite="$BASEDIR/mysql2sqlite"
snapshot_url="https://www.hackerpublicradio.org/hpr.sql"
db_name="hpr.db"
#-------------------------------------------------------------------------------

#
# Sanity check
#
[ -e "$mysql2sqlite" ] || {
    echo "$SCRIPT: Unable to find mandatory script $mysql2sqlite"
    exit 1
}

#
# Collect the SQL dump into a temporary file`
#
if ! wget -q "$snapshot_url" -O "$TMP1"; then
    echo "$SCRIPT: Failed to download from $snapshot_url"
    exit 1
fi

#
# Delete the SQLite database if it exists (otherwise the new data is merged
# with it causing chaos)
#
[ -e "$db_name" ] && rm -f "$db_name"

#
# Convert the MySQL/MariaDB dump. First run sed on it, then run mysql2sqlite
# (from https://github.com/mysql2sqlite) to do the SQL dump conversion. Use
# the result to make a SQLite database.
#
sed '/^DELIMITER ;;/,/^DELIMITER ;/d' "$TMP1" > "$TMP2"
$mysql2sqlite "$TMP2" | sqlite3 "$db_name"

#
# Report success if the new database exists
#
if [[ -e "$db_name" ]]; then
    echo "Created SQLite database '$db_name'"
else
    echo "Failed to find the expected new database '$db_name'"
fi

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