#!/bin/bash


if [ $# -ne 1 ]; then
    echo $0: usage: multiJpegEnc [/path/to/store]
    exit 1
fi

_term() {
	printf "\nterminating processes\n"
	killall gst-launch-1.0
	#kill yourself	
	kill $$
}

trap _term SIGINT

echo "saving to $1, press ctrl-c to cancel"

FPS=6;
JPEG_QUALITY=90;

/home/ubuntu/gst_1.9.1/out/bin/gst-inspect-1.0 ./librxproc_plugin.0.5.arm64.so  --gst-disable-registry-fork --gst-plugin-spew

v4l2-ctl -d /dev/video0 -c delayed_start=1
v4l2-ctl -d /dev/video1 -c delayed_start=1
v4l2-ctl -d /dev/video2 -c delayed_start=1
v4l2-ctl -d /dev/video3 -c delayed_start=1
v4l2-ctl -d /dev/video4 -c delayed_start=1
v4l2-ctl -d /dev/video5 -c delayed_start=1

v4l2-ctl -d /dev/video0 -c singleshot_mode=0
v4l2-ctl -d /dev/video1 -c singleshot_mode=0
v4l2-ctl -d /dev/video2 -c singleshot_mode=0
v4l2-ctl -d /dev/video3 -c singleshot_mode=0
v4l2-ctl -d /dev/video4 -c singleshot_mode=0
v4l2-ctl -d /dev/video5 -c singleshot_mode=0


GST_LAUNCH='/home/ubuntu/gst_1.9.1/out/bin/gst-launch-1.0  --gst-disable-registry-fork --gst-plugin-spew --gst-plugin-path=. '

SOURCE0='v4l2src device=/dev/video0 io-mode=GST_V4L2_IO_USERPTR';
SOURCE1='v4l2src device=/dev/video1 io-mode=GST_V4L2_IO_USERPTR';
SOURCE2='v4l2src device=/dev/video2 io-mode=GST_V4L2_IO_USERPTR';
SOURCE3='v4l2src device=/dev/video3 io-mode=GST_V4L2_IO_USERPTR';
SOURCE4='v4l2src device=/dev/video4 io-mode=GST_V4L2_IO_USERPTR';
SOURCE5='v4l2src device=/dev/video5 io-mode=GST_V4L2_IO_USERPTR';

CAPSFILTER='video/x-bayer,width=3840,height=2160,framerate='$FPS'/1,format=rggb';

PIPE0='rxcudaupload ! rxcudadebayer ! rxcudajpegsink quality='$JPEG_QUALITY' location='$1'cam0_%05d.jpg'
PIPE1='rxcudaupload ! rxcudadebayer ! rxcudajpegsink quality='$JPEG_QUALITY' location='$1'cam1_%05d.jpg'
PIPE2='rxcudaupload ! rxcudadebayer ! rxcudajpegsink quality='$JPEG_QUALITY' location='$1'cam2_%05d.jpg'
PIPE3='rxcudaupload ! rxcudadebayer ! rxcudajpegsink quality='$JPEG_QUALITY' location='$1'cam3_%05d.jpg'
PIPE4='rxcudaupload ! rxcudadebayer ! rxcudajpegsink quality='$JPEG_QUALITY' location='$1'cam4_%05d.jpg'
PIPE5='rxcudaupload ! rxcudadebayer ! rxcudajpegsink quality='$JPEG_QUALITY' location='$1'cam5_%05d.jpg'


# capture
$GST_LAUNCH  $SOURCE0 ! $CAPSFILTER ! $PIPE0 &   
$GST_LAUNCH  $SOURCE1 ! $CAPSFILTER ! $PIPE1 &
$GST_LAUNCH  $SOURCE2 ! $CAPSFILTER ! $PIPE2 &
$GST_LAUNCH  $SOURCE3 ! $CAPSFILTER ! $PIPE3 &	
$GST_LAUNCH  $SOURCE4 ! $CAPSFILTER ! $PIPE4 &  	
$GST_LAUNCH  $SOURCE5 ! $CAPSFILTER ! $PIPE5 &     



while true
do
	#clear
	# ${1::-1} removes last character from $1, otherwise grep won't find it
	DATA=$(df -h | grep ${1::-1})
	printf "%s\r" "$DATA"
	sleep 1
done

