#!/bin/sh

log() {
	echo "$1"
}

# AutoRun Recovery
if [ $# -lt 3 ]
then
	log "Verify input argument fail!"
	log "Usage: recovery.sh [I2C_BUS] [I2C_ID] [CAM_NAME]"
	log "    [I2C_BUS]: I2C bus index, 10 is a example"
	log "    [I2C_ID]: I2C device address, 0x48 is a example (hex)"
	log "    [CAM_NAME]: Camera product name, as follows:"
	log "                - TEVS-AR0144"
	log "                - TEVS-AR0145-M"
	log "                - TEVS-AR0234"
	log "                - TEVS-AR0521"
	log "                - TEVS-AR0522"
	log "                - TEVS-AR0522-M"
	log "                - TEVS-AR0821"
	log "                - TEVS-AR0822"
	log "                - TEVS-AR0822-M"
	log "                - TEVS-AR1335"
	exit 1
fi

NOW_PATH=$(pwd)

I2C_BUS=$1
RAW_ID=$2
CAM_NAME=$3

case "$RAW_ID" in
    0x*|0X*)
        I2C_ID_HEX="${RAW_ID#0x}"
        I2C_ID="${I2C_ID_HEX#0X}"
        ;;
    *)
        I2C_ID="${RAW_ID#0X}"
        ;;
esac

CAM_VER="25.10.00.01"

case "$CAM_NAME" in
	"TEVS-AR0144")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0144_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR0145-M")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0145m_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR0234")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0234_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR0521")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0521_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR0522")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0522_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR0522-M")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0522m_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR0821")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0821_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR0822")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0822_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR0822-M")
		BIN_FILE="$NOW_PATH/bin/tevs_ar0822m_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	"TEVS-AR1335")
		BIN_FILE="$NOW_PATH/bin/tevs_ar1335_MSPM0G3507_nortos_ticlang_w_map_$CAM_VER.bin"
		;;
	*)
		log "Unknow camera product name"
		exit 1
		;;
esac

# resource check
if [ ! -f "$BIN_FILE" ]; then
    log "Bin file not found: $BIN_FILE"
    exit 1
fi

log "TechNexion TEVS camera recovery tool for RPi5"
log "TEVS firmware version $CAM_VER"

cam0=0
cam1=0
if [ -L /sys/bus/i2c/devices/10-0048/driver ]; then
	log "Camera0 found"
	cam0=1
else
	log "Camera0 cannot found"
fi
if [ -L /sys/bus/i2c/devices/11-0048/driver ]; then
	log "Camera1 found"
	cam1=1
else
	log "Camera1 cannot found"
fi

cam0_expander=0
cam1_expander=0
if [ -L /sys/bus/i2c/devices/10-0027/driver ]; then
	log "Camera0 found RPI22 adaptor"
	cam0_expander=1
else
	log "Camera0 cannot found RPI22 adaptor"
fi
if [ -L /sys/bus/i2c/devices/11-0027/driver ]; then
	log "Camera1 found RPI22 adaptor"
	cam1_expander=1
else
	log "Camera1 cannot found RPI22 adaptor"
fi

if [ "$cam0" -eq 1 ] && [ "$I2C_BUS" -eq 10 ]; then
	log "Camera0 is working, please update the camera firmware using the usual methods."
	exit 0
elif [ "$cam1" -eq 1 ] && [ "$I2C_BUS" -eq 11 ]; then
	log "Camera1 is working, please update the camera firmware using the usual methods."
	exit 0
fi

if [ "$cam0_expander" -eq 1 ] && [ "$cam1_expander" -eq 1 ]; then
	vizion-ctl -fu sensor="$BIN_FILE" \
	  --i2c-bus "$I2C_BUS" \
	  --slave-id "`printf '%d' 0x$I2C_ID`" \
	  --bsl-hook '
		if [ "$I2C_BUS" -eq 10 ]; then
			pinctrl set 34 op dl
			timeout 0.1s gpioset -c 15 6=1
			pinctrl set 34 op dh
			sleep 0.1
			timeout 0.1s gpioset -c 15 6=0
		elif [ "$I2C_BUS" -eq 11 ]; then
			pinctrl set 46 op dl
			timeout 0.1s gpioset -c 14 6=1
			pinctrl set 46 op dh
			sleep 0.1
			timeout 0.1s gpioset -c 14 6=0
		fi' \
	  --recovery
elif [ "$cam0_expander" -eq 1 ] && [ "$cam1_expander" -eq 0 ]; then
	if [ "$I2C_BUS" -eq 10 ]; then
		vizion-ctl -fu sensor="$BIN_FILE" \
		  --i2c-bus "$I2C_BUS" \
		  --slave-id "`printf '%d' 0x$I2C_ID`" \
		  --bsl-hook '
			pinctrl set 34 op dl
			timeout 0.1s gpioset -c 14 6=1
			pinctrl set 34 op dh
			sleep 0.1
			timeout 0.1s gpioset -c 14 6=0' \
		  --recovery
	else
		log "Please connect Camera1 with RPI22 adaptor"
		exit 0
	fi
elif [ "$cam0_expander" -eq 0 ] && [ "$cam1_expander" -eq 1 ]; then
	if [ "$I2C_BUS" -eq 11 ]; then
		vizion-ctl -fu sensor="$BIN_FILE" \
		  --i2c-bus "$I2C_BUS" \
		  --slave-id "`printf '%d' 0x$I2C_ID`" \
		  --bsl-hook '
			pinctrl set 46 op dl
			timeout 0.1s gpioset -c 14 6=1
			pinctrl set 46 op dh
			sleep 0.1
			timeout 0.1s gpioset -c 14 6=0' \
		  --recovery
	else
		log "Please connect Camera0 with RPI22 adaptor"
		exit 0
	fi
fi
