#!/bin/sh

#As prpl uses a different flashing layout a conversion table might be needed.

. /lib/prpl-layout-partlabels #Environment variables with the partitioning standard installed by the board-configurator package

PARTLABEL=$1
[ -z "$PARTLABEL" ] && exit 1

bootconfig_part_id="0"

if [ -e /proc/upgrade_info/trybit ]
then
    age0=$(cat /proc/boot_info/bootconfig0/age)
    age1=$(cat /proc/boot_info/bootconfig1/age)
    if [ -e /proc/upgrade_info/trymode_inprogress ]
    then
        if [ $age0 -gt $age1 ]
        then
            bootconfig_part_id="1"
        fi
    else
        if [ $age0 -le $age1 ]
        then
            bootconfig_part_id="1"
        fi
    fi
fi

# Initially active copy of rootfs (being executed)
if [ "$(cat /proc/boot_info/bootconfig${bootconfig_part_id}/rootfs/primaryboot)" -eq 0 ]; then
    VENDOR_ROOTFS_ACTIVE="rootfs"
else
    VENDOR_ROOTFS_ACTIVE="rootfs_1"
fi

# Main U-Boot copy
if [ "$(cat /proc/boot_info/bootconfig${bootconfig_part_id}/0:APPSBL/primaryboot)" -eq 0 ]; then
    VENDOR_UBOOT_1="0:APPSBL"
else
    VENDOR_UBOOT_1="0:APPSBL_1"
fi

# Initially active copy of kernel (being executed)
if [ "$(cat /proc/boot_info/bootconfig${bootconfig_part_id}/0:HLOS/primaryboot)" -eq 0 ]; then
    VENDOR_KERNEL_ACTIVE="0:HLOS"
else
    VENDOR_KERNEL_ACTIVE="0:HLOS_1"
fi

VENDOR_ROOTFS_INACTIVE="$(cat /proc/boot_info/bootconfig${bootconfig_part_id}/rootfs/upgradepartition)"           #Inactive copy of rootfs: backup if rootfs is corrupt or target for writing upgrades
VENDOR_UBOOT_2="$(cat /proc/boot_info/bootconfig${bootconfig_part_id}/0:APPSBL/upgradepartition)"                 #Second U-Boot copy
VENDOR_KERNEL_INACTIVE="$(cat /proc/boot_info/bootconfig${bootconfig_part_id}/0:HLOS/upgradepartition)"           #Initially inactive copy of kernel: backup if kernel is corrupt or target for writing upgrades

VENDOR_ROOTFS_DATA="rootfs_data"            #default openWRT overlayfs partition. SHOULD NOT BE PRESENT ON PRODUCTION 
VENDOR_LCM_DATA="lcm_data"                  #Storage for LCM application (OCI images + data)
VENDOR_SECURESTORE="securestore"            #Store non-volatile configuration
VENDOR_MFGDATA="mfgdata"                    #Manufacturing data

PRPL_PART=""
#All PRPL_LAYOUT_ variables come from the board-configurator's script /lib/prpl-layout-partlabels
case "$PARTLABEL" in
    "$VENDOR_ROOTFS_ACTIVE")
	PRPL_PART="$PRPL_LAYOUT_ROOTFS_ACTIVE"
        ;;
    "$VENDOR_ROOTFS_INACTIVE")
	PRPL_PART="$PRPL_LAYOUT_ROOTFS_INACTIVE"
	;;
    "$VENDOR_ROOTFS_DATA")
	PRPL_PART="$PRPL_LAYOUT_ROOTFS_DATA"
	;;
    "$VENDOR_UBOOT_1")
	PRPL_PART="$PRPL_LAYOUT_UBOOT_1"
	;;
    "$VENDOR_UBOOT_2")
	PRPL_PART="$PRPL_LAYOUT_UBOOT_2"
	;;
    "$VENDOR_KERNEL_ACTIVE")
	PRPL_PART="$PRPL_LAYOUT_KERNEL_ACTIVE"
	;;
    "$VENDOR_KERNEL_INACTIVE")
	PRPL_PART="$PRPL_LAYOUT_KERNEL_INACTIVE"
	;;
    "$VENDOR_LCM_DATA")
	PRPL_PART="$PRPL_LAYOUT_LCM_DATA"
	;;
    "$VENDOR_SECURESTORE")
	PRPL_PART="$PRPL_LAYOUT_SECURESTORE"
	;;
    "$VENDOR_MFGDATA")
	PRPL_PART="$PRPL_LAYOUT_MFGDATA"
	;;
esac

echo "$PRPL_PART"
