#!/bin/sh
#
#	Copyright 2026 (c) Shin-ichi Nagamura.
#	All rights reserved.
#
#	$Id$
#



IMGOPTS_DEFAULT="w=1920&h=1200&design=C5"
FFMPEG_INOPTS="-i pipe:0"
FFMPEG_OUTOPTS="-pix_fmt rgb565le -f fbdev /dev/fb0"

if [ $# -eq 0 ]; then
        echo usage $0 API_URL [IMGOPTS]
        exit 1
fi

APIURL=$1
IMGOPTS="${2:-$IMGOPTS_DEFAULT}"
CURLOPTS="-sk"
FFMPEGOPTS="$FFMPEG_INOPTS $FFMPEG_OUTOPTS"

if [ "$3" = "regza" ]; then
FFMPEG_RGZ="-vf 'scale=1840:1040:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2:black,format=rgb565le'"
FFMPEGOPTS="$FFMPEG_INOPTS $FFMPEG_RGZ $FFMPEG_OUTOPTS"
fi

while true; do
    curl $CURLOPTS -N "$APIURL/u12/photo/session?$IMGOPTS" | \
    while read -r line; do
        filename=$(echo "$line" | sed -n 's/^data: //p')

        if [ -n "$filename" ]; then
            http_code=$(curl $CURLOPTS -w "%{http_code}" -o t.jpeg "$APIURL/u12/photo/$filename")
            if [ "$http_code" = "200" ]; then
                cat t.jpeg | eval ffmpeg $FFMPEGOPTS
            fi
        fi
    done
    sleep 5
done

