happybot/happybot/topic-diff.sh

49 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
. /home/zgrep/offtopiabday/happybot/common.sh
segment() {
sed 's/[a-zA-Z][a-zA-Z]*:\/\/\S*\|./&\n/g' | sed -n '/./p'
}
oldtopic="$(
tac "$serv/$chan/out" \
| awk '{if($3" "$5$6$7=="-!- changedtopicto"){print;exit}}' \
| sed 's/^[^"]*"//;s/"$//'
)"
tail -n 0 -f "$serv/$chan/out" | gawk '{if($3" "$5$6$7=="-!- changedtopicto"){gsub(/^[^"]*"/,"");gsub(/"$/,"");print;fflush("/dev/stdout")}}' | while read -r t; do
if [[ "$t" != "$oldtopic" ]]; then
diff="(diff) "
mode="_"
diff="$(diff -a -d -U 1024 <(var "$oldtopic" | segment) <(var "$t" | segment) \
| tail -n+4 | sed 's/^ /_/;s/^./& _/;s/$/_/' \
| while read -r d l; do
l="$(var "$l" | sed 's/^_//;s/_$//')"
p=""
if [[ "$mode" != "$d" ]]; then
if [[ "$d" == "-" ]]; then
if [[ "$mode" == "+" ]]; then
p="$(echo -ne "\x0305,99")"
else
p="$(echo -ne "\x1f\x0305,99")"
fi
elif [[ "$d" == "+" ]]; then
if [[ "$mode" == "-" ]]; then
p="$(echo -ne "\x0303,99")"
else
p="$(echo -ne "\x1f\x0303,99")"
fi
elif [[ "$d" == "_" ]]; then
p="$(echo -ne "\x0f")"
fi
mode="$d"
fi
diff="$diff$p$l"
var "$diff"
done | tail -n1)"
var "$diff" | zwsp | say
oldtopic="$t"
fi
done