#!/usr/bin/env bash
# Copyright Ken Fallon - Released into the public domain. http://creativecommons.org/publicdomain/ 
#============================================================

working_dir="$HOME/tmp/hpr/hpr_generator/sourcecode"
hpr_sql_rss="${working_dir}/hpr_sql_rss.xml"
previous_update_txt="${working_dir}/last_update.txt"

if [ ! -d "${working_dir}/.git" ]
then
  mkdir --parents --verbose "${working_dir}"
  git clone gitea@repo.anhonesthost.net:HPR/hpr_generator.git "${working_dir}"
fi

cd "${working_dir}"
git pull

function get_latest_update_time() {
  if [ "$( curl --silent --netrc-file ${HOME}/.netrc --write-out '%{http_code}' https://hackerpublicradio.org/hpr.sql.rss --output "${hpr_sql_rss}" )" != 200 ]
  then
    echo "Could not get a list of the queue status from \"https://hackerpublicradio.org/hpr.sql.rss\""
  fi

  if [[ ! -s "${hpr_sql_rss}" || -z "${hpr_sql_rss}" ]]
  then
    echo "The \"hpr_sql_rss\" variable/file is missing."
    exit
  fi
  if [ "$( file --brief --mime-type "${hpr_sql_rss}" | grep --count 'text/xml' )" -ne "1" ]
  then
    echo "The \"hpr_sql_rss\" variable has not a valid \"text/xml\" mime type."
    exit
  fi
  xmllint --format "${hpr_sql_rss}" >/dev/null 2>&1
  if [ $? -ne 0 ]
  then
    echo "The file \"${hpr_sql_rss}\" is not valid xml."
    exit
  fi

  current_update_iso8601="$( \date --utc --date="$( xmlstarlet sel --text --template --match '/rss/channel/pubDate' --value-of '.' --nl "${hpr_sql_rss}" )" +%Y-%m-%dT%H:%M:%SZ )"
  if [ -z "${current_update_iso8601}" ]
  then
    echo "The \"current_update_iso8601\" variable is missing."
    exit
  fi

  current_update_epoch="$( \date --utc --date="${current_update_iso8601}" +%s )"
  if [ -z "${current_update_epoch}" ]
  then
    echo "The \"current_update_epoch\" variable/file is missing."
    exit
  fi
  echo -e "Current update time is\t${current_update_iso8601} (${current_update_epoch})"

  if [ -s "${previous_update_txt}" ]
  then
    #echo "Found the last update file \"${previous_update_txt}\""
    previous_update_iso8601="$( \date --utc --date="$( cat "${previous_update_txt}" )" +%Y-%m-%dT%H:%M:%SZ )"
    previous_update_epoch="$( \date --utc --date="$( cat "${previous_update_txt}" )" +%s )"
  else
    #echo "Did not find the last update file \"${previous_update_txt}\""
    previous_update_iso8601=""
    previous_update_epoch="0"
  fi

  echo -e "Previous update time is\t${previous_update_iso8601} (${previous_update_epoch})"

  echo "${current_update_iso8601}" > "${previous_update_txt}"

}

get_latest_update_time


# if [ "${current_update_epoch}" -eq "previous_update_epoch"
# exit

# ssh hpr -t "ls -al /home/hpr/www/hpr.sql;md5sum /home/hpr/www/hpr.sql"
# ssh hpr -t "/home/hpr/bin/hpr_db_backup.bash"
# ssh hpr -t "ls -al /home/hpr/www/hpr.sql;md5sum /home/hpr/www/hpr.sql"

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

./site-generator --all --verbose
if [ $? -ne 0 ]
then
  echo 'Terminating...' >&2
  exit 1
fi

cd "${working_dir}/public_html"

for feed in comments.rss
do
  xmllint --format "${feed}" >/dev/null 2>&1
  if [ "${?}" -ne "0" ]
  then
    echo "Error: The rss feed \"${feed}\" is not correct"
    xmllint --format "${feed}"
    exit 1
  fi
done
    
cd "${working_dir}"

rsync -av --partial --progress "${working_dir}/public_html/" hpr:hackerpublicradio.org/public_html
#rsync -av --partial --progress "${working_dir}/public_html/" hobbypublicradio.org:hobbypublicradio.org/

# # # cd $HOME/sourcecode/hpr/hpr_hub/
# # # git pull 
# # # cd $HOME/sourcecode/hpr/hpr_hub/sql
# # # split --hex-suffixes --lines=1000 --additional-suffix=.sql hpr.sql hpr-db-part-
# # # cd $HOME/sourcecode/hpr/hpr_hub/
# # # git add $HOME/sourcecode/hpr/hpr_hub/sql/hpr*sql 
# # # git commit -m "$(\date -u +%Y-%m-%d_%H-%M-%SZ_%A ) database changed"
# # # git push
# # # #xdg-open https://hackerpublicradio.org/
