#!/bin/sh
# Copyright (c) 2012-2019 Algo Communication Products Ltd.

# This script automates the creation and archival of the fault pixel table

/etc/init.d/algo-main stop
/etc/init.d/algo-vidsrv stop
/etc/init.d/algo-uieng stop

/opt/algo/bin/bootui fillcolor all white
/opt/algo/bin/bootui showtxt 22 "Generating fault pixel table ..."

# get the mac address
MAC=$(sed 's/://g' /sys/class/net/eth0/address)

# disable the auto exposure so we don't have high gain
# when we capture the raw image
echo "Disabling auto exposure"
echo "0" > /sys/module/mt9v034/aex_enable

# make sure the auto exposure has completely disabled before we capture a frame
sleep 1

cd /tmp

# grab a raw capture and compute faultpixel outputs
/opt/algo/bin/capture-raw
echo "Computing fault pixels - will take approx 60 seconds..."
/opt/algo/bin/calc-fp -l 8 -t 4 -x 752 -y 480 -p 1536 -f image.raw

# create a short filename (no date) that will represent the latest.
shortfilename=faultpixels-${MAC}.off
cp faultpixels.off /opt/algo/cache/faultpixels.off

# copy fault pixel table into NAND flash data partition
if [ ! -c /dev/mtd6 ]; then
	echo "NAND data partition not defined in kernel"
	exit 0
fi

mkdir -p /tmp/nand-data
if ! mount -t vfat /dev/mtdblock6 /tmp/nand-data; then
	mkfs.vfat /dev/mtdblock6
	if ! mount -t vfat /dev/mtdblock6 /tmp/nand-data; then
		echo "Failed to mount NAND data partition"
		exit 1
	fi
fi
cp /tmp/faultpixels.off /tmp/nand-data/
umount /tmp/nand-data

rm image.raw faultpixels.*

# restore everything
# enable the auto exposure again to resume normal operations.
echo "Enabling auto exposure"
echo "1" > /sys/module/mt9v034/aex_enable

/etc/init.d/algo-uieng start
/etc/init.d/algo-vidsrv start
/etc/init.d/algo-main start
