#!/bin/sh
PART1="/dev/mmcblk1p1"
PART2="/dev/mmcblk1p2"
PART3="/dev/mmcblk1p3"

if [ -s /etc/functions-common.sh ]; then
    source /etc/functions-common.sh
fi

if [ -s /etc/first-boot-common.sh ]; then
    source /etc/first-boot-common.sh
fi

dbg_logger "Initializing device"

unmount_all_rootfs_partitions ${PART1} ${PART2}

# see if first time image booted, if so, create dirs
if [ -e "${FIRST_IMAGE_BOOT_FLAG}" ]; then
    # set registers to default value on boot as they may be tweaked by previous FW
    /usr/sbin/i2cset -y 0 0x08 0x23 0x8
    /usr/sbin/i2cset -y 0 0x08 0x38 0x8
    /usr/sbin/i2cset -y 0 0x08 0x3f 0x8

    format_var_data_if_invalid_and_mount ${PART3}
    resize_all_partitions ${PART1} ${PART2} ${PART3}
    create_var_data_folders_if_needed

    #set rootfs to readwrite
    /bin/mount -o remount,rw /dev/root /

    #if python package is waiting, install it
    if [ -f "/var/data/.tmp_py_upgrade/ctm_one_python_packages.tar" ]; then
        cd /var/data/.tmp_py_upgrade/
        /usr/bin/upgrade-python-boot ctm_one_python_packages.tar
        cd /var/data
    fi
    link_var_data_python_to_local_rootfs
    python_install_cleanup

    add_libiptext_symlink
    
    clean_prev_fw_files
fi
