diff --git a/src/today.sh b/src/today.sh index cb9c9162..25c14d03 100755 --- a/src/today.sh +++ b/src/today.sh @@ -1,6 +1,10 @@ #!/bin/bash # Do some prep work +command -v jq >/dev/null 2>&1 || { + echo >&2 "We require jq for this script to run, but it's not installed. Aborting." + exit 1 +} command -v git >/dev/null 2>&1 || { echo >&2 "We require git for this script to run, but it's not installed. Aborting." exit 1 @@ -11,13 +15,49 @@ command -v curl >/dev/null 2>&1 || { } # global config options -DRYRUN=0 +DRY_RUN=0 +# translation version +VERSION="kjv" +# scripture +TODAY_S_SCRIPTURE='' # check if we have options while :; do case $1 in --dry) - DRYRUN=1 + DRY_RUN=1 + ;; + -s | --scripture) # Takes an option argument; ensure it has been specified. + if [ "$2" ]; then + TODAY_S_SCRIPTURE=$2 + shift + else + echo >&2 '"--scripture" requires a non-empty option argument.' + exit 17 + fi + ;; + -s=?* | --scripture=?*) + TODAY_S_SCRIPTURE=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -s= | --scripture=) # Handle the case of an empty --scripture= + echo >&2 '"--scripture=" requires a non-empty option argument.' + exit 17 + ;; + -v | --version) # Takes an option argument; ensure it has been specified. + if [ "$2" ]; then + VERSION=$2 + shift + else + echo >&2 '"--version" requires a non-empty option argument.' + exit 17 + fi + ;; + -v=?* | --version=?*) + VERSION=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -v= | --version=) # Handle the case of an empty --version= + echo >&2 '"--version=" requires a non-empty option argument.' + exit 17 ;; *) # Default case: No more options, so break out of the loop. break ;; @@ -28,6 +68,7 @@ done #██████████████████████████████████████████████████████████████ DATE TODAY ███ # must set the time to Namibian :) TODAY=$(TZ="Africa/Windhoek" date '+%A %d-%B, %Y') +TODAY_FOLDER=$(TZ="Africa/Windhoek" date '+%D') #█████████████████████████████████████████████████████████████ SCRIPT PATH ███ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -36,78 +77,154 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SORTED="$DIR/SCRIPTURE" USED="$DIR/SCRIPTURE_USED" TMP="$DIR/.TMP" +SCRIPTURE_DIR="$DIR/../scripture/${VERSION}/${TODAY_FOLDER}" -#██████████████████████████████████████████████████████████████████ RANDOM ███ -# set a random temp file -sort -R -k1 -b "${SORTED}" >"${TMP}" -# get the first line -VERSE=$(head -n 1 "${TMP}") -# test behaviour -if (("$DRYRUN" == 1)); then - echo "selected: $VERSE" -else - # remove the verse - sed -i -e '1,1d' "${TMP}" - # add to used verses - [ -f "${USED}" ] && echo "$VERSE" >>"${USED}" || echo "$VERSE" >"${USED}" -fi - -#█████████████████████████████████████████████████████ SIX MONTH RETENTION ███ -# check test behaviour -if (("$DRYRUN" == 0)); then - # count the number of verse in used file - LINES_NR=$(wc -l <"${USED}") - if [ "$LINES_NR" -gt 182 ]; then +#█████████████████████████████████████████████████ GET SCRIPTURE FOR TODAY ███ +# only load if not already given +if [ -z "${TODAY_S_SCRIPTURE}" ]; then + #████████████████████████████████████████████████████████████████ RANDOM ███ + # set a random temp file + sort -R -k1 -b "${SORTED}" >"${TMP}" # get the first line - VERSE_BACK=$(head -n 1 "${USED}") - # remove the verse - sed -i -e '1,1d' "${USED}" - # add add back to the pile - echo "$VERSE_BACK" >>"${TMP}" + TODAY_S_SCRIPTURE=$(head -n 1 "${TMP}") + # only if not a test + if (("$DRY_RUN" == 0)); then + # remove the verse + sed -i -e '1,1d' "${TMP}" + # add to used verses + [ -f "${USED}" ] && echo "$TODAY_S_SCRIPTURE" >>"${USED}" || echo "$TODAY_S_SCRIPTURE" >"${USED}" + fi + + #███████████████████████████████████████████████████ SIX MONTH RETENTION ███ + # check test behaviour + if (("$DRY_RUN" == 0)); then + # count the number of verse in used file + LINES_NR=$(wc -l <"${USED}") + if [ "$LINES_NR" -gt 182 ]; then + # get the first line + VERSE_BACK=$(head -n 1 "${USED}") + # remove the verse + sed -i -e '1,1d' "${USED}" + # add add back to the pile + echo "$VERSE_BACK" >>"${TMP}" + fi fi + + #█████████████████████████████████████████████████████████████ SORT BACK ███ + # check test behaviour + if (("$DRY_RUN" == 0)); then + # store back for next time + sort -h -b -k1 "${TMP}" >"${SORTED}" + fi + # remove the temp file + rm -f "${TMP}" fi -#███████████████████████████████████████████████████████████████ SORT BACK ███ -# check test behaviour -if (("$DRYRUN" == 0)); then - # store back for next time - sort -h -b -k1 "${TMP}" >"${SORTED}" +#█████████████████████████████████████████████ SHOW WHAT SCRIPTURE SELECTED ███ +if (("$DRY_RUN" == 1)); then + echo "selected: $TODAY_S_SCRIPTURE" + echo "version: $VERSION" fi -# remove the temp file -rm -f "${TMP}" #███████████████████████████████████████████████████████ GET SCRIPTURE TEXT ███ # Get the verses from the getBible API -VERSES=$(bash <(curl -s https://raw.githubusercontent.com/getbible/getverse/master/src/chapter.sh) "${VERSE}") +TODAY_S_SCRIPTURE_TEXT=$(bash <(curl -s https://raw.githubusercontent.com/getbible/getverse/master/src/chapter.sh) -s="\"${TODAY_S_SCRIPTURE}\"" -v="${VERSION}" ) #███████████████████████████████████████████████████████ GET SCRIPTURE NAME ███ # Get the verses name from the getBible API -NAME=$(bash <(curl -s https://raw.githubusercontent.com/getbible/getverse/master/src/name.sh) "${VERSE}") +NAME=$(bash <(curl -s https://raw.githubusercontent.com/getbible/getverse/master/src/name.sh) -s="\"${TODAY_S_SCRIPTURE}\"" -v="${VERSION}" ) #████████████████████████████████████████████ SET TODAY'S SCRIPTURE IN HTML ███ -HTML="${NAME} -${VERSES//$'\n'/ } +HTML="${NAME}
+${TODAY_S_SCRIPTURE_TEXT//$'\n'/ }

+${TODAY}" + +#████████████████████████████████████████████ SET TODAY'S SCRIPTURE IN JSON ███ +# convert text to json +IFS=$'\n'; TODAY_S_SCRIPTURE_ARRAY=( $TODAY_S_SCRIPTURE_TEXT ) +TODAY_S_SCRIPTURE_JSON='[]' +for line in "${TODAY_S_SCRIPTURE_ARRAY[@]}"; do + # shellcheck disable=SC2001 + text_nr=$(echo "${line}" | sed 's@^[^0-9]*\([0-9]\+\).*@\1@') + text="${line#$text_nr }" + TODAY_S_SCRIPTURE_JSON="$( + jq <<<"$TODAY_S_SCRIPTURE_JSON" -c \ + --arg nr "$text_nr" \ + --arg text "$text" ' + . += [{ + nr: $nr, + text: $text + }] + ' + )" +done +# build the json object +JSON='{}'; JSON="$( + jq <<<"$JSON" -c \ + --arg name "${NAME}" \ + --argjson scripture "${TODAY_S_SCRIPTURE_JSON}" \ + --arg version "${VERSION}" \ + --arg date "${TODAY}" \ + --arg telegram "daily_scripture" \ + --arg source "https://github.com/trueChristian/daily-scripture" ' + { + name: $name, + scripture: $scripture, + version: $version, + date: $date, + telegram: $telegram, + source: $source + } + ' + )" + +#██████████████████████████████████████████████ SET TODAY'S SCRIPTURE IN TG ███ +TG="${NAME} +${TODAY_S_SCRIPTURE_TEXT//$'\n'/ } ${TODAY}" #████████████████████████████████████████ SET TODAY'S SCRIPTURE IN MARKDOWN ███ MARKDOWN="**${NAME}** -${VERSES//$'\n'/ } +${TODAY_S_SCRIPTURE_TEXT//$'\n'/ } [${TODAY}](https://t.me/s/daily_scripture)" #███████████████████████████████████████████████████████████████ SET FILES ███ # check test behaviour -if (("$DRYRUN" == 1)); then +if (("$DRY_RUN" == 1)); then + echo "====================================================" + echo "selected: ${TODAY_S_SCRIPTURE}" + echo "version: ${VERSION}" + echo "====================================================" echo "${HTML}" echo "----------------------------------------------------" + jq <<<"$JSON" -S . + echo "----------------------------------------------------" + echo "${TG}" + echo "----------------------------------------------------" echo "${MARKDOWN}" else - echo "${HTML}" >README.html - echo "${MARKDOWN}" >README.md - + # update the default if this is kjv + if [ "${VERSION}" = 'kjv' ]; then + echo "${HTML}" >README.html + jq <<<"$JSON" -S . >README.json + echo "${TG}" >README.tg + echo "${MARKDOWN}" >README.md + echo "${TODAY_S_SCRIPTURE}" >README.today + fi + # make sure the folders exist + mkdir -p "${SCRIPTURE_DIR}" + # set today's verse to persistent state + echo "${HTML}" >"${SCRIPTURE_DIR}/scripture.html" + jq <<<"$JSON" -S . >"${SCRIPTURE_DIR}/scripture.json" + echo "${TG}" >"${SCRIPTURE_DIR}/scripture.tg" + echo "${MARKDOWN}" >"${SCRIPTURE_DIR}/scripture.md" + echo "${TODAY_S_SCRIPTURE}" >"${SCRIPTURE_DIR}/scripture.today" + # make sure to add new files and folders + git add . git commit -am"${TODAY}" git push fi