#!/bin/bash

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

printProductDataList () {
    i2cdetect -a -y ${1} ${I2CCONFIGADDRESS} ${I2CCONFIGADDRESS} | grep -q '\-\-'

    if [ $? -eq 1 ]; then
        t=$(printf "%08x" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 0 bl))
        BOARDTYPE=${t:2:2}
        TT=${t:4:4}
        TYPE=${t:4:2}
        SUBTYPE=${t:6:2}

        if [ ${BOARDTYPE} = '02' ] ; then
            subtype=$(i2crdsubtype)
            if [ "${subtype}" = "80" ] ; then
                echo -n "OEM OfficeMaster Gate Advanced"
            elif [ "${subtype}" = "02" ] ; then
                echo -n "OfficeMaster Gate Advanced with license"
            else
                echo -n "OfficeMaster Gate Advanced without license"
            fi
        elif [ ${BOARDTYPE} = '01' ] ; then
            subtype=$(i2crdsubtype)
            if [ "${subtype}" = "80" ] ; then
                echo -n "OEM QuadE1"
            elif [ "${subtype}" = "02" ] ; then
                echo -n "QuadE1 with license"
            else
                echo -n "QuadE1 without license"
            fi
        elif [ ${BOARDTYPE} = '00' ] ; then 
            if [ ${TT} = '0100' ] ; then
                echo -n "Single Unit with 1 S0 interface"
            elif [ ${TT} = '0101' ] ; then
                echo -n "Single Unit with 4 S0 interfaces"
            elif [ ${TT} = '0102' ] ; then
                echo -n "Single Unit with 4 S0 interfaces and with license"
            elif [ ${TT} = '0200' ] ; then
                echo -n "S2Me without license"
            elif [ ${TT} = '0201' ] ; then
                echo -n "S2Me with relay and without license"
            elif [ ${TT} = '0202' ] ; then
                echo -n "S2Me with license"
            elif [ ${TT} = '0203' ] ; then
                echo -n "S2Me with relais and with license"
            elif [ ${TT} = '0280' ] ; then
                echo -n "OEM S2Me"
            elif [ ${TT} = '0300' ] ; then
                echo -n "MGB without license"
            elif [ ${TT} = '0302' ] ; then
                echo -n "MGB with license"
            elif [ ${TT} = '0380' ] ; then
                echo -n "OEM MGB"
            fi
        else
            echo -n "noname"
        fi

        echo -n ",${TYPE},${SUBTYPE}"

        OMGREVISION=$(printf "%01x\n" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 7 b))
        echo -n ",${OMGREVISION}"

        OMGSERIAL=$(printf "%d\n" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 8 bl))
        echo -n ",${OMGSERIAL}"

        OMGHWSERIAL=$(printf "%d\n" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 12 bl))
        echo -n ",${OMGHWSERIAL}"

        ARTICLENR=$(printf "%d\n" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 16 bl))
        ARTICLENRHEX=$(printf "%08x\n" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 16 bl))
        if [ "${ARTICLENRHEX}" = "ffffffff" ] ; then
            echo -n ",0"
        else
            echo -n ",${ARTICLENR}"
        fi

        LINES=$(i2ccalclines -b ${1})
        echo -n ",${LINES}"

        LINES=$(i2ccalclines -b ${1} -d)
        echo -n ",${LINES}"

        echo -n ",$(articletoname ${ARTICLENR})"

        
    fi
}

printDeviceDataList () {
    i2cdetect -a -y ${1} ${2} ${2} | grep -q '\-\-'
    rc=$?
    if [ $rc -eq 1 ]; then

        TT=$(i2crdtype ${1} ${2} ${3})
        subtype=$(i2crdsubtype ${1} ${2} ${3})

        if [ ${TT} = '01' ] ; then
            if [ "${subtype}" = "00" ] ; then
                echo -n "::1S0"
            else
                echo -n "::4S0"
            fi

        elif [ ${TT} = '02' ] ; then
            echo -n "::2S2M"

        elif [ ${TT} = '04' ] ; then
            echo -n "::2S2M"

        elif [ ${TT} = '05' ] ; then
            echo -n "::4BRI"

        elif [ ${TT} = '06' ] ; then
            echo -n "::4FXS"
        elif [ ${TT} = '07' ] ; then
            echo -n "::2FXS"

        else
            echo -n "::noname"
        fi

        echo -n ",${TT},${subtype}"

        OMGREVISION=$(printf "%01x\n" $(i2cutil -y -r ${1} ${2} 7 b))
        echo -n ",${OMGREVISION}"

        OMGSERIAL=$(printf "%d\n" $(i2cutil -y -r ${1} ${2} 8 bl))
        echo -n ",${OMGSERIAL}"

        OMGHWSERIAL=$(printf "%d\n" $(i2cutil -y -r ${1} ${2} 12 bl))
        echo -n ",${OMGHWSERIAL}"

        ARTICLENR=$(printf "%d\n" $(i2cutil -y -r ${1} ${2} 16 bl))
        ARTICLENRHEX=$(printf "%08x\n" $(i2cutil -y -r ${1} ${2} 16 bl))
        if [ "${ARTICLENRHEX}" = "ffffffff" ] ; then
            echo -n ",0"
        else
            echo -n ",${ARTICLENR}"
        fi

        if [ -z "${3}" ]; then
            OFFSET="0"
        else
            OFFSET=${3}
        fi

        LINES=$(i2ccalclines -b ${1} -a ${2} -o ${OFFSET})
        echo -n ",${LINES}"

        LINES=$(i2ccalclines -d -b ${1} -a ${2} -o ${OFFSET})
        echo -n ",${LINES}"

        echo -n ",$(articletoname ${ARTICLENR})"
    fi
}

printProductData () {
    i2cdetect -a -y ${1} ${I2CCONFIGADDRESS} ${I2CCONFIGADDRESS} | grep -q '\-\-'
    if [ $? -eq 1 ]; then
        t=$(printf "%08x" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 0 bl))
        BOARDTYPE=${t:2:2}
        TT=${t:4:4}

        if [ ${BOARDTYPE} = '02' ] ; then
            subtype=$(i2crdsubtype)
            if [ "${subtype}" = "80" ] ; then
                echo "OEM AtomBox"
            elif [ "${subtype}" = "02" ] ; then
                echo "AtomBox with license"
            else
                echo "AtomBox without license"
            fi
        elif [ ${BOARDTYPE} = '01' ] ; then
            subtype=$(i2crdsubtype)
            if [ "${subtype}" = "80" ] ; then
                echo "OEM QuadE1"
            elif [ "${subtype}" = "02" ] ; then
                echo "QuadE1 with license"
            else
                echo "QuadE1 without license"
            fi
        elif [ ${BOARDTYPE} = '00' ] ; then
            if [ ${TT} = '0100' ] ; then
                echo "Single Unit with 1 S0 interface"
            elif [ ${TT} = '0101' ] ; then
                echo "Single Unit with 4 S0 interfaces"
            elif [ ${TT} = '0102' ] ; then
                echo "Single Unit with 4 S0 interfaces and with license"
            elif [ ${TT} = '0200' ] ; then
                echo "S2Me without license"
            elif [ ${TT} = '0201' ] ; then
                echo "S2Me with relay and without license"
            elif [ ${TT} = '0202' ] ; then
                echo "S2Me with license"
            elif [ ${TT} = '0203' ] ; then
                echo "S2Me with relais and with license"
            elif [ ${TT} = '0280' ] ; then
                echo "OEM S2Me"
            elif [ ${TT} = '0300' ] ; then
                echo "MGB without license"
            elif [ ${TT} = '0302' ] ; then
                echo "MGB, with license"
            elif [ ${TT} = '0380' ] ; then
                echo "OEM MGB"
            fi
        else
            echo "Sorry, unknown Type: ${t}"
        fi

        OMGREVISION=$(printf "%01x\n" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 7 b))
        echo "Board revision is ${OMGREVISION}"

        OMGSERIAL=$(printf "%d\n" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 8 bl))
        echo "Serial number is ${OMGSERIAL}"

        OMGHWSERIAL=$(printf "%d\n" $(i2cutil -y -r ${1} ${I2CCONFIGADDRESS} 12 bl))
        echo "HW Serial number is ${OMGHWSERIAL}"

    fi
}

printDeviceData () {
    i2cdetect -a -y ${1} ${2} ${2} | grep -q '\-\-'
    rc=$?
    if [ $rc -eq 1 ]; then

        echo -n "        "

        TT=$(i2crdtype ${1} ${2} ${3})

        if [ ${TT} = '01' ] ; then
            subtype=$(i2crdsubtype ${1} ${2} ${3})
            if [ "${subtype}" = "00" ] ; then
                echo "1 S0 module"
            else
                echo "4 S0 module"
            fi

        elif [ ${TT} = '02' ] ; then
            echo "S2M Module"

        elif [ ${TT} = '04' ] ; then
            echo "S2M Module"

        elif [ ${TT} = '05' ] ; then
            echo "4BRI Module"

        elif [ ${TT} = '06' ] ; then
            echo "4FXS Module"
        elif [ ${TT} = '07' ] ; then
            echo "2FXS Module"
        else
            echo "Sorry, unknown Type: ${TT}"
        fi
    fi
}

while getopts "lh" opt;  do
    case "$opt" in

	l)  LIST="true"
	    ;;

	h)  echo "Usage: $0 [-f]"
	    echo "       collect eeprom infos"
	    echo "    -l print parseable list"
	    echo "    -h gives this message"
	    exit 1;;
        
	*)
	    echo "See $0 -h for usage"
	    exit 1;;
    esac
done

let i=1
for BUS in ${I2CCONFIGBUS};do
    BUS2=$(echo ${I2CBUS} | cut -d " " -f $i)

    t=$(printf "%08x" $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl))
    BOARDTYPE=${t:2:2}
    TYPE=${t:4:2}
    if [ ${TYPE} = '01' ] ; then
        SINGLEUNIT="true"
    fi

    if [ "${LIST}" = "true" ] ; then
        printProductDataList $BUS
        if [ "${SINGLEUNIT}" = "true" ] ; then
            printDeviceDataList $BUS  "0x6f"
        elif [ ${BOARDTYPE} = '02' ] ; then
            printDeviceDataList $BUS2 "${I2CCONFIGADDRESS}" 0
            printDeviceDataList $BUS2 "${I2CCONFIGADDRESS}" 1
            printDeviceDataList $BUS2 "${I2CCONFIGADDRESS}" 2
        elif [ ${BOARDTYPE} = '01' ] ; then
            printDeviceDataList $BUS2 "0x40"
            printDeviceDataList $BUS2 "0x40"
            printDeviceDataList $BUS2 "0x50"
            printDeviceDataList $BUS2 "0x58"
        else
            printDeviceDataList $BUS2 "0x40"
            printDeviceDataList $BUS2 "0x48"
            printDeviceDataList $BUS2 "0x50"
            printDeviceDataList $BUS2 "0x58"
        fi
    else
        printProductData $BUS
        if [ "${SINGLEUNIT}" = "true" ] ; then
            printDeviceData $BUS  "0x6f"
        elif [ ${BOARDTYPE} = '02' ] ; then
            printDeviceData $BUS2 "${I2CCONFIGADDRESS}" 0
            printDeviceData $BUS2 "${I2CCONFIGADDRESS}" 1
            printDeviceData $BUS2 "${I2CCONFIGADDRESS}" 2
        elif [ ${BOARDTYPE} = '01' ] ; then
            printDeviceData $BUS2 "0x40"
            printDeviceData $BUS2 "0x40"
            printDeviceData $BUS2 "0x50"
            printDeviceData $BUS2 "0x58"
        else
            printDeviceData $BUS2 "0x40"
            printDeviceData $BUS2 "0x48"
            printDeviceData $BUS2 "0x50"
            printDeviceData $BUS2 "0x58"
        fi
    fi
    echo


    let i=$i+1
done

