#!/usr/bin/env bash

############################################################################################
# Update db

source settings $1

git_dir="/home/ken/tmp/hpr/hpr_generator"
if [ ! -d "${git_dir}/.git" ]
then
  #git clone gitea@repo.anhonesthost.net:rho_n/hpr_generator.git "${git_dir}"
  git clone gitea@repo.anhonesthost.net:HPR/hpr_generator.git "${git_dir}"
fi

if [ ! -d "${git_dir}" ]
then
  echo_error "The git dir \"${git_dir}\" is missing"
fi

cd "${git_dir}"

git pull

ssh hpr -t "ls -al /docker/users/hpr/userfiles/sql/hpr.sql;md5sum /docker/users/hpr/userfiles/sql/hpr.sql"
ssh hpr -t "/docker/users/hpr/userfiles/sql/hpr_db_backup.bash"
ssh hpr -t "ls -al /docker/users/hpr/userfiles/sql/hpr.sql;md5sum /docker/users/hpr/userfiles/sql/hpr.sql"

#/home/ken/tmp/hpr/hpr_generator/utils/update-hpr-db.sh
./utils/update-hpr-db.sh
if [ $? -ne 0 ]
then
  echo 'Terminating...' >&2
  exit 1
fi

hpr_db="${git_dir}/hpr.db"

if [[ ! -s "${hpr_db}" || -z "${hpr_db}" ]]
then
  echo_error "The \"hpr_db\" variable/file is missing."
fi
if [ "$( file --brief --mime-type "${hpr_db}" | grep --count 'application/vnd.sqlite3' )" -ne "1" ]
then
  echo_error "The \"hpr_db\" variable has not a valid \"application/vnd.sqlite3\" mime type."
fi

############################################################################################
# Get previous show info

row=$(sqlite3 "${hpr_db}" "
SELECT notes
FROM eps
WHERE duration > 0
AND series = 47
ORDER BY date DESC
LIMIT 1;
")

printf "\n%s\n" "SELECT notes
FROM eps
WHERE duration > 0
AND series = 47
ORDER BY date DESC
LIMIT 1;" 1>&2

eval $(
  echo "$row" |
  grep -o '<!-- HPRCN:[^>]*-->' |
  sed -E 's/<!-- HPRCN:(.*) -->/\1/' |
  tr ',' '\n'
)

echo_debug "The last show month was for ${month}, previous_cn_latest_comment: ${latest_comment}, latest_host: ${latest_host}, and previous_cn_latest_episode: ${previous_cn_latest_episode}"

previous_cn_latest_comment="${latest_comment}"
previous_cn_latest_episode="${latest_episode}"

############################################################################################
# Get next show info

this_month=$( \date --date="${month}-01 +1 month" +%Y-%m )

IFS='|' read -r eps_num eps_date <<< "$(sqlite3 "${hpr_db}" "
SELECT id, date
FROM eps
WHERE duration = 0
AND series = 47
ORDER BY date ASC
LIMIT 1;
")"
printf "\n%s\n" "SELECT id, date
FROM eps
WHERE duration = 0
AND series = 47
ORDER BY date ASC
LIMIT 1;" 1>&2

echo_debug "This is going to be hpr${eps_num} for release on ${eps_date}"

eps_dir="/home/ken/processing/x_${eps_num}_${eps_date}_x"
if [ ! -d "${eps_dir}" ]
then
  mkdir -vp "${eps_dir}"
fi

echo_debug "The episode dir is \"${eps_dir}\""

shownotes_html="${eps_dir}/shownotes.html"

############################################################################################
# Add New hosts to shownotes

results=$(sqlite3 "${hpr_db}" "SELECT hostid, host FROM hosts WHERE hostid > ${latest_host};")
printf "\n%s\n" "SELECT hostid, host FROM hosts WHERE hostid > ${latest_host};" 1>&2

echo "<h2>New hosts</h2>" > "${shownotes_html}"
echo "<p>" >> "${shownotes_html}"
if [ -z "${results}" ]
then
  echo "There were no new hosts this month." >> "${shownotes_html}"
  newest_host="${latest_host}"
else
  echo "Welcome to our new hosts: <br>" >> "${shownotes_html}"
  echo "<ul>" >> "${shownotes_html}"
  while IFS="|" read -r hostid host
  do
    newest_host="${hostid}"
    printf -v hostid "%04d" "${hostid}"
    echo "<li><a href=\"https://hackerpublicradio.org/correspondents/${hostid}.html\" target=\"_blank\">${host}</a></li>" >> "${shownotes_html}"
  done <<< "${results}"
  echo "</ul>" >> "${shownotes_html}"
fi
echo "</p>" >> "${shownotes_html}"
echo "" >> "${shownotes_html}"

############################################################################################
# Add Last Month's Shows to shownotes

results=$(sqlite3 -separator '→' "${hpr_db}" "
SELECT eps.id, hosts.host, eps.title, eps.date, eps.hostid
FROM eps, hosts
WHERE eps.id > ${previous_cn_latest_episode}
AND eps.date LIKE \"${this_month}%\"
AND eps.valid = 1
AND eps.hostid = hosts.hostid;
")

printf "\n%s\n" "SELECT eps.id, hosts.host, eps.title, eps.date, eps.hostid
FROM eps, hosts
WHERE eps.id > ${previous_cn_latest_episode}
AND eps.date LIKE \"${this_month}%\"
AND eps.valid = 1
AND eps.hostid = hosts.hostid;" 1>&2

echo "<h2>Last Month's Shows</h2>" >> "${shownotes_html}"
echo "" >> "${shownotes_html}"
# [%# The id 't01' is in the HPR CSS but might give trouble on the IA -%]
echo "<table id=\"t01\">" >> "${shownotes_html}"
echo "<tr>" >> "${shownotes_html}"
echo "<th>Id</th>" >> "${shownotes_html}"
echo "<th>Day</th>" >> "${shownotes_html}"
echo "<th>Date</th>" >> "${shownotes_html}"
echo "<th>Title</th>" >> "${shownotes_html}"
echo "<th>Host</th>" >> "${shownotes_html}"
echo "</tr>" >> "${shownotes_html}"
if [ -z "${results}" ]
then
  echo_error "There were no shows found for this month."
else
  while IFS="→" read -r eps_id host title eps_date hostid
  do
    printf -v hostid "%04d" "${hostid}"
    printf -v eps_id "%04d" "${eps_id}"
    echo "<tr>" >> "${shownotes_html}"
    echo "<td><strong><a href=\"https://hackerpublicradio.org/eps/hpr${eps_id}/index.html\" target=\"_blank\">${eps_id}</a></strong></td>" >> "${shownotes_html}"
    echo "<td>$( \date --date=${eps_date} +%a )</td>" >> "${shownotes_html}"
    echo "<td class=\"shrink\">${eps_date}</td>" >> "${shownotes_html}"
    echo "<td><a href=\"https://hackerpublicradio.org/eps/hpr${eps_id}/index.html\" target=\"_blank\">${title}</a></td>" >> "${shownotes_html}"
    echo "<td><a href=\"https://hackerpublicradio.org/correspondents/${hostid}.html\" target=\"_blank\">${host}</a></td>" >> "${shownotes_html}"
    latest_episode=${eps_id}
    echo "</tr>" >> "${shownotes_html}"
  done <<< "${results}"
fi
echo "</table>" >> "${shownotes_html}"
echo "" >> "${shownotes_html}"

############################################################################################
# Add Comments to shownotes


function render_comments() {

  results=${1}

  if [ -z "${results}" ]
  then
    echo_error "There were no comments found for this month."
  else
    this_comment="0"
    open_line="0"
    echo "<ul>" >> "${shownotes_html}"
    while IFS="→" read -r comments_id comments_eps_id eps_date eps_title hosts_host hosts_hostid comments_comment_author_name comments_comment_timestamp comments_comment_title
    do
      if [ "${this_comment}" -ne "${comments_eps_id}" ]
      then
        if [ "${open_line}" -ne "0" ]
        then
          echo "</ul>" >> "${shownotes_html}"
        fi
        open_line="1"
        this_comment="${comments_eps_id}"
        printf -v hosts_hostid "%04d" "${hosts_hostid}"
        printf -v comments_eps_id "%04d" "${comments_eps_id}"
        echo "<li><strong><a href=\"https://hackerpublicradio.org/eps/hpr${comments_eps_id}/index.html#comments\" target=\"_blank\">hpr${comments_eps_id}</a></strong> (${eps_date}) \"<em>${eps_title}</em>\" by <a href=\"https://hackerpublicradio.org/correspondents/${hosts_hostid}.html\" target=\"_blank\">${hosts_host}</a>.<br></li>" >> "${shownotes_html}"
        echo "<ul>" >> "${shownotes_html}"
      fi
      echo "<li style=\"list-style: none; display: inline\"><strong>${comments_comment_author_name}</strong> said: \"<em><a href=\"https://hackerpublicradio.org/eps/hpr${comments_eps_id}/index.html#comment_${comments_id}\" target=\"_blank\">${comments_comment_title}</a></em>\" (${comments_comment_timestamp})<br/></li>" >> "${shownotes_html}"
      latest_comment="${comments_id}"
    done <<< "${results}"
    echo "</ul>" >> "${shownotes_html}"
    echo "</ul>" >> "${shownotes_html}"
  fi
}

# -------------------------------------------

echo "<h2>Comments this month</h2>" >> "${shownotes_html}"
echo "" >> "${shownotes_html}"

echo "<h3>Past shows</h3>" >> "${shownotes_html}"

previous_cn_latest_comment_last_changed=$(sqlite3 "${hpr_db}" "SELECT last_changed FROM comments WHERE comments.id = ${previous_cn_latest_comment};")
printf "\n%s\n" "SELECT last_changed FROM comments WHERE comments.id = ${previous_cn_latest_comment};" 1>&2

results=$(sqlite3 -separator '→' "${hpr_db}" "
SELECT
  comments.id,
  comments.eps_id,
  eps.date,
  eps.title,
  hosts.host,
  hosts.hostid,
  comments.comment_author_name,
  comments.comment_timestamp,
  comments.comment_title
FROM comments
JOIN eps   ON comments.eps_id = eps.id
JOIN hosts ON eps.hostid = hosts.hostid
WHERE comments.id > "${previous_cn_latest_comment}"
  AND eps.id <= "${previous_cn_latest_episode}"
ORDER BY eps.date;
")
printf "\n%s\n" "
SELECT
  comments.id,
  comments.eps_id,
  eps.date,
  eps.title,
  hosts.host,
  hosts.hostid,
  comments.comment_author_name,
  comments.comment_timestamp,
  comments.comment_title
FROM comments
JOIN eps   ON comments.eps_id = eps.id
JOIN hosts ON eps.hostid = hosts.hostid
WHERE comments.id > "${previous_cn_latest_comment}"
  AND eps.id <= "${previous_cn_latest_episode}"
ORDER BY eps.date;
" 1>&2

render_comments "${results}"

echo "<h3>This month's shows</h3>" >> "${shownotes_html}"

results=$(sqlite3 -separator '→' "${hpr_db}" "
SELECT
  comments.id,
  comments.eps_id,
  eps.date,
  eps.title,
  hosts.host,
  hosts.hostid,
  comments.comment_author_name,
  comments.comment_timestamp,
  comments.comment_title
FROM comments
JOIN eps   ON comments.eps_id = eps.id
JOIN hosts ON eps.hostid = hosts.hostid
WHERE eps.date LIKE \"${this_month}-%\"
ORDER BY eps.date;
")
printf "\n%s\n" "SELECT
  comments.id,
  comments.eps_id,
  eps.date,
  eps.title,
  hosts.host,
  hosts.hostid,
  comments.comment_author_name,
  comments.comment_timestamp,
  comments.comment_title
FROM comments
JOIN eps   ON comments.eps_id = eps.id
JOIN hosts ON eps.hostid = hosts.hostid
WHERE eps.date LIKE \"${this_month}-%\"
ORDER BY eps.date;" 1>&2

render_comments "${results}"

############################################################################################
# Add maillist and cal to shownotes

mailman_link="https://lists.hackerpublicradio.com/pipermail/hpr/$( \date -d"${month}-01 next month" +%Y-%B )/thread.html"
echo "<h2>Mailing List discussions</h2>" >> "${shownotes_html}"
echo "<p>Policy decisions surrounding HPR are taken by the community as a whole. This
discussion takes place on the <a href=\"https://lists.hackerpublicradio.com/mailman/listinfo/hpr\" target=\"_blank\">Mailing List</a>
which is open to all HPR listeners and contributors. The discussions are open
and available on the HPR server under <a href=\"https://lists.hackerpublicradio.com/pipermail/hpr\">Mailman</a>.
</p>
<p>The threaded discussions this month can be found here:</p>
<a href=\"${mailman_link}\" target=\"_blank\">${mailman_link}</a>

<h2>Events Calendar</h2>
<p>With the kind permission of <strong>LWN.net</strong> we are linking to
<a href=\"https://lwn.net/Calendar/\" target=\"_blank\">The LWN.net Community Calendar</a>.</p>
<p>Quoting the site:</p>
<blockquote>This is the LWN.net community event calendar, where we track
events of interest to people using and developing Linux and free software.
Clicking on individual events will take you to the appropriate web
page.</blockquote>" >> "${shownotes_html}"

latest_comment=$(sqlite3 "${hpr_db}" "SELECT MAX(id) FROM comments;")
printf "\n%s\n" "" 1>&2
echo "<!-- HPRCN:month=${this_month},latest_comment=${latest_comment},latest_host=${newest_host},latest_episode=${latest_episode} -->" >> "${shownotes_html}"

cp -v "${shownotes_html}" "${shownotes_html%.*}"_edited.html
ls -al "${eps_dir}"
