. /scripts/common/upgrade

# This is a common shell script fragment for platforms that use
# initramfs-tools(8) and lvm(8).

prep_upgrade()
{
    echo "Preparing image upgrade..."
    inactive="$(( ( $active % 2 ) + 1 ))"
    active_sysroot_dir=$(mktemp -d)
    mount_slot $active $active_sysroot_dir
    passive_sysroot_dir=$(mktemp -d)
    mount_slot $inactive $passive_sysroot_dir
    passive_sysroot_init="${passive_sysroot_dir}/var/lib/cumulus/installer/onie-installer"
    passive_sysroot_init_dir=$(mktemp -d)
    tmp_cl_content=$(mktemp -d)
    sh $passive_sysroot_init extract $tmp_cl_content || {
        echo "Could not extract original sysroot"
        rm -fr $tmp_cl_content
    }
    . "${tmp_cl_content}/file.list"
    tar -xzf "${tmp_cl_content}/${img_sysroot}" -C $passive_sysroot_init_dir || {
        echo "Could not extract old sysroot"
    }
    rm -fr $tmp_cl_content
    reinstall_pkgs=$(mktemp)
    existing_pkgs=$(mktemp)
    echo "# Image Upgrade..." 
}

cleanup_upgrade()
{   
    for f in $reinstall_pkgs $existing_pkgs
    do
        [ -f $f ] && rm -f $f
    done
    for u in $active_sysroot_dir $passive_sysroot_dir
    do
        umount $u && rm -fr $u
    done
    rm -fr "$tmp_cl_content" "$passive_sysroot_init_dir"
}

copy_all_files()
{
    local exclude="
    /dev
    /proc
    /sys
    /run
    /var/lib/apt
    /var/lib/dpkg
    /var/lib/cumulus
    /etc/fstab
    /etc/lvm/backup
    /etc/lvm/archive
    /tmp
    "

    # File types:
    # (1) file belongs to a package
    # (2) file does NOT belog to a package
    # (3) file needs to be excluded

    list_installed_pkgs_in $active_sysroot_dir $existing_pkgs

    find_files $passive_sysroot_dir $exclude | while read f
    do
        local _filepath="${f#$passive_sysroot_dir}"
        diff -qN "${passive_sysroot_dir}${_filepath}" "${passive_sysroot_init_dir}${_filepath}" &>/dev/null
        local _diff=$? # 0 if the same, 1 if different, 2 if one/both missing
        if [ ! -f "${passive_sysroot_init_dir}${_filepath}" -o $_diff -ne 0 ] ; then
            # New file (not in initial image)
            local pkgs=$(file_in_package "$passive_sysroot_dir" "$_filepath")
            if [ -z "$pkgs" ]; then
                # if (2) then copy
                bury_copy "$f" "${active_sysroot_dir}${_filepath}"
            else
                # if (1) then 
                #   if (pkg present in new slot) then
                #     copy file
                #   else
                #     dpkg-repackage on old slot && reinstall on new slow
                # * This should take care of the package conflicts like
                # * monit/jdoo etc. and will install new versions/configs
                for pkg in $(echo "$pkgs" | tr -d ' ' | tr ',' '\n')
                do
                    if grep -Fxq "$pkg" $existing_pkgs ; then
                        bury_copy "$f" "${active_sysroot_dir}${_filepath}"
                    else
                        echo $pkg >> $reinstall_pkgs
                    fi
                done
            fi
        fi
    done
    sort $reinstall_pkgs -uo $reinstall_pkgs 
}

reinstall_pkgs()
{
    local _old="/mnt/old"
    local mnt_old="${active_sysroot_dir}${_old}"
    local new_pkgs=
    while read line; do new_pkgs="$new_pkgs $line"; done < $reinstall_pkgs;

    if [ -f "${passive_sysroot_dir}/var/lib/dpkg/status" ]; then
        mkdir -p $mnt_old  && mount --bind $passive_sysroot_dir $mnt_old
    fi

    upgrade_reinstall_packages "$new_pkgs" "$active_sysroot_dir" "$_old"
    umount $mnt_old
}


