#!/usr/bin/env bash
#License: GPL v3
#see <https://www.gnu.org/licenses/>.

#Name: button_hpr_container.sh
#Purpose: build/run HPR Container.
#Version: beta 0.01
#Author: SGOTI (Some Guy On The Internet)
#Email: Lyunpaw@gmail.com
#Date: 2025-04-19

#declaration:
declare bindir="/usr/bin/"
declare podman="${bindir}podman"
declare echo="builtin echo -e"
declare unprivilegedUser="janitor"
declare date="${bindir}date"
declare flags
declare OPTIND
declare -A containerBulidProperties

declare currentMonth
declare nextMonth
declare currentYear

#start:
currentMonth=$(${date} +%m)
currentYear=$(${date} +%Y)

if [[ ${currentmonth} -gt 0 ]] && [[ ${currentmonth} -le 11 ]]; then
    nextMonth="(($(${date} +%m)+01))" #Incomplete: Can return single-digit integer; must be a double-digit integer.
else
    nextMonth="01"
fi

containerBulidProperties=(
    "containerFile" "/path/to/Containerfile"
    "hostMountDir01" "/path/to/project/directory"
    "hostMountDir02" "/tmp/"
    "containerMountDir01" "/opt/hpr/"
    "containerMountDir02" "/tmp/hpr/"
    "containerImageTag" "hpr_image:5.40.1"
    "pullNewImage" "podman pull docker.io/library/perl"
    "recordingDate" "$(${date} -d "${currentYear}/${currentMonth}/01")"
    "recordingTimeStart" "15:00" #TZ: UTC
    "recordingTimeEnd" "17:00" #TZ: UTC
)

function runHPRContainer () {
    local makeEmail #Incomplete: 
    makeEmail="./make_email -month=${containerBulidProperties[recordingDate]} -start=15:00 -end=17:00 -out=/tmp/hpr/%semail.txt"
    local makeShownotes #Incomplete: 
    makeShownotes="/opt/hpr/src/hpr-tools/Community_News/make_shownotes -from=${containerBulidProperties[recordingDate]} -full=/tmp/hpr/%sfull_shownotes.html -mail -comments"

    ${podman} run \
        --mount type=bind,src=${containerBulidProperties[hostMountDir02]},dst=${containerBulidProperties[containerMountDir02]},rw=true \
        --label="Project"="HPR" \
        --label="Forge"="https://repo.anhonesthost.net/HPR/" \
        --interactive \
        --tty \
        --rm=true \
        --privileged=false \
        --pull="never" \
        --cgroups=enabled \
        --cgroupns=private \
        --uts=private \
        --pid=private \
        --memory="32m" \
        --memory-reservation="16m" \
        --hostname="hpr" \
        --name="hpr_project" \
        --user="${unprivilegedUser}" \
        ${containerBulidProperties[containerImageTag]} bash;
    return;
}

function buildNewContainerImage () {
    if [[ -f "${containerBulidProperties[containerFile]}" ]]; then
        ${echo} "Building new container image...\nThis may take several minutes.\
            \nThis is a non interactive build process, so you can return when \
            it's completed.";
        ${podman} build --file="${containerBulidProperties[containerFile]}" --tag="${containerBulidProperties[containerImageTag]}";
    else
        ${echo} 'Dont forget to assign the ${containerBulidProperties[containerFile]} ;). The Containerfile is needed to build the container image.'
    fi
    return;
}

function help () {
    ${echo} "$0 [-hbpq]\n\t[-h] help\n\t[-b] build new container\n\t[-p] pull new perl image\n\t[-q] quit";
    return;
}

while getopts 'hbpq' flags; do
    case "${flags}" in
        h) help; exit 0;
        ;;

        i) ${echo} "Work in progress... :D"; break;
        ;;

        b) buildNewContainerImage;
        ;;

        p) ${containerBulidProperties[pullNewImage]}; exit 0;
        ;;

        e) ${echo} "Work in progress... :D"; break;
        ;;

        s) ${echo} "Work in progress... :D"; break;
        ;;

        q)
        ${echo} "Quitting script."; break;
        ;;

        *)
        ${echo} "Good Heavens! Wrong input."; help; exit 1
        ;;
    esac
done
shift $((OPTIND-1))

if [[ -z ${1} ]]; then help; runHPRContainer; fi;

unset echo
unset flags
unset podman
unset OPTIND
unset currentMonth
unset currentYear
unset containerBulidProperties
exit 0

