aboutsummaryrefslogtreecommitdiff
path: root/append_latest_list.sh
blob: 37546836d2a663ab7e246cfa3e1c8aa2e3b7e4eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/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