#!/bin/bash

. /tmp/cmdline
. /usr/bin/i2csetup

if [ ${1} ]; then
    I2CCONFIGBUS=${1}
fi

if [ ${2} ]; then
    I2CCONFIGADDRESS=${2}
fi


if [ -z "$(which i2cutil)" ] ; then
    echo "i2crdserial: i2cutil not found"
    exit 1
else
    #read only first card found
    if [ -z "${I2CCONFIGBUS}" ] ; then
        cat "/data/serial.txt"
        exit 0;
    else
        for BUS in ${I2CCONFIGBUS};do 
            i2crdhwserial=$(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) || exit 2
            
            if [ "${i2crdhwserial}" = "0xffffffff" ] ; then
                echo "i2crdhwserial: hwserial not set"
    	        exit 1
            else
    	        printf "%d " $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl)
            fi
        done
    fi

    printf "\n"
fi

