shellcheck, remove old files
[distro-setup] / ziva-screen
1 #!/bin/bash
2
3 # Get screenshots from bow, discard them if they dont change much.
4
5 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
6
7 set -e; . /usr/local/lib/bash-bear; set +e
8
9 dest_dir=/d/ziva-log
10 case $HOSTNAME in
11 sy)
12 dest_dir=/t/zlog
13 ;;
14 esac
15
16 f=/run/user/1000/ziva-tmp.jpg
17
18 install -g 1000 -o 1000 -m 700 $dest_dir
19 cd $dest_dir
20 shopt -s nullglob
21 jpgs=( 20*jpg )
22 if (( ${#jpgs[@]} >= 1 )); then
23 # shellcheck disable=SC2048 # intentional
24 lastf=$(ls -1 20*jpg | tail -n1)
25 fi
26
27 ssh bow DISPLAY=:0 scrot -z $f 2>/dev/null || exit 0
28 rsync --inplace bow:$f $dest_dir 2>/dev/null || exit 0
29 ssh bow rm -f $f
30
31 same=false
32 if [[ $lastf ]]; then
33 # returns 2 on error, but maybe in that case we want to ignore, file the image away
34 # and then it can go away in a subsequent comparison. going with that for now.
35 diff=$(compare -metric MSE $lastf ziva-tmp.jpg /dev/null |& sed 's/[^0-9].*//') ||:
36 if printf "%s\n" "$diff" | grep -E '^[0-9]+$' &>/dev/null && (( diff < 200 )); then
37 same=true
38 fi
39 fi
40
41 if $same; then
42 rm -f ziva-tmp.jpg
43 else
44 mv ziva-tmp.jpg "$(date +%F.%R)".jpg
45 fi