#! /bin/sh

echo "nvram init" >> /tmp/bootlog
# mount /nvram

if [ -e /sys/module/yaffs/parameters/yaffs_gid ]; then
    echo 501 > /sys/module/yaffs/parameters/yaffs_gid
    echo 501 > /sys/module/yaffs/parameters/yaffs_uid
fi

# Special partition handling for Preload POS feature (FW-12011):
# First check to see if the UBAPP flag "supportnandpos" is set:
#  - If not set then only mount /nvram
#  - If "supportnandpos" is set, check the UBAPP flag "tvconnected":
#   - If the flag is set to "true", run as normal (no special handling)
#   - If the flag is not set, mount the POS partition and the small /nvram partition (set in teeloaders code)
#   - If the flag is set to "justnow", do below steps:
#       -- Mount the RW2 (/dev/mtdblock9) partition to /nvram (which is the small /nvram partition)
#       -- Copy all content on /nvram to /tmp
#       -- Unmount /nvram
#       -- Erase the whole /nvram partition (/dev/mtd3)
#       -- Mount the RW partition (which is the normal /nvram partition)
#       -- Copy file back from /tmp tp /nvram


if ubappedit -q supportnandpos; then
    posSupportFlag=`ubappedit -q supportnandpos 2>/dev/null | tail -1`
    echo "S20nvram: 'supportnandpos' is $posSupportFlag"
    if [ $posSupportFlag = "true" ]; then

        # Get the POS partition filesystem type (FAT or YAFFS2)
        if ubappedit -q posfstype; then
            posfstype=`ubappedit -q posfstype 2>/dev/null | tail -1`
            echo "S20nvram: 'posfstype' is $posfstype"
        fi

        if ubappedit -q tvconnected; then # Connect flag has been set; process according to the set value
            connState=`ubappedit -q tvconnected 2>/dev/null | tail -1`
            echo "'tvconnected' is $connState"
            # If "tvconnected" Flag is "justnow", this is the first boot after connected; 
            # erase POS file and now /nvram takes all available space
            if [ $connState = "justnow" ]; then
                mkdir -p /media/POSVideo
                mount -t $posfstype /dev/mtdblock9 /media/POSVideo
                echo "ls /media/POSVideo:"
                ls /media/POSVideo
                mkdir -p /tmp/nvramcopy
                cp -a /media/POSVideo/nvramcopy/. /tmp/nvramcopy/
                echo "ls /tmp/nvramcopy/:"
                ls /tmp/nvramcopy/
                umount /media/POSVideo
                flash_eraseall /dev/mtd3
                mount /nvram
                cp -a /tmp/nvramcopy/. /nvram/
                rm -rf /media/POSVideo
                rm -rf /tmp/nvramcopy
                echo "ls nvram (after):"
                ls /nvram

                ubappedit tvconnected="true" -w
            # "tvconnected" Flag is "true"; TV is connected and has at least booted once before; just mount /nvram
            else
                mount /nvram
            fi
        # "tvconnected" Flag has NOT been set; mount the POS partition and the small /nvram partition
        else
            echo "S20nvram: 'tvconnected' is NOT set"

            # Assume the POS video file is embedded in ECC image
            posisembedded=1

            mkdir -p /media/POSVideo
            mount -t $posfstype /dev/mtdblock9 /media/POSVideo

            # When NAND POS flag is enabled in Custom Package but POS file isn't embedded in ECC image,
            # mount of POS partition as FAT ("vfat") will fail. Remount as YAFFS2 to save the hassle
            # of doing the same later in application code.
            if [ $? -ne 0 ]; then
                echo "S20nvram: cannot mount POS as vfat (not embedded); mount as YAFFS2 instead"
                mount -t yaffs2 /dev/mtdblock9 /media/POSVideo
                ubappedit posfstype="yaffs2" -w

                # POS video file is NOT embedded in ECC image
                posisembedded=0
            fi

            # Set the POS embedded flag if it hasn't been set (this is only done once)
            if ! ubappedit -q posinecc; then
                if [ $posisembedded = 1 ]; then
                    ubappedit posinecc="true" -w
                else
                    ubappedit posinecc="false" -w
                fi
            fi
            
            mount /nvram
        fi
    else # "supportnandpos" is "false"
        mount /nvram
    fi
else
    echo "S20nvram: 'supportnandpos' is NOT set"
    mount /nvram
fi


factoryresetfastboot=0
if [ -f /nvram/factoryresetfastboot ]; then
    #Fast boot requested
    factoryresetfastboot=1
fi

# NOTE: erasing /nvram for factory reset is done only here.
# 1) Pressing and holding the resetbutton causes u-boot to pass this information to
#    the kernel via the command line
# 2) If initiated by the UI creating the factoryresetflag2 file,
# Both are handled here.

# Remove obsolete flag file that may be left hanging around.
rm -f /nvram/factoryresetflag

# Reset button pressed
grep -q "factoryreset" /proc/cmdline
if [ "$?" == "0" ]; then
    touch /tmp/factoryresetflag
fi

if [ -f /nvram/factoryresetflag2 -o -f /nvram/factoryresetflag3 -o -f /tmp/factoryresetflag ]; then
    echo "Factory reset - erasing /nvram" >>/dev/console
    rm /tmp/factoryresetflag
    # if 'factoryresetflag3', we want to wipe nvram INCLUDING
    # user.pqdb.persist
    if [ -f /nvram/factoryresetflag3 ]; then
        echo "Factory reset - erasing user.pqdb" >>/dev/console
        rm /nvram/user.pqdb.persist*
    fi
    
    mkdir -p /tmp/persist
    for a in /nvram/*.persist ; do
        echo " saving `basename $a`" >>/dev/console
        cp -f $a /tmp/persist/.
    done
    umount /nvram
    flash_eraseall /dev/mtd3
    mount /nvram
    for a in /tmp/persist/* ; do
        echo " restoring `basename $a`" >>/dev/console
        cp -f $a /nvram/.
    done
    rm -rf /tmp/persist
fi

if grep "The corrupted file that is marked for deletion:" /proc/dm_auth_corrupted_file; then
    cat /proc/dm_auth_corrupted_file >> /tmp/dm_auth_corrupted_file
    rm -f `cat /proc/dm_auth_corrupted_file | awk '/The corrupted/{getline; print $1}'`;
fi

# To accelerate factory process and this is executed just one time
if [ $factoryresetfastboot = 1 ]; then
    # Disable MCU watchdog
    noreset
    #Verify /dev/root
    /bin/fastbootverify -v
    if [ $? = "0" ]; then
        #/dev/root/ OK
        #Boot Partition marked ok
        rm -f /nvram/factoryresetfastboot
        sync
        #Start app to process panel buttons
        /bin/factorysuspend &
        #update path for splash logo and show it
        export UNPLUG_IMG=/custom/assets/ShopInit/unplug_logo.png
        /bin/fastbootverify -b
        echo "pass and let's change splash screen"
    else
        echo "Failure verifying the /dev/root"
    fi
    while [ 1 ]; do
        sleep 3
    done
        reboot
fi

mkdir -p /nvram/incoming
chown app.app /nvram/incoming
mount /nvram/incoming /www/incoming
chown default.default /www/cgi-bin/pkgs

if [ `roku_platform` = longview -a ! -s /nvram/IB.bin ] ; then
    echo "*** Create fake IB.bin" 
    cp /etc/IB.fake /nvram/IB.bin 
fi
