#!/bin/bash

# Environments
export GST_PLUGIN_PATH=/usr/lib/gstreamer-1.0
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib
export LIBVA_DRIVER_NAME=iHD
export GST_GL_PLATFORM=egl
export DISPLAY=:0
export XAUTHORITY=$(find /run/user/$UID/ -name ".mutter-Xwaylandauth.*" 2>/dev/null)
export vblank_mode=1
# export GST_DEBUG="icamerasrc:7"
# export cameraDebug=7
# export GST_DEBUG_DUMP_DOT_DIR=./gst_debug/

DEV_NAME=${1:-"tevs-ar0144"}

DEFAULT_RES="640x480"
DEFAULT_FMT="UYVY"
CAM_RES="${2:-$DEFAULT_RES}"
IFS='x' read -r -a RES_ARRAY <<< "$CAM_RES"
# VIDEO format setting
VIDEO_WIDTH=${RES_ARRAY[0]}
VIDEO_HEIGHT=${RES_ARRAY[1]}

CAM_FMT="${3:-$DEFAULT_FMT}"

echo "device name: $DEV_NAME"
echo "width: $VIDEO_WIDTH, height: $VIDEO_HEIGHT, format=$CAM_FMT"

DEFAULT_TEST_MODE="CAM_PREVIEW"
TEST_MODE="${4:-$DEFAULT_TEST_MODE}"

echo $TEST_MODE

if [ "$TEST_MODE" = "MULTICAMx2-CSI0" ]; then
   echo "Start $TEST_MODE Preview...."
   gst-launch-1.0 \
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=2 scene-mode=auto \
    device-name=$DEV_NAME-1 ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position \
\
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=2 scene-mode=auto \
    device-name=$DEV_NAME-2 ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position

elif [ "$TEST_MODE" = "MULTICAMx2-CSI1" ]; then
   echo "Start $TEST_MODE Preview...."
   gst-launch-1.0 \
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=2 scene-mode=auto \
    device-name=$DEV_NAME-3 ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position \
\
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=2 scene-mode=auto \
    device-name=$DEV_NAME-4 ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position
elif [ "$TEST_MODE" = "MULTICAMx4" ]; then
   echo "Start $TEST_MODE Preview...."
   gst-launch-1.0 \
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=4 scene-mode=auto \
    device-name=$DEV_NAME-1 ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position \
\
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=4 scene-mode=auto \
    device-name=$DEV_NAME-2 ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position \
\
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=4 scene-mode=auto \
    device-name=$DEV_NAME-3 ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position \
\
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=4 scene-mode=auto \
    device-name=$DEV_NAME-4 ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position
else
   echo "Start Camera Preview...."
   gst-launch-1.0 \
    icamerasrc num-buffers=-1 printfps=true io-mode=dma_mode num-vc=1 scene-mode=auto \
    device-name=$DEV_NAME ! \
    "video/x-raw(memory:DMABuf),drm-format=$CAM_FMT,width=$VIDEO_WIDTH,height=$VIDEO_HEIGHT" ! queue ! \
    glupload ! glcolorconvert ! gldownload ! xvimagesink sync=false --no-position
fi