upload_chatlogs.sh

 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
#!/usr/bin/env bash
CHATLOGS_DIR="$HOME/chatlogs";
SAVE_FILE="$HOME/$(basename $0 .sh).save";
TMP_FILE="/tmp/$(date +"%s")_$(basename $0 .sh).tmp";
function refresh_saves() {
local FILES=($@);
test ${#FILES[@]} -gt 0 || return 1;
test ${#FILES[@]} -eq 1 && {
local REG_EXP="${FILES[0]}";
} || {
local REG_EXP="${FILES[0]}$(printf "|%s" "${FILES[@]:1}")";
}
grep -E "$REG_EXP" "$SAVE_FILE" > "$TMP_FILE";
cat "$TMP_FILE" > "$SAVE_FILE";
unlink "$TMP_FILE";
return 0;
}
function get_new_hash() {
local FILE="$1";
echo $(md5sum "$FILE" | awk '{print $1; exit}');
}
function get_old_hash() {
test -z "$1" && return 1;
echo $(awk -v f="$1" 'match($0, f) {print $1; exit};' "$SAVE_FILE");
}
function update_old_hash() {
local FILE="$1";
local NEW_MD5="$2";
grep -q -m 1 "$FILE" "$SAVE_FILE" \
&& sed "s|.*\( .*$FILE\)|$NEW_MD5 \1|g" -i "$SAVE_FILE" \
|| echo "$NEW_MD5 $FILE" >> "$SAVE_FILE";
}
function has_updates() {
local FILE="$1";
local OLD_MD5="$(get_old_hash "$FILE")";
test -r "$FILE" && {
local NEW_MD5="$(get_new_hash "$FILE")";
test "$OLD_MD5" == "$NEW_MD5" || {
# Return new md5 hash
echo $NEW_MD5;
# Return true is file must be updated
return 0;
}
}
return 1;
}
FILE_LIST=(
"$CHATLOGS_DIR/$(date --date="yesterday" +%Y/%m/%d).html"
"$CHATLOGS_DIR/$(date +%Y/%m/%d).html"
);
refresh_saves ${FILE_LIST[@]};
for i in ${FILE_LIST[@]}; do
echo -n "$i - ";
HASH=$(has_updates "$i") && {
echo "Yes";
update_old_hash "$i" "$HASH";
} || {
echo "No";
}
done;
exit 0;