#! /bin/bash

set -euo pipefail
cd "$(dirname "$(realpath "$BASH_SOURCE")")"

mkdir -p ./record.d/

for i in 83 98; do
   (
      while sleep 1; do
         mkdir -p ./record.d/$i/
         /var/services/homes/squeaky2x3/bin/ffmpeg7 \
            -y \
            -rtsp_transport udp \
            -i rtsp://192.168.0.$i:8554/unicast \
            -t 180 \
            ./record.d/$i/$(date +%Y-%m-%dT%H-%M-%S).%05d.jpg \
            < /dev/null &> ./record.d/$i/ffmpeg.log
      done
   ) &
done

cleanup() {
   local jobs="$(jobs -p)"
   if [ -n "$jobs" ]; then
      echo -e "killing $jobs"
      kill -9 $jobs
      wait
   fi
}
trap cleanup EXIT
echo -e "waiting..."
wait
