#!/bin/sh

source fcns.shinc
source /usr/bin/utils.sh

actComp="/"

apiActive $actComp "Building PFU bundle."

actComp="/bundle/pfu"

# =====================================================================
# $1 - Name of the ini file with mtd-to-file properties
# $2 - Name of the folder where all files will be saved
# =====================================================================
createMcMtdFiles()
{
    local iniFile="$1"
    local outFolder="$2"

    mkdir -p "$outFolder"

    # depends on info in fcns.shinc to tell which partitions
    getExtractableMtds mtdNames
    echo "$mtdNames"
    for name in $mtdNames; do
        apiActive $actComp "Install $name."
        # See if we need to save meta-data about the partition to its tail
        local map=mtd_mc_$name
        eval local outFile="\$$map"
        local partnbr=mtdnbr_$name
        eval local partition="/dev/mtd\$$partnbr"

        if [ "$name" == "loader" -o "$name" == "loader_meta" ];
	    then
            strings "$partition" | head -1 | grep -q x$
            if [ "$?" -eq 1 ];
            then
	            local nbrBytes=$(inicntrl "$iniFile" "${outFile//\./_}" size)
            else
	            local nbrBytes=$(inicntrl "$iniFile" "${outFile//\./_}_16" size)
            fi
        else
	       local nbrBytes=$(inicntrl "$iniFile" "${outFile//\./_}" size)
        fi
        if [ -n "$nbrBytes" ]; then
            dd if=$partition of=$outFolder/$outFile \
               bs=$nbrBytes count=1

            md5Orig=$(calcmd5 $partition 0 $nbrBytes)
            echo "md5Orig: "$md5Orig
            md5New=$(calcmd5 $outFolder/$outFile)
            echo "md5New: "$md5New
            if [ "$name" == "loader" -o "$name" == "loader_meta" ];
            then
                strings "$outFolder"/"$outFile" | head -1 | grep -q x$
                if [ "$?" -eq 1 ];
                then
                    md5Expected=$(inicntrl "$iniFile" "${outFile//\./_}" md5sum)
                else
                    md5Expected=$(inicntrl "$iniFile" "${outFile//\./_}_16" md5sum)
                fi
            else
                md5Expected=$(inicntrl "$iniFile" "${outFile//\./_}" md5sum)
            fi
            echo "md5Expected: "$md5Expected

            if [ "$md5Orig" == "$md5New" ]; then
                if [ "$md5New" == "$md5Expected" ]; then
                    apiActive $actComp "Verified - Extracted file $name from mtd partition"
                else
                    apiError $actComp 2006 "ERROR - md5sum from version.txt does not match mtd file $outFile"
                    local badBundle=$(inicntrl "$iniFile" system_bundle version)
                    apiError $actComp 2006 "ERROR - bad version.txt has system_bundle.version=$badBundle"
                    return 1
                fi
            else
                apiError $actComp 2006 "ERROR - md5sum error while extracting mtd file $outFile"
                return 1
            fi
        fi
    done

    return 0

}

genAllMtdPropVariables "$(cat /proc/moose/mtd)"
rm -rf /mnt/ramdisk/bundle/mc
rm -rf /mnt/ramdisk/bundle/sc
createMcMtdFiles  /mnt/cfg/version.txt  /mnt/ramdisk/bundle/mc/components
rc=$?
touch /mnt/ramdisk/bundle/do.reboot
touch /mnt/ramdisk/bundle/is.pfu
touch /mnt/ramdisk/bundle/is.spiboot

# install our scripts and copy capi.app over just in case I guess.
cp /usr/bin/*.sh /mnt/ramdisk/bundle/
cp /usr/bin/*.awk /mnt/ramdisk/bundle/
cp /usr/bin/capi.app /mnt/ramdisk/bundle/

#cp /cfg/pfu/mc/* /mnt/ramdisk/bundle/mc


# We need to grab the release.xml and platform.xml files from the filesystem
# to bring over just in case the receiver ever needs to use it.
# Note that buildcfgbundle also does this. So these copies are redundant in the
# case where both buildmcbundle and buildcfgbundle are executed together.

# We want the installed XML files to be the ones that are pushed across,
# not the ones that are sitting in /cfg/pfu/mc.
rm -f /mnt/ramdisk/bundle/mc/release.xml
rm -f /mnt/ramdisk/bundle/mc/platform.xml

cp /app/relinfo/release.xml /mnt/ramdisk/bundle/mc
cp /etc/platform.xml /mnt/ramdisk/bundle/mc

bundleadditions.sh "/" "/mnt/ramdisk/bundle"

mkdir /mnt/ramdisk/bundle/sc
mkdir /mnt/ramdisk/bundle/sc/components
#cp /cfg/pfu/sc/* /mnt/ramdisk/bundle/sc

# now copy over the CPLD
if [ -d /cfg/pfu/cpld ]
then
    apiActive $actComp "Install CPLD."
    mkdir -p /mnt/ramdisk/bundle/cpld/components
    # going to grab all of the CPLD images and the update.sh script in one swoop
    cp -r /cfg/pfu/cpld/* /mnt/ramdisk/bundle/cpld
fi

if [ "$rc" -eq "0" ]
then
    apiOK $actComp
else
    rm -rf /mnt/ramdisk/bundle
    # if we messed up, we really do not want to flash the partner. We're going to clear this out and fail the PFU.
fi
