#!/bin/bash
logger -s "## postinst_all ##"

now="`date`"
echo "Setting update date to current date: ${now}"
date +%s > ${upgradePath}/etc/version.update

## Manage saved settings and indicate "updateInProgress" on next boot

## remove this condition when pkg manager is eliminated from freshinstall process
if [ ! -f /tmp/fresh_install ]; then
##
    if [ -f /tmp/fresh_update ]; then
        logger -s "Fresh state detected"
        # copy master_drive_serial_number and reformat the DataVolume for 'fresh update'
        echo "${dataVolumeFormat}" > ${upgradePath}/${reformatDataVolume}
        cp -v /var/log/master_drive_serial_number ${upgradePath}/var/log/master_drive_serial_number 2>/dev/null
        # notify system about "fresh" condition on reboot
        factoryRestore.sh
        touch ${upgradePath}/etc/.fresh_install
        # Force the kernel and control partion copying here for now - 
        # TODO: Use separate kernel and image containers deployed by common group container 
        # ("why?" For one, there is a really ugly dependency V-directly below here-V of a project specific component name which must now be hard coded into the postinst)
        ${upgradePath}/usr/local/sbin/settingsManager.sh -p kernel-mindspeed-sequoia --action update --upgradepath "${upgradePath}"
    else
        logger -s "Upgrade state detected"
        if [ ! -f /tmp/fresh_update ]; then
            ## manage saved settings for master-package ##
            if [ -n "${upgradeDevice}" ]; then
                ${upgradePath}/usr/local/sbin/settingsManager.sh --action update --upgradepath "${upgradePath}"
            fi
            ## Indicate fw update on re-boot
            touch ${upgradePath}/etc/.updateInProgress
        fi
    fi
##
fi
##

## Save cached debs accross factory-restore
if [ -d ${pkg_upgrades_dir} ]; then
    logger -s "Saving upgrade cache to redundant location in rootfs"
    echo "Saving upgrade cache to redundant location in rootfs" >> /CacheVolume/update.log
    rm -rf ${upgradePath}/usr/local/nas/upgrade/* #*/
    mkdir -p ${upgradePath}/usr/local/nas/upgrade
    cp -rpvf ${pkg_upgrades_dir}/* ${upgradePath}/usr/local/nas/upgrade >> /CacheVolume/update.log #*/ 
fi
