#!/bin/sh
# This script is based on /etc/hotplug.d/block/21_sah_read_version script
# but adapted to be run during the preinit phase.
# Read the version from the FIT header and add it to the /tmp/sw-versions file

############################################################################################################################
######################################################### Constants ########################################################
############################################################################################################################
VERSIONS_FILE="/tmp/sw-versions"
PRPL_VERSIONS_FILE="/tmp/sw-versions-prpl"
CONVERT_PARTLABEL_SCRIPT_VENDOR="/usr/bin/convert_partlabel_to_prpl"

############################################################################################################################
############################################################################################################################
############################################################################################################################

. /lib/functions/prpl-layout-functions.sh

do_read_version() {
  if ! [ -f $CONVERT_PARTLABEL_SCRIPT_VENDOR ]; then
    echo "The required script: '$CONVERT_PARTLABEL_SCRIPT_VENDOR' to run '21_sah_read_version' is not present. Exiting."
    return
  fi

  if ! command -v fit_tools 2>&1 >/dev/null
  then
    echo "The required 'fit_tools' is not present to run '21_sah_read_version'. Exiting."
    return
  fi

  for DEVNAME_PATH in /dev/mmcblk[0-9]p* /dev/ubi*; do
    DEVNAME_PATH_FIT=$DEVNAME_PATH
    DEVNAME=${DEVNAME_PATH##*/}

    # If the device it's an ubiblock don't do anything
    [[ "$DEVNAME" == *"ubiblock"* ]] && continue

    PARTLABEL=
    if [[ "$DEVNAME" == *"mmc"* ]]; then
      PARTLABEL=`blkid --match-tag PARTLABEL -o value "$DEVNAME_PATH" 2> /dev/null || true`
    elif [[ "$DEVNAME" == *"ubi"* ]] && [ -d "/sys/class/ubi/$DEVNAME/" ]; then
      # For UBI devices sometimes they might have two devices ubi and ubiblock, so let's use just the ubi
      PARTLABEL=`cat /sys/class/ubi/$DEVNAME/name 2> /dev/null || true`
      DEVNAME_PATH_FIT=`echo "$DEVNAME_PATH" | sed 's/ubi/ubiblock/' 2> /dev/null || true`
      [ -z "$DEVNAME_PATH_FIT" ] && continue
    fi
    # If no partlabel is found, skip to the next partition
    [ -z "$PARTLABEL" ] && continue

    # Get the partlabel and check if it is the same as the prpl label for the partition
    # This can be done by the convert_partlabel_to_prpl script
    PRPL_PARTLABEL=`$CONVERT_PARTLABEL_SCRIPT_VENDOR $PARTLABEL 2> /dev/null || true`
    # If PRPL_PARTLABEL is empty, check if the label matches the PRPL layout
    if [ -z "$PRPL_PARTLABEL" ]; then
        if is_label_prpl_layout "$PARTLABEL"; then 
          # If the label is the same as the prpl layout, set PRPL_PARTLABEL to the PARTLABEL
          PRPL_PARTLABEL="$PARTLABEL"
        fi
    fi
    # If no prpl partlabel conversion is found, skip to the next partition
    [ -z "$PRPL_PARTLABEL" ] && continue

    # Try do create the ubiblock (if it's not an ubi device it will fail and jump to the next line)
    ubiblock --create $DEVNAME_PATH 2> /dev/null || true

    # Read the block device version using fit_tools
    FIT_TOOLS_VERSION=`fit_tools info $DEVNAME_PATH_FIT -p s,/version 2> /dev/null || true`

    # Try do remove the ubiblock (if it's not an ubiblock device it will fail and jump to the next line)
    ubiblock --remove $DEVNAME_PATH 2> /dev/null || true

    # If no version was found, skip to the next partition
    [ -z "$FIT_TOOLS_VERSION" ] && continue

    # Extract the PROP VALUE from FIT_TOOLS_VERSION
    PROP_VALUE=`echo "$FIT_TOOLS_VERSION" | grep "PROP VALUE" | awk -F": " '{print $2}' 2> /dev/null || true`
    # If PROP_VALUE is empty, skip this partition
    [ -z "$PROP_VALUE" ] && continue

    # Check if the version is already written to the VERSIONS_FILE
    if ! grep -q "$PARTLABEL" "$VERSIONS_FILE"; then
        # If not, add it to the VERSIONS_FILE
        echo "$PARTLABEL $PROP_VALUE" >> "$VERSIONS_FILE"
    fi

    # Check if the version is already written to the PRPL_VERSIONS_FILE
    if ! grep -q "$PRPL_PARTLABEL" "$PRPL_VERSIONS_FILE"; then
        # If not, add it to the PRPL_VERSIONS_FILE
        echo "$PRPL_PARTLABEL $PROP_VALUE" >> "$PRPL_VERSIONS_FILE"
    fi
  done
}

boot_hook_add preinit_main do_read_version
