#!/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 
            i2crdserial=$(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) || exit 2
            
            if [ "${i2crdserial}" = "0xffffffff" ] ; then
    	        echo "serial not set"
    	        exit 1
            else
    	        printf "%d " $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl)
            fi
        done
    fi     
    printf "\n"
   
fi

