#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to generate JSON file from lsblk output with ID_PATH_TAG, excluding loop* devices
# This file contains code generated by Grok, created by xAI.

USAGE() {
    echo "$ocs - To generate JSON file from lsblk output with ID_PATH_TAG, excluding loop* devices"
    echo "Usage:"
    echo "To run $ocs:"
    echo "$ocs OUTPUT_FILE"
    echo
} # end of USAGE

####################
### Main program ###
####################

ocs_file="$0"
ocs=`basename $ocs_file`
#
while [ $# -gt 0 ]; do
 case "$1" in
   -*)     echo "${0}: ${1}: invalid option" >&2
           USAGE >& 2
           exit 2 ;;
   *)      break ;;
 esac
done
ocs_out_f="$1"

if [ -z "$ocs_out_f" ]; then
  USAGE
  exit 1
fi

# Ensure udev has processed all devices
udevadm settle

# Run lsblk with JSON output, excluding loop, ram, and rom devices (-e 1,7,11)
lsblk_output=$(LC_ALL=C lsblk -J -o name,type,size,model,fstype,serial,label -e 1,7,11 -x name 2>/dev/null)

# Check if lsblk output is empty
if [ -z "$lsblk_output" ] || [ "$(echo "$lsblk_output" | jq '.blockdevices | length')" -eq 0 ]; then
    echo "[]" > $ocs_out_f
    exit 0
fi

# Initialize JSON array
json_array="[]"

# Parse lsblk JSON output and process each device
while IFS= read -r device; do
    # Extract fields from lsblk JSON
    name=$(echo "$device" | jq -r '.name')
    
    # Skip devices starting with "loop"
    if [[ "$name" == loop* ]]; then
        continue
    fi
    
    # Replace spaces with underscores
    type=$(echo "$device" | jq -r '.type' | tr ' ' '_')
    size=$(echo "$device" | jq -r '.size // ""' | tr ' ' '_')
    model=$(echo "$device" | jq -r '.model // ""' | tr ' ' '_')
    fstype=$(echo "$device" | jq -r '.fstype // ""' | tr ' ' '_')
    serial=$(echo "$device" | jq -r '.serial // ""' | tr ' ' '_')
    label=$(echo "$device" | jq -r '.label // ""' | tr ' ' '_')
    
    # Get the full device path (e.g., /dev/nvme0n2p2)
    dev_path="/dev/$name"
    
    # Query udevadm for ID_PATH_TAG
    id_path_tag=$(udevadm info --query=property --name="$dev_path" 2>/dev/null | grep '^ID_PATH_TAG=' | cut -d'=' -f2 | tr ' ' '_')
    
    # If ID_PATH_TAG is empty, set it to "N/A"
    id_path_tag=${id_path_tag:-N/A}
    
    # Create JSON object for the device
    json_object=$(jq -n \
        --arg name "$name" \
        --arg type "$type" \
        --arg size "$size" \
        --arg model "$model" \
        --arg fstype "$fstype" \
        --arg serial "$serial" \
        --arg label "$label" \
        --arg id_path_tag "$id_path_tag" \
        '{name: $name, type: $type, size: $size, model: $model, fstype: $fstype, serial: $serial, label: $label, id_path_tag: $id_path_tag}')
    
    # Append JSON object to array
    json_array=$(echo "$json_array" | jq --argjson obj "$json_object" '. + [$obj]')
done <<< "$(echo "$lsblk_output" | jq -c '.blockdevices[]')"

# Output formatted JSON to $ocs_out_f
echo "$json_array" | jq . > $ocs_out_f
