#!/bin/bash

if [ -z "$1" ]; then
  echo "Usage: $0 <file_path>"
  exit 1
fi

filename=$(realpath "$1" 2>/dev/null || readlink -f "$1" 2>/dev/null)

if [ ! -e "$filename" ]; then
  echo "Error: File '$1' does not exist or cannot be resolved to an absolute path."
  exit 1
fi

inode=$(ls -i "$filename" | awk '{print $1}')
echo "Inode number of '$filename': $inode"

found_files=$(find /var/lib/ironic/ -inum "$inode" 2>/dev/null)

if [ -z "$found_files" ]; then
  echo "No file with inode $inode found in /var/lib/ironic/"
else
  echo "Found files in /var/lib/ironic/:"
  echo "$found_files"

  echo "Deleting the found files..."
  rm -f $found_files

  if [ $? -eq 0 ]; then
    echo "Files deleted successfully."
  else
    echo "Error: Failed to delete files."
  fi
fi
