#!/usr/bin/env bash

dest_dir="$HOME/tmp/hpr/reserve/"
piper_bin="/opt/bin/piper/piper/piper"      # The location of the text to speech engine
piper_voice="/opt/bin/piper/piper/piper-voices/en/en_US/lessac/medium/en_US-lessac-medium.onnx"

rsync -av --partial --progress  hpr:/docker/users/hpr/hub.hackerpublicradio.org/reserve/ ${dest_dir}

if [ $# -eq 0 ]
then
  working_dir="$( find ${dest_dir} -maxdepth 1 -mindepth 1 -type d -iname "*_*_*" | sort -n | head -1 )"
else
  if [ $1 == "skip" ]
  then
    find ${dest_dir} -maxdepth 1 -mindepth 1 -type d -iname "*_*_*" | sort -n
    exit 1
  fi
  working_dir="$( find ${dest_dir} -maxdepth 1 -mindepth 1 -type d -iname "*_*_*" | sort -n | grep "${1}" | head -1 )"
fi

cd "${working_dir}"

submitted_month="$( \date --date @$( basename $( echo $working_dir  ) | awk -F '_' '{print $1}' ) +"%B %Y" )"

num_reserve_shows="$( find ${dest_dir} -maxdepth 1 -mindepth 1 -type d -iname "*_*_*" |  wc -l )"

if [ "$( ls ${working_dir}/*srt | wc -l )" -ne "1" ]
then
  echo "Can't determine the srt file"
  exit
fi

media_srt="$( ls ${working_dir}/*srt )"
media_txt="${media_srt%.*}.txt"

if [ "${num_reserve_shows}" -ge "10" ]
then
  feedback="You are listening to a show from the reserve pool. This show was submitted in ${submitted_month} to cover occasions like this where there are gaps in the schedule."
  echo "1
00:00:00,000 --> 00:00:03,000
You are listening to a show from the reserve pool.

2
00:00:03,000 --> 00:00:07,000
This show was submitted in ${submitted_month} to cover occasions like this 

2
00:00:07,000 --> 00:00:09,000
where there are gaps in the schedule.
" > ${working_dir}/reserve.srt

elif [ "${num_reserve_shows}" -ge "5" ]
then
  feedback="You are listening to a show from the reserve pool. We have less than two weeks of backup shows available. Now would be a good time to consider submitting a show. This show was submitted in ${submitted_month} to cover occasions like this where there are gaps in the schedule."
  echo "1
00:00:00,000 --> 00:00:03,000
You are listening to a show from the reserve pool.

2
00:00:03,000 --> 00:00:06,000
We have less than two weeks of backup shows available.

3
00:00:06,000 --> 00:00:09,000
Now would be a good time to consider submitting a show.

4
00:00:09,000 --> 00:00:14,000
This show was submitted in ${submitted_month} to cover occasions like this

5
00:00:14,000 --> 00:00:15,000
where there are gaps in the schedule.
" > ${working_dir}/reserve.srt


elif [ "${num_reserve_shows}" -ge "1" ]
then
  feedback="You are listening to a show from the reserve pool. We have very few shows left to cover occasions like this where there are gaps in the schedule. This is a community project that needs listeners to contribute shows in order to survive. Once all shows are posted we will shut down the project, so if you want Hacker Public Radio to continue then record a show. This show was submitted in ${submitted_month}."
  echo "1
00:00:00,000 --> 00:00:03,000
You are listening to a show from the reserve pool.

2
00:00:03,000 --> 00:00:06,000
We have very few shows left to cover occasions like this

3
00:00:06,000 --> 00:00:08,000
where there are gaps in the schedule

4
00:00:08,000 --> 00:00:12,000
This is a community project that needs listeners to contribute shows in order to survive

5
00:00:12,000 --> 00:00:15,000
Once all shows are posted we will shut down the project

6
00:00:15,000 --> 00:00:19,000
So if you want Hacker Public Radio to continue then record a show. 

7
00:00:19,000 --> 00:00:22,000
This show was submitted in ${submitted_month}.
" > ${working_dir}/reserve.srt

else
  feedback="Thank you for listening to Hacker Public Radio. We are shutting down the project. So  long, and thanks for all the fish."
  
  echo "1
00:00:00,000 --> 00:00:03,000
Thank you for listening to Hacker Public Radio.

2
00:00:03,000 --> 00:00:05,000
We are shutting down the project

3
00:00:05,000 --> 00:00:06,000
So  long, and thanks for all the fish.
" > ${working_dir}/reserve.srt
fi

echo "${feedback}" | "${piper_bin}" --model "${piper_voice}" --output_file ${working_dir}/reserve.flac

if [ ! -s "${working_dir}/reserve.flac" ]
then
  echo "Cannot find \"${working_dir}/reserve.flac\""
  exit
fi

itsoffset_intro="$( mediainfo --full --Output=JSON ${working_dir}/reserve.flac | jq --raw-output '.media.track | .[] | select(."@type"=="Audio") | .Duration' | awk -F '.' '{print $1}' )"

if [[ -z "${itsoffset_intro}" || "${itsoffset_intro}" == "null" ]]
then
  echo_error "Could not retrieve the itsoffset_intro to correct the timing of the subtitles."
fi

episode_body_srt="${working_dir}/episode_body.srt"
ffmpeg -hide_banner -loglevel error -y -itsoffset "${itsoffset_intro}" -i "${media_srt}" -c copy "${episode_body_srt}"

itsoffset_intro_reserved="$( grep --max-count 1 ' --> ' "${episode_body_srt}" | awk '{print $1}' )"

mv -v "${media_srt}" "${media_srt}.orig"

cat "${working_dir}/reserve.srt" "${episode_body_srt}" > "${media_srt}"

count=1
cat "${media_srt}" | while read this_line
do
  if [ "$( echo "${this_line}" | grep --count --perl-regexp '^[0-9]+$' )" -eq "1" ]
  then
    echo "${count}"
    count=$((count+1))
  else
    echo "${this_line}"
  fi
done | sponge "${media_srt}"

jq 'del(.episode.Show_Notes, .metadata.Host_IP)' ${working_dir}/shownotes.json 

jq --raw-output '.episode.Show_Notes' ${working_dir}/shownotes.json > ${working_dir}/shownotes.html

kate ${working_dir}/shownotes.html "${media_txt}" "${media_srt}" 
librewolf ${working_dir}/shownotes.html

f ${working_dir}


echo "cd ${working_dir}"
echo "EDIT show in audacity"
echo "RENAME srt to new export name"
echo "SUBMIT show as host"
audacity "${working_dir}/reserve.flac"
echo ""
echo "cd"
echo "mv -v ${working_dir} ${dest_dir}/done/"
echo "ssh hpr \"rm -rvf /docker/users/hpr/hub.hackerpublicradio.org/reserve/$(basename ${working_dir} )\""
echo "ssh hpr \"/docker/users/hpr/userfiles/bin/rename-reserve.bash\""
xdg-open https://whp01.cloud-hosting.io:8443/phpMyAdmin/index.php
echo "DELETE FROM reservations WHERE reservations.key = '$( basename ${working_dir} | awk -F '_' '{print $3}' )';"

