#!/bin/sh
# This script is based on /etc/hotplug.d/block/22_sah_prpl_layout script
# but adapted to be run during the preinit phase.
# Create symlinks, converting vendor specific flash layout to the prpl flash layout

############################################################################################################################
######################################################### Constants ########################################################
############################################################################################################################
CONVERT_PARTLABEL_SCRIPT_VENDOR="/usr/bin/convert_partlabel_to_prpl"

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

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

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

  mkdir -p $PRPL_LAYOUT_PART_PATH
  mkdir -p /dev/disk/by-partlabel

  # Go over all the partitions with the use of blkid
  for DEVNAME_1_PATH in /dev/mmcblk[0-9]p* /dev/ubi*; do
    DEVNAME=${DEVNAME_1_PATH##*/}
    DEVNAME_2_PATH=$DEVNAME_1_PATH

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

    PARTLABEL=
    if [[ "$DEVNAME" == *"mmc"* ]]; then
      PARTLABEL=`blkid --match-tag PARTLABEL -o value "$DEVNAME_1_PATH" 2> /dev/null || true`
    elif [[ "$DEVNAME" == *"ubi"* ]]; 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_2_PATH=`echo "$DEVNAME_1_PATH" | sed 's/ubi/ubiblock/' 2> /dev/null || true`
    fi

    [ -z "$PARTLABEL" ] && continue

    DEVNAME_PATH=$DEVNAME_1_PATH
    DEVNAME_PATH_FIT=$DEVNAME_2_PATH

    # Get the prpl PARTLABEL
    PRPL_PARTLABEL=`$CONVERT_PARTLABEL_SCRIPT_VENDOR $PARTLABEL 2> /dev/null || true`
    if [ -z "$PRPL_PARTLABEL" ]; then
      # Extra layer in case there is no vendor converter script with common cases (only for rootfs partitions)
      # Usually the rootfs-active is labeled as rootfs, so this extra convertion cover such cases
      if [ "$PARTLABEL" == "rootfs" ]; then
        # If the string is exactly rootfs
        PRPL_PARTLABEL="$PRPL_LAYOUT_ROOTFS_ACTIVE"
      elif [[ "$PARTLABEL" == *"rootfs"* ]] && [[ "$PARTLABEL" == *"active"* ]]; then
        # If the string has "rootfs" and contains the word "active"
        PRPL_PARTLABEL="$PRPL_LAYOUT_ROOTFS_ACTIVE"
      elif [[ "$PARTLABEL" == *"rootfs"* ]] && [[ "$PARTLABEL" == *"inactive"* ]]; then
        # If the string has "rootfs" and contains the word "inactive"
        PRPL_PARTLABEL="$PRPL_LAYOUT_ROOTFS_INACTIVE"
      elif [[ "$PARTLABEL" == *"rootfs"* ]] && [[ "$PARTLABEL" != *"data"* ]]; then
        # If the string has "rootfs" and doesn't contain the word "data"
        PRPL_PARTLABEL="$PRPL_LAYOUT_ROOTFS_INACTIVE"
      elif [[ "$PARTLABEL" == *"rootfs"* ]] && [[ "$PARTLABEL" == *"data"* ]]; then
        # If the string has "rootfs" and contains the word "data"
        PRPL_PARTLABEL="$PRPL_LAYOUT_ROOTFS_DATA"
      elif is_label_prpl_layout "$PARTLABEL"; then 
        # If the label is the same as the prpl layout
        PRPL_PARTLABEL="$PARTLABEL"
      fi
    fi

    if ! is_label_prpl_layout "$PRPL_PARTLABEL"; then 
      # Continue if it was not possible to convert the partition name to the prpl equivalent
      continue
    fi

    # Create symbolic links
    PRPL_PART_PATH="$PRPL_LAYOUT_PART_PATH/$PRPL_PARTLABEL"
    PRPL_PARTBYLABEL_PATH="/dev/disk/by-partlabel/$PRPL_PARTLABEL"

    # If the partition already exists in the prpl layout then proceed with next partition
    [ -f "$PRPL_PART_PATH" ] && continue

    [ ! -L "$PRPL_PART_PATH" ] && ln -s "$DEVNAME_PATH" "$PRPL_PART_PATH"
    [ ! -L "$PRPL_PARTBYLABEL_PATH" ] && ln -s "$DEVNAME_PATH" "$PRPL_PARTBYLABEL_PATH"
    if [ "$DEVNAME_PATH" != "$DEVNAME_PATH_FIT" ]; then
      [ ! -L "${PRPL_PART_PATH}_1" ] && ln -s "$DEVNAME_PATH_FIT" "${PRPL_PART_PATH}_1"
    fi
  done
}

boot_hook_add preinit_main do_prpl_layout
