2021-04-20 00:38:47 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
|
|
|
# Do some prep work
|
2021-04-21 02:41:43 +00:00
|
|
|
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 sha1sum >/dev/null 2>&1 || {
|
|
|
|
echo >&2 "We require sha1sum for this script to run, but it's not installed. Aborting."
|
|
|
|
exit 1
|
|
|
|
}
|
2021-04-20 00:38:47 +00:00
|
|
|
|
|
|
|
# make sure we have at least one argument
|
2021-04-21 02:41:43 +00:00
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
echo >&2 "Target folder must be supplied. Aborting."
|
|
|
|
exit 1
|
2021-04-20 00:38:47 +00:00
|
|
|
fi
|
|
|
|
|
2021-04-21 02:41:43 +00:00
|
|
|
# setup: positional arguments to pass in literal variables, query with code
|
|
|
|
jq_args=()
|
2021-04-20 00:38:47 +00:00
|
|
|
jq_query='.'
|
2021-04-21 02:41:43 +00:00
|
|
|
jq_t_args=()
|
2021-04-20 00:38:47 +00:00
|
|
|
jq_t_query='.'
|
|
|
|
|
|
|
|
# counter
|
|
|
|
nr=1
|
|
|
|
|
|
|
|
# target folder
|
|
|
|
target_folder="$1"
|
|
|
|
# tracker counter
|
|
|
|
counter=0
|
2021-04-21 04:09:09 +00:00
|
|
|
each="${2:-1}"
|
2021-04-20 00:38:47 +00:00
|
|
|
|
|
|
|
# check if the folder exist
|
2021-04-21 02:41:43 +00:00
|
|
|
if [ ! -d $target_folder ]; then
|
|
|
|
echo >&2 "Folder $target_folder not found. Aborting."
|
|
|
|
exit 1
|
2021-04-20 00:38:47 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# book names
|
2021-04-21 02:41:43 +00:00
|
|
|
echo "# language translation abbreviation direction filename sha" >"${target_folder}/translations"
|
2021-04-20 00:38:47 +00:00
|
|
|
# checksum
|
2021-04-21 02:41:43 +00:00
|
|
|
echo "# filename sha" >"${target_folder}/checksum"
|
2021-04-20 00:38:47 +00:00
|
|
|
|
|
|
|
for filename in $target_folder/*.json; do
|
|
|
|
# get the abbreviation
|
|
|
|
abbreviation="${filename/.json/}"
|
|
|
|
abbreviation="${abbreviation/${target_folder}\//}"
|
|
|
|
# do not work with the translations or checksum file
|
2021-04-21 02:41:43 +00:00
|
|
|
if [[ "$abbreviation" == 'translations' || "$abbreviation" == 'checksum' ]]; then
|
2021-04-20 00:38:47 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
# do not work with the books or chapters file
|
2021-04-21 02:41:43 +00:00
|
|
|
if [[ "$abbreviation" == 'books' || "$abbreviation" == 'chapters' ]]; then
|
2021-04-20 00:38:47 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
# load the translation in
|
2021-04-21 02:41:43 +00:00
|
|
|
bible=$(cat "${filename}" | jq '.' -a)
|
2021-04-20 00:38:47 +00:00
|
|
|
# update the file formating
|
2021-04-21 02:41:43 +00:00
|
|
|
echo "${bible}" >"${filename}"
|
2021-04-20 00:38:47 +00:00
|
|
|
# build the hash file name
|
|
|
|
hashFileName="${target_folder}/${abbreviation}.sha"
|
|
|
|
# get the hash
|
|
|
|
fileHash=$(sha1sum "${filename}" | awk '{print $1}')
|
|
|
|
# build the return values
|
2023-05-31 14:49:24 +00:00
|
|
|
bible=$(echo "${bible}" | jq ". | del(.books) | del(.discription) | .[\"url\"]=\"https://api.getbible.net/v2/${abbreviation}.json\" | .[\"sha\"]=\"${fileHash}\"" -a)
|
2021-04-20 00:38:47 +00:00
|
|
|
# get the details
|
2021-04-21 02:41:43 +00:00
|
|
|
language=$(echo "${bible}" | jq '.language' -r)
|
|
|
|
translation=$(echo "${bible}" | jq '.translation' -r)
|
|
|
|
direction=$(echo "${bible}" | jq '.direction' -r)
|
2021-04-20 00:38:47 +00:00
|
|
|
# set file details to text file
|
2021-04-21 02:41:43 +00:00
|
|
|
echo "${nr} ${language} ${translation} ${abbreviation} ${direction} ${abbreviation} ${fileHash}" >>"${target_folder}/translations"
|
2021-04-20 00:38:47 +00:00
|
|
|
# load the values for json
|
2021-04-21 02:41:43 +00:00
|
|
|
jq_t_args+=(--arg "key$nr" "$abbreviation")
|
|
|
|
jq_t_args+=(--argjson "value$nr" "$bible")
|
2021-04-20 00:38:47 +00:00
|
|
|
# build query for jq
|
|
|
|
jq_t_query+=" | .[\$key${nr}]=\$value${nr}"
|
|
|
|
# create/update the Bible file checksum
|
2021-04-21 02:41:43 +00:00
|
|
|
echo "${fileHash}" >"$hashFileName"
|
2021-04-20 00:38:47 +00:00
|
|
|
# echo "${fileHash}" > "$_hashFileName"
|
2021-04-21 02:41:43 +00:00
|
|
|
echo "${nr} ${abbreviation} ${fileHash}" >>"${target_folder}/checksum"
|
2021-04-20 00:38:47 +00:00
|
|
|
# load the values for json
|
2021-04-21 02:41:43 +00:00
|
|
|
jq_args+=(--arg "key$nr" "$abbreviation")
|
|
|
|
jq_args+=(--arg "value$nr" "$fileHash")
|
2021-04-20 00:38:47 +00:00
|
|
|
# build query for jq
|
|
|
|
jq_query+=" | .[\$key${nr}]=\$value${nr}"
|
|
|
|
#next
|
2021-04-21 02:41:43 +00:00
|
|
|
nr=$((nr + 1))
|
2021-04-20 00:38:47 +00:00
|
|
|
# check if we have counter upto 98
|
2021-04-21 02:41:43 +00:00
|
|
|
if (("$counter" >= 98)); then
|
2021-04-20 00:38:47 +00:00
|
|
|
counter=70
|
|
|
|
fi
|
|
|
|
# add more
|
2021-04-21 04:09:09 +00:00
|
|
|
counter=$((counter + each))
|
2021-04-20 00:38:47 +00:00
|
|
|
# give notice
|
|
|
|
echo -e "XXX\n${counter}\nDone Hashing $abbreviation\nXXX"
|
|
|
|
done
|
|
|
|
|
|
|
|
# run the generated command with jq
|
2021-04-21 02:41:43 +00:00
|
|
|
jq "${jq_args[@]}" "$jq_query" <<<'{}' >"${target_folder}/checksum.json"
|
|
|
|
jq "${jq_t_args[@]}" "$jq_t_query" <<<'{}' >"${target_folder}/translations.json"
|