#!/bin/bash

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

boxtype=$1
subtype=$2
revision=$3
serial=$4
hwserial=$5
advanced=0

if [ "${1}" == "advanced" ]; then
    advanced=1
fi

isnews2m=$(lspci -n | grep -c "1011:f001")
ismgb=$(lspci -n | grep -c "1011:f002")
isQuadE1=$(lspci -n | grep -c "1011:f004")
isAtomBox=$(lspci -n | grep -c "1011:f005")

if [ -z "$(which i2cutil)" ] ; then
    "i2cutil not found"
    exit 1
else
    let i=1
    for CONFIGBUS in ${I2CCONFIGBUS}; do
        BUS=$(echo ${I2CBUS} | cut -d " " -f $i)

        if [ $isQuadE1 -ge 1 ] ; then
	    boxtype="7"
        elif [ $isAtomBox -ge 1 ] ; then
	    boxtype="8"
        elif [ $ismgb -ge 1 ] ; then
	    boxtype="3"
        elif [ $isnews2m -ge 1 ] ; then
	    boxtype="2"
        else
	    boxtype="1"
        fi

        if [ $advanced -ge 1 ] ; then
            oldboxtype=${boxtype}
            echo "Box type:"
	    echo " 1=Single Unit (4S0)"
	    echo " 2=S2Me"
	    echo " 3=MGB"
	    echo " 7=QuadE1"
	    echo " 8=OfficeMaster Gate Advanced"
            read -p "default: ${oldboxtype}? "  boxtype
            if [ -z "${boxtype}" ]; then
	        boxtype="${oldboxtype}"
	    fi
        fi

        oldsubtype=$(i2crdsubtype ${CONFIGBUS})
        if [ -z "${oldsubtype}" ]; then
	    if [ "${boxtype}" == "2" ] ; then
	        oldsubtype="2"
	    else
	        oldsubtype="2"
	    fi
        elif [ "${oldsubtype}" == "ff" ]; then
            oldsubtype="2"
        else
            oldsubtype=$(printf "%d" "${oldsubtype}")
        fi
        while [ -z "${subtype}" ]; do
	    if [ "${boxtype}" == "7" ] ; then
	        echo "Channel type:"
	        if [ $advanced -ge 1 ] ; then
	            echo " 0=QuadE1 without license (develop only)"
	            echo " 2=QuadE1 with    license (actual product)"
                    echo "80=QuadE1 without license (actual product OEM)"
                else
	            echo " 2=QuadE1 with    license (actual product)"
                    echo "80=QuadE1 without license (actual product OEM)"
                fi
	        read -p "default: ${oldsubtype}? "  subtype
	        if [ -z "${subtype}" ]; then
		    subtype="${oldsubtype}"
	        fi

                if [ "${subtype}" != "0" -a "${subtype}" != "2" -a "${subtype}" != "80" ]; then
		    echo "wrong subtype"
		    subtype=''
	        fi
	    elif [ "${boxtype}" == "8" ] ; then
	        echo "Channel type:"
	        if [ $advanced -ge 1 ] ; then
	            echo " 0=AtomBox without license (develop only)"
	            echo " 2=AtomBox with    license (actual product)"
                    echo "80=AtomBox without license (actual product OEM)"
                else
	            echo " 2=AtomBox with    license (actual product)"
                    echo "80=AtomBox without license (actual product OEM)"
                fi
	        read -p "default: ${oldsubtype}? "  subtype
	        if [ -z "${subtype}" ]; then
		    subtype="${oldsubtype}"
	        fi

                if [ "${subtype}" != "0" -a "${subtype}" != "2" -a "${subtype}" != "80" ]; then
		    echo "wrong subtype"
		    subtype=''
	        fi
	    elif [ "${boxtype}" == "3" ] ; then
	        echo "Channel type:"
	        if [ $advanced -ge 1 ] ; then
	            echo " 0=MGB without license (develop only)"
	            echo " 2=MGB with    license (actual product)"
                    echo "80=MGB without license (actual product OEM)"
                else
	            echo " 2=MGB"
                    echo "80=MGB without license (actual product OEM)"
                fi
	        read -p "default: ${oldsubtype}? "  subtype
	        if [ -z "${subtype}" ]; then
		    subtype="${oldsubtype}"
	        fi

                if [ "${subtype}" != "0" -a "${subtype}" != "2" -a "${subtype}" != "80" ]; then
		    echo "wrong subtype"
		    subtype=''
	        fi
	    elif [ "${boxtype}" == "2" ] ; then
	        echo "Channel type:"
	        if [ $advanced -ge 1 ] ; then
	            echo " 0=S2Me without relais and without license (develop only)"
	            echo " 1=S2Me with    relais and without license (develop only)"
	            echo " 2=S2Me without relais and with    license (actual product)"
	            echo " 3=S2Me with    relais and with    license (develop only)"
	            echo "80=S2Me with    relais and with    license (actual product OEM)"
                else
	            echo " 2=S2Me"
	            echo "80=S2Me OEM"
                fi
	        read -p "default: ${oldsubtype}? "  subtype
	        if [ -z "${subtype}" ]; then
		    subtype="${oldsubtype}"
	        fi

                if [ "${subtype}" != "0" -a "${subtype}" != "1" -a "${subtype}" != "2" -a "${subtype}" != "3" -a "${subtype}" != "80" ]; then
		    echo "wrong subtype"
		    subtype=''
	        fi
	    else
	        read -p "Channel type (0=1S0, 1=4S0, 2=4S0 with license default: ${oldsubtype})? "  subtype
	        if [ -z "${subtype}" ]; then
		    subtype="${oldsubtype}"
	        fi
	        if [ "${subtype}" != "0" -a "${subtype}" != "1" -a "${subtype}" != "2" ]; then
		    echo "wrong subtype"
		    subtype=''
	        fi
	    fi
        done


        oldrevision=$(i2crdrevision ${CONFIGBUS})
        if [ -z "${oldrevision}" -o "${oldrevision}" == "ff" ]; then
	    if [ "${boxtype}" == "2" ] ; then
	        oldrevision="a6"
	    elif [ "${boxtype}" == "3" ] ; then
	        oldrevision="b1"
	    elif [ "${boxtype}" == "7" ] ; then
	        oldrevision="a1"
	    elif [ "${boxtype}" == "8" ] ; then
	        oldrevision="a1"
	    else
	        oldrevision="a6"
	    fi
        fi
        while [ -z "${revision}" ]; do
            read -p "Board revision (a1-a6, b1 default: ${oldrevision})? " revision
            if [ -z "${revision}" ]; then
                revision="${oldrevision}"
            fi
            if [ "${revision}" == "" ]; then
                echo "wrong revision"
                revision=''
            fi
        done

        oldserial=$(i2crdserial ${CONFIGBUS})
        dispoldserial=${oldserial}
        if [ -z "${oldserial}" ]; then
            oldserial=$(printf "%d" "0xffffffff")
	    dispoldserial="(not set)"
        fi
        while [ -z "${serial}" ]; do
            read -p "Serial number default: ${dispoldserial} ? " serial
            if [ -z "${serial}" ]; then
                serial="${oldserial}"
            fi
            if [  ${serial} -eq ${serial} 2> /dev/null ]; then
                i=0 # just syntactical reasons
            else
                echo "Not a number"
                serial=''
            fi
        done

        oldhwserial=$(i2crdhwserial ${CONFIGBUS})
        dispoldhwserial=${oldhwserial}
        if [ -z "${oldhwserial}" -o "${oldhwserial}" = "i2crdhwserial: hwserial not set" ]; then
            oldhwserial="${serial}"
	    dispoldhwserial="${serial}"
        fi
        while [ -z "${hwserial}" ]; do
            read -p "Hwserial number default: ${dispoldhwserial} ? " hwserial
            if [ -z "${hwserial}" ]; then
                hwserial="${oldhwserial}"
            fi
            if [  ${hwserial} -eq ${hwserial} 2> /dev/null ]; then
                i=0 # just syntactical reasons
            else
                echo "Not a number"
                hwserial=''
            fi
        done

        oldarticle=$(i2crdarticle ${CONFIGBUS})
        dispoldarticle=${oldarticle}
        if [ -z "${oldarticle}" -o "${oldarticle}" = "i2crdarticle: article not set" ]; then
            oldarticle="${serial}"
	    if [ ${isQuadE1} -ge 1 ]; then
		dispoldarticle="22002"
	    elif [ ${isAtomBox} -ge 1 ]; then
		dispoldarticle="20624"
	    else
		dispoldarticle="${serial}"
	    fi
        fi
        while [ -z "${article}" ]; do
            read -p "Article number default: ${dispoldarticle} ? " article
            if [ -z "${article}" ]; then
                article="${oldarticle}"
            fi
            if [  ${article} -eq ${article} 2> /dev/null ]; then
                i=0 # just syntactical reasons
            else
                echo "Not a number"
                article=''
            fi
        done


        
        if [ ${subtype} = "80" ] ; then
            subtype=128 # set to 0x80
        fi

        if [ "${boxtype}" == "7" ] ; then
            w1=$(printf "0x00%02x%02x%02x" 1 4 ${subtype})
        elif [ "${boxtype}" == "8" ] ; then
            w1=$(printf "0x00%02x%02x%02x" 2 4 ${subtype})
        else
            w1=$(printf "0x0000%02x%02x" ${boxtype} ${subtype})
        fi

        w2=$(printf "0x000000%s" ${revision})
        w3=$(printf "0x%08x" $serial)
        w4=$(printf "0x%08x" $hwserial)
        w5=$(printf "0x%08x" $article)

        i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
        i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
        i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
        i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
        i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 16 ${w5} bl > /dev/null

        W1=$(printf "set bus ${CONFIGBUS} device ${I2CCONFIGADDRESS} bytes 0-23:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 0 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 4 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 8 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 12 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 16 bl))
        echo "${W1}"

        if [ "${boxtype}" == "7" ] ; then
	    echo "set device eeproms for QuadE1"

            w6=$(printf "0x0000%04x" 32)
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 28 ${w6} bl > /dev/null
            W1=$(printf "set bus ${CONFIGBUS} device ${I2CCONFIGADDRESS} bytes 28-31:\n0x%08x\n\n" $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 28 bl))
            echo "${W1}"

            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 ${w1} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 ${w2} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 ${w3} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 ${w4} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 48 ${w5} bl > /dev/null

            W1=$(printf "set bus ${CONFIGBUS} device ${I2CCONFIGADDRESS} bytes 32-51:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 48 bl))
            echo "${W1}"

            i2cdetect -a -y ${BUS} 0x50 0x50 | grep -q '\-\-'
            if [ $? -eq 1 ]; then
                I2CCONFIGADDRESS="0x50"

                echo

                revision=""
 	        oldrevision=$(i2crdrevision ${BUS} ${I2CCONFIGADDRESS})
                if [ -z "${oldrevision}" ]; then
           	    oldrevision="a1"
                elif [ "${oldrevision}" == "0xff" ]; then
                    oldrevision="a1"
                fi
                while [ -z "${revision}" ]; do
                    read -p "Analog Board revision (a1 default: ${oldrevision})? " revision
                    if [ -z "${revision}" ]; then
                        revision="${oldrevision}"
                    fi
                    if [ "${revision}" == "" ]; then
                        echo "wrong revision"
                        revision=''
                    fi
                done

                serial=""
                oldserial=$(i2crdserial ${BUS} ${I2CCONFIGADDRESS})
                dispoldserial=${oldserial}
                if [ -z "${oldserial}" ]; then
                    oldserial=$(printf "%d" "0xffffffff")
        	    dispoldserial="(not set)"
                fi
                while [ -z "${serial}" ]; do
                    read -p "Analog Board serial number default: ${dispoldserial} ? " serial
                    if [ -z "${serial}" ]; then
                        serial="${oldserial}"
                    fi
                    if [  ${serial} -eq ${serial} 2> /dev/null ]; then
                        i=0 # just syntactical reasons
                    else
                        echo "Not a number"
                        serial=''
                    fi
                done

                hwserial=""
                oldhwserial=$(i2crdhwserial ${BUS} ${I2CCONFIGADDRESS})
                dispoldhwserial=${oldhwserial}
                if [ -z "${oldhwserial}" -o "${oldhwserial}" = "i2crdhwserial: hwserial not set" ]; then
                    oldhwserial=$(printf "%d" "0xffffffff")
        	    dispoldhwserial="(not set)"
                fi
                while [ -z "${hwserial}" ]; do
                    read -p "Analog Board hwserial number default: ${dispoldhwserial} ? " hwserial
                    if [ -z "${hwserial}" ]; then
                        hwserial="${oldhwserial}"
                    fi
                    if [  ${hwserial} -eq ${hwserial} 2> /dev/null ]; then
                        i=0 # just syntactical reasons
                    else
                        echo "Not a number"
                        hwserial=''
                    fi
                done

                article=""
                oldarticle=$(i2crdarticle ${BUS} ${I2CCONFIGADDRESS})
                dispoldarticle=${oldarticle}
                if [ -z "${oldarticle}" -o "${oldarticle}" = "i2crdarticle: article not set" ]; then
                    oldarticle=$(printf "%d" "0xffffffff")
        	    dispoldarticle="(not set)"
                fi
                while [ -z "${article}" ]; do
                    read -p "Analog Board article number default: ${dispoldarticle} ? " article
                    if [ -z "${article}" ]; then
                        article="${oldarticle}"
                    fi
                    if [  ${article} -eq ${article} 2> /dev/null ]; then
                        i=0 # just syntactical reasons
                    else
                        echo "Not a number"
                        article=''
                    fi
                done

                w1=$(printf "0x0000%02x%02x" 6 0)
                w2=$(printf "0x000000%s" ${revision})
                w3=$(printf "0x%08x" $serial)
                w4=$(printf "0x%08x" $hwserial)
                w5=$(printf "0x%08x" $article)
		i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
		i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
		i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
		i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
		i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 16 ${w5} bl > /dev/null
		W1=$(printf "set bus ${BUS} device ${I2CCONFIGADDRESS}:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 16 bl))
                echo "${W1}"
            fi
        elif [ "${boxtype}" == "8" ] ; then
	    echo "set device eeproms for AtomBox"
            w1=$(printf "0x0000%02x%02x" 5 0)
            w2=$(printf "0x000000%s" ${revision})
            w3=$(printf "0x%08x" $serial)
            w4=$(printf "0x%08x" $hwserial)
            w5=$(printf "0x%08x" $article)
            w6=$(printf "0x0000%04x" 32)

            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 28 ${w6} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 ${w1} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 ${w2} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 ${w3} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 ${w4} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 48 ${w5} bl > /dev/null
            W1=$(printf "set bus ${CONFIGBUS} device ${I2CCONFIGADDRESS} bytes 32-63:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 48 bl))
            echo "${W1}"

            w1=$(printf "0x0000%02x%02x" 7 0)
            w6=$(printf "0x0000%04x" 96)
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 60 ${w6} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 64 ${w1} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 68 ${w2} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 72 ${w3} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 76 ${w4} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 80 ${w5} bl > /dev/null
            W1=$(printf "set bus ${CONFIGBUS} device ${I2CCONFIGADDRESS} bytes 64-95:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 64 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 68 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 72 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 76 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 80 bl))
            echo "${W1}"

            w1=$(printf "0x0000%02x%02x" 4 0)
            w6=$(printf "0x0000%04x" 64)
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 92 ${w6} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 96 ${w1} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 100 ${w2} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 104 ${w3} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 108 ${w4} bl > /dev/null
            i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 112 ${w5} bl > /dev/null
            W1=$(printf "set bus ${CONFIGBUS} device ${I2CCONFIGADDRESS} bytes 96-115:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 96 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 100 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 104 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 108 bl) $(i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 102 bl))
            echo "${W1}"

        elif [ "${boxtype}" == "3" ] ; then
	    echo "set device eeproms for mgb"

	    I2CCONFIGADDRESS="0x40"
	    w1=$(printf "0x0000%02x%02x" 4 0)
	    i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
	    i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
	    i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
	    i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
            i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 16 ${w5} bl > /dev/null
	    W1=$(printf "set bus ${BUS} device ${I2CCONFIGADDRESS}:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 16 bl))
	    echo "${W1}"


	    I2CCONFIGADDRESS="0x48"
	    w1=$(printf "0x0000%02x%02x" 5 0)
	    i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
	    i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
	    i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
	    i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
            i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 16 ${w5} bl > /dev/null
	    W1=$(printf "set bus ${BUS} device ${I2CCONFIGADDRESS}:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 16 bl))
	    echo "${W1}"

            i2cdetect -a -y ${BUS} 0x50 0x50 | grep -q '\-\-'
            if [ $? -eq 1 ]; then
                I2CCONFIGADDRESS="0x50"

                echo

                revision=""
 	        oldrevision=$(i2crdrevision ${BUS} ${I2CCONFIGADDRESS})
                if [ -z "${oldrevision}" ]; then
           	    oldrevision="a1"
                elif [ "${oldrevision}" == "0xff" ]; then
                    oldrevision="a1"
                fi
                while [ -z "${revision}" ]; do
                    read -p "Analog Board revision (a1 default: ${oldrevision})? " revision
                    if [ -z "${revision}" ]; then
                        revision="${oldrevision}"
                    fi
                    if [ "${revision}" == "" ]; then
                        echo "wrong revision"
                        revision=''
                    fi
                done

                serial=""
                oldserial=$(i2crdserial ${BUS} ${I2CCONFIGADDRESS})
                dispoldserial=${oldserial}
                if [ -z "${oldserial}" ]; then
                    oldserial=$(printf "%d" "0xffffffff")
        	    dispoldserial="(not set)"
                fi
                while [ -z "${serial}" ]; do
                    read -p "Analog Board serial number default: ${dispoldserial} ? " serial
                    if [ -z "${serial}" ]; then
                        serial="${oldserial}"
                    fi
                    if [  ${serial} -eq ${serial} 2> /dev/null ]; then
                        i=0 # just syntactical reasons
                    else
                        echo "Not a number"
                        serial=''
                    fi
                done

                hwserial=""
                oldhwserial=$(i2crdhwserial ${BUS} ${I2CCONFIGADDRESS})
                dispoldhwserial=${oldhwserial}
                if [ -z "${oldhwserial}" -o "${oldhwserial}" = "i2crdhwserial: hwserial not set" ]; then
                    oldhwserial=$(printf "%d" "0xffffffff")
        	    dispoldhwserial="(not set)"
                fi
                while [ -z "${hwserial}" ]; do
                    read -p "Analog Board hwserial number default: ${dispoldhwserial} ? " hwserial
                    if [ -z "${hwserial}" ]; then
                        hwserial="${oldhwserial}"
                    fi
                    if [  ${hwserial} -eq ${hwserial} 2> /dev/null ]; then
                        i=0 # just syntactical reasons
                    else
                        echo "Not a number"
                        hwserial=''
                    fi
                done

                article=""
                oldarticle=$(i2crdarticle ${BUS} ${I2CCONFIGADDRESS})
                dispoldarticle=${oldarticle}
                if [ -z "${oldarticle}" -o "${oldarticle}" = "i2crdarticle: article not set" ]; then
                    oldarticle=$(printf "%d" "0xffffffff")
        	    dispoldarticle="(not set)"
                fi
                while [ -z "${article}" ]; do
                    read -p "Analog Board article number default: ${dispoldarticle} ? " article
                    if [ -z "${article}" ]; then
                        article="${oldarticle}"
                    fi
                    if [  ${article} -eq ${article} 2> /dev/null ]; then
                        i=0 # just syntactical reasons
                    else
                        echo "Not a number"
                        article=''
                    fi
                done

                w1=$(printf "0x0000%02x%02x" 6 0)
                w2=$(printf "0x000000%s" ${revision})
                w3=$(printf "0x%08x" $serial)
                w4=$(printf "0x%08x" $hwserial)
                w5=$(printf "0x%08x" $article)
                i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
                i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
                i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
                i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
                i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 16 ${w5} bl > /dev/null
                W1=$(printf "set bus ${BUS} device ${I2CCONFIGADDRESS}:\n0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n\n" $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 16 bl))
                echo "${W1}"
            fi
        fi
        let i=$i+1
        boxtype=""
        subtype=""
        revision=""
        serial=""
        hwserial=""
        article=""

    done
    exit
fi
