#!/bin/bash
# License: GPL
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to show the kernel messages about removable disks
# //NOTE// This program is used to work with screen.

#
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# Load the config in ocs-live.conf. This is specially for Clonezilla live. It will overwrite some settings of /etc/drbl/drbl-ocs.conf, such as $DIA...
[ -e "/etc/ocs/ocs-live.conf" ] && . /etc/ocs/ocs-live.conf

# Settings

#
ask_and_load_lang_set

#
now="$(LC_ALL=C date +%Y/%m/%d" "%T)"
echo "$now"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo -e "$msg_insert_storage_dev_now"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "Scanning devices... Available disk(s) on this machine:"
# Show it like:
# NAME    TYPE  SIZE MODEL          FSTYPE SERIAL ID_PATH_TAG
# nvme0n1 disk   64G VMware Virtual NVMe Disk              VMware NVME_0000 pci-0000_0b_00_0-nvme-1
# nvme0n2 disk 1000G VMware Virtual NVMe Disk              VMware NVME_0000 pci-0000_0b_00_0-nvme-2
# sda     disk   60G VMware Virtual SATA Hard Drive ext4   00000000000000000001 pci-0000_02_04_0-ata-1_0
echo "==================================="
lsblk_output=$(LC_ALL=C lsblk -d -o name,type,size,model,fstype,serial -e 1,7,11 -x name -n)
# Print header with ID_PATH_TAG added
echo "NAME TYPE SIZE MODEL FSTYPE SERIAL ID_PATH_TAG"
# Process each device from lsblk output
echo "$lsblk_output" | while IFS= read -r line; do
  # Extract the device name (first column)
  name=$(echo "$line" | awk '{print $1}')
  # Get the full device path (e.g., /dev/sda)
  device="/dev/$name"
  # Query udevadm for ID_PATH_TAG
  id_path_tag=$(udevadm info --query=property --name="$device" | grep '^ID_PATH_TAG=' | cut -d'=' -f2)
  # If ID_PATH_TAG is empty, set it to "N/A"
  id_path_tag=${id_path_tag:-N/A}
  # Append ID_PATH_TAG to the lsblk line
  echo "$line $id_path_tag"
done
echo "==================================="
echo -en '\033[1;33m'
echo Updates periodically. Press Ctrl-C to exit this window.
echo -en '\033[0;39m'
