#!/bin/bash if [ ! -f "hashes/list" ] then echo 'ERROR: No list to append' echo 'Use ./create_list.sh instead' exit 1 fi function get_latest_list(){ cat ./hashes/list | jq -r '.latest_list' } latest_list="$(get_latest_list)" echo "Latest list's hash: ${latest_list}" latest_show_object="$(cat hashes/${latest_list} | jq -r '.list' | jq '.[-1:]')" latest_index_number="$(echo ${latest_show_object} | jq -r '.[].index')" latest_starts_on="$(echo ${latest_show_object} | jq -r '.[].starts_on')" latest_duration="$(echo ${latest_show_object} | jq -r '.[].duration')" list_started_on="$(cat hashes/${latest_list} | jq -r '.started_on')" list_duration="$(cat hashes/${latest_list} | jq -r '.duration')" already_in_list="$(mktemp)" time_now="$(date -u +%s)000" new_list_file="$(mktemp)" new_list_started_on="$(($(($(($((${time_now} - ${list_started_on}))/${list_duration}))*${list_duration}))+${list_started_on}))" new_shows="$(mktemp)" cat hashes/${latest_list} | jq -r '.list.[].hash' > ${already_in_list} find hashes -type f -size -4096 | sort | while read filepath do if [ "${filepath}" == "hashes/list" ] then continue fi cat "${filepath}" | jq > /dev/null 2>&1 if [ $? -ne 0 ] then continue fi if [ "$(cat "${filepath}" | jq -r '.type')" == "show" ] then grep $(basename ${filepath}) ${already_in_list} > /dev/null if [ $? -ne 0 ] then echo "New show found: ${filepath}" echo "${filepath}" >> ${new_shows} fi fi done function make_new_list(){ new_show_found="$(cat ${new_shows})" new_show_hash="$(basename ${new_show_found})" new_show_starts_on="$((${latest_duration}+${latest_starts_on}))" new_show_duration="$(cat ${new_show_found}| jq -r '.duration')" ( echo -n '{' echo -n '"type":"list",' echo -n '"started_on":"'${new_list_started_on}'",' echo -n '"list":' echo -n '[' cat hashes/${latest_list} | jq -r '.list.[]' | sed -e 's/}/},/g' | tr -d '\n' echo -n '{' echo -n '"hash":"'${new_show_hash}'",' echo -n '"index":"'$((${latest_index_number}+1))'",' echo -n '"starts_on":"'${new_show_starts_on}'",' echo -n '"duration":"'${new_show_duration}'"' echo -n '}' echo -n '],' echo -n '"duration":"'$((${new_show_duration}+${new_show_starts_on}))'"' echo -n '}' )| jq -c -M > ${new_list_file} sha_live=$(sha512sum ${new_list_file}|cut -d ' ' -f 1) cat ${new_list_file} | jq if [ $? -ne 0 ] then echo "Corrupted list: ${new_list_file}" exit 1 fi echo '{"latest_list":"'${sha_live}'"}' > ./hashes/list mv ${new_list_file} ./hashes/${sha_live} } case "$(cat ${new_shows}|wc -l)" in 0) echo "no new shows";; 1) make_new_list;; *) echo "more than one shows...";break; esac rm ${new_shows} rm ${already_in_list} #cat hashes/${latest_list} | jq -r '.list.[]' | sed -e 's/}/},/g' #cat hashes/${latest_list} | jq