#!/bin/bash
SCRIPT_PATH="$( cd "$( dirname "$0" )" && pwd )"

if [ -e /tmp/cmdline ]; then
    . /tmp/cmdline
fi

. ${SCRIPT_PATH}/i2csetup


advanced=0
reinit=0

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

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

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

let i=1
for CONFIGBUS in ${I2CCONFIGBUS}; do
    BUS=$(echo ${I2CBUS} | cut -d " " -f $i)
    
    if [ $isQuadE1 -ge 1 ] ; then
        boxtype="7"
    elif [ $isOfficeMasterGateAdvanced -ge 1 ] ; then
        boxtype="8"
    elif [ $ismgb -ge 1 ] ; then
        if [ "$CPU" = "Atom" ]; then
            boxtype="3"
        else
            boxtype="9"
        fi
    elif [ $isnews2m -ge 1 ] ; then
        boxtype="2"
    else
        boxtype="1"
    fi
    
    if [[ "${1}" =~ ^OMG[A28] ]]; then
        t=${1:3:1}
        serial=$(echo ${1:4}|sed -e 's/^0*//')
        hwserial=${serial}
        
        if [ ${1} -eq ${1} 2>/dev/null ]; then
            case ${p} in

                # Subject to change
                A)  article="20685"
                    revision="c"
                    subtype=2
                    ;;

                2)  article="20683"
                    subtype=2
                    revision="a6"
                    ;;

                8)  article="20693"
                    subtype=2
                    revision="a6"
                    ;;
            esac
            dontask=1
        fi
    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"
        echo " 9=MGB with new CPU Module"
        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
            if [ $advanced -ge 1 ] ; then
                echo "Channel type:"
                echo " 0=QuadE1 without license (develop only)"
                echo " 2=QuadE1 with    license (actual product)"
                echo "80=QuadE1 without license (actual product OEM)"
                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
            else
                subtype="2"
            fi
        elif [ "${boxtype}" == "8" ] ; then
            if [ $advanced -ge 1 ] ; then
                echo "Channel type:"
                echo " 0=OfficeMaster Gate Advanced without license (develop only)"
                echo " 2=OfficeMaster Gate Advanced with    license (actual product)"
                echo "80=OfficeMaster Gate Advanced without license (actual product OEM)"
                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
            else
                subtype="2"
            fi
        elif [ "${boxtype}" == "3" ] || [ "${boxtype}" == "9" ] ; then
            if [ $advanced -ge 1 ] ; then
                echo "Channel type:"
                echo " 0=MGB without license (develop only)"
                echo " 2=MGB with    license (actual product)"
                echo "80=MGB without license (actual product OEM)"
                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
            else
                subtype="2"
            fi
        elif [ "${boxtype}" == "2" ] ; then
            if [ $advanced -ge 1 ] ; then
                echo "Channel type:"
                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)"
                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
                subtype="2"
            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" ]  || [ "${boxtype}" == "9" ]; then
            oldrevision="b1"
        elif [ "${boxtype}" == "7" ] ; then
            oldrevision="a1"
        elif [ "${boxtype}" == "8" ] ; then
            oldrevision="c"
        else
            oldrevision="a6"
        fi
    fi
    if [ $advanced -ge 1 ] ; then
        while [ -z "${revision}" ]; do
            read -p "Board revision (a1-a6, b1-b2, c, default: ${oldrevision})? " revision
            if [ -z "${revision}" ]; then
                revision="${oldrevision}"
            fi
            if [ "${revision}" == "" ]; then
                echo "wrong revision"
                revision=''
            fi
        done
    else
        revision="${oldrevision}"
    fi
    
    oldserial=$(i2crdserial ${CONFIGBUS})
    dispoldserial=${oldserial}
    if [ -z "${oldserial}" ]; then
        oldserial=$(printf "%d" "0xffffffff")
        dispoldserial="(not set)"
    fi
    if [ $reinit -ge 1 ];then
        serial=${oldserial}
    else
        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
                :
            else
                echo "Not a number"
                serial=''
            fi
        done
    fi

    oldhwserial=$(i2crdhwserial ${CONFIGBUS})
    dispoldhwserial=${oldhwserial}
    if [ -z "${oldhwserial}" -o "${oldhwserial}" = "i2crdhwserial: hwserial not set" ]; then
        oldhwserial="${serial}"
        dispoldhwserial="${serial}"
    fi
    if [ $advanced -ge 1 ] ; then
        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
    else
        hwserial="${serial}"
    fi
    
    oldarticle=$(i2crdarticle ${CONFIGBUS})
    if [ $reinit -ge 1 ];then
        oldarticle="0"
    fi
    if [ -z "${oldarticle}" -o "${oldarticle}" = "0" -o "${oldarticle}" = "i2crdarticle: article not set" ]; then
        if [ ${isQuadE1} -ge 1 ]; then
            oldarticle="22003"
        elif [ ${isOfficeMasterGateAdvanced} -ge 1 ]; then
            oldarticle="20685"
        else
            oldarticle="0"
        fi
    fi
    if [ $advanced -ge 1 -o \( ! ${isQuadE1} -ge 1 -a ! ${isOfficeMasterGateAdvanced} -ge 1 \) ] ; then 
        while [ -z "${article}" ]; do
            read -p "Article number default: ${oldarticle} ? " 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
    else
        article="${oldarticle}"
    fi
    
    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})
    elif [ "${boxtype}" == "9" ] ; then
        w1=$(printf "0x00%02x%02x%02x" 3 3 ${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)
    
    ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
    ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
    ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
    ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
    ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 0 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 4 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 8 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 12 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 16 bl))
    echo "${W1}"
    
    if [ "${boxtype}" == "7" ] ; then
        echo "set device eeproms for QuadE1"
        
        w6=$(printf "0x0000%04x" 32)
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 28 ${w6} bl > /dev/null
        W1=$(printf "set bus ${CONFIGBUS} device ${I2CCONFIGADDRESS} bytes 28-31:\n0x%08x\n\n" $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 28 bl))
        echo "${W1}"
        
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 ${w1} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 ${w2} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 ${w3} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 ${w4} bl > /dev/null
        ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 bl) $(${SCRIPT_PATH}/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)
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
            ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 16 bl))
            echo "${W1}"
        fi
    elif [ "${boxtype}" == "8" ] ; then
        echo "set device eeproms for OfficeMaster Gate Advanced"
        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)
        
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 28 ${w6} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 ${w1} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 ${w2} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 ${w3} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 ${w4} bl > /dev/null
        ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 48 bl))
        echo "${W1}"
        
        w1=$(printf "0x0000%02x%02x" 7 0)
        w6=$(printf "0x0000%04x" 96)
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 60 ${w6} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 64 ${w1} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 68 ${w2} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 72 ${w3} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 76 ${w4} bl > /dev/null
        ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 64 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 68 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 72 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 76 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 80 bl))
        echo "${W1}"
        
        w1=$(printf "0x0000%02x%02x" 4 0)
        w6=$(printf "0x0000%04x" 64)
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 92 ${w6} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 96 ${w1} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 100 ${w2} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 104 ${w3} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 108 ${w4} bl > /dev/null
        ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 96 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 100 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 104 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 108 bl) $(${SCRIPT_PATH}/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)
        ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
        ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 16 bl))
        echo "${W1}"
        
        
        I2CCONFIGADDRESS="0x48"
        w1=$(printf "0x0000%02x%02x" 5 0)
        ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
        ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(${SCRIPT_PATH}/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)
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
            ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 16 bl))
            echo "${W1}"
        fi
    elif [ "${boxtype}" == "9" ] ; then
        echo "set device eeproms for MGB with new CPU"
        w1=$(printf "0x0000%02x%02x" 4 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)
        
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 28 ${w6} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 ${w1} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 ${w2} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 ${w3} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 ${w4} bl > /dev/null
        ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 32 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 36 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 40 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 44 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 48 bl))
        echo "${W1}"
        
        w1=$(printf "0x0000%02x%02x" 5 0)
        w6=$(printf "0x0000%04x" 96)
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 60 ${w6} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 64 ${w1} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 68 ${w2} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 72 ${w3} bl > /dev/null
        ${SCRIPT_PATH}/i2cutil -y ${CONFIGBUS} ${I2CCONFIGADDRESS} 76 ${w4} bl > /dev/null
        ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 64 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 68 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 72 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 76 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${CONFIGBUS} ${I2CCONFIGADDRESS} 80 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)
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 0 ${w1} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 4 ${w2} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 8 ${w3} bl > /dev/null
            ${SCRIPT_PATH}/i2cutil -y ${BUS} ${I2CCONFIGADDRESS} 12 ${w4} bl > /dev/null
            ${SCRIPT_PATH}/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" $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 0 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 4 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 8 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 12 bl) $(${SCRIPT_PATH}/i2cutil -y -r ${BUS} ${I2CCONFIGADDRESS} 16 bl))
            echo "${W1}"
        fi
    fi
    let i=$i+1
    boxtype=""
    subtype=""
    revision=""
    serial=""
    hwserial=""
    article=""
    
done

exit
