#!/usr/bin/env bash

set -euo pipefail
export LC_ALL=C

debug_enabled="${DEBUG:-0}"

log_info() {
  echo "[INFO] $*"
}

log_warn() {
  echo "[WARNING] $*" >&2
}

die() {
  echo "[ERROR] $*" >&2
  exit 1
}

debug() {
  if [[ "$debug_enabled" == "1" ]]; then
    echo "[DEBUG] $*" >&2
  fi
}

usage() {
  echo "Usage: $0 <device> <partition_number> [apply]" >&2
  echo "Without 'apply', the script only displays and validates the resize plan." >&2
}

if (($# < 2 || $# > 3)); then
  usage
  exit 2
fi

device="$1"
partition_number="$2"
mode="${3:-dry-run}"

[[ "$partition_number" =~ ^[1-9][0-9]*$ ]] || \
  die "partition number must be a positive integer."
case "$mode" in
  dry-run | apply) ;;
  *) die "the optional third argument must be 'apply'." ;;
esac

command -v parted >/dev/null 2>&1 || die "parted is not installed."
command -v lsblk >/dev/null 2>&1 || die "lsblk is not installed."
[[ -b "$device" ]] || die "device is not a block device: $device"

if [[ "$device" =~ [0-9]$ ]]; then
  partition="${device}p${partition_number}"
else
  partition="${device}${partition_number}"
fi
[[ -b "$partition" ]] || die "partition is not a block device: $partition"

parent_name="$(lsblk -ndo PKNAME "$partition" 2>/dev/null || true)"
device_name="$(basename "$device")"
if [[ -n "$parent_name" && "$parent_name" != "$device_name" ]]; then
  die "$partition belongs to /dev/$parent_name, not $device."
fi

partition_table="$(parted -sm "$device" unit MiB print)" || \
  die "parted could not read the partition table on $device."
debug "$partition_table"
if ! awk -F: -v number="$partition_number" '$1 == number { found = 1 } END { exit !found }' \
  <<<"$partition_table"; then
  die "partition $partition_number is not present in $device's partition table."
fi

current_bytes="$(lsblk -bdno SIZE "$partition")"
device_bytes="$(lsblk -bdno SIZE "$device")"
[[ "$current_bytes" =~ ^[0-9]+$ ]] || die "could not determine the partition size."
[[ "$device_bytes" =~ ^[0-9]+$ ]] || die "could not determine the device size."

current_mib=$((current_bytes / 1024 / 1024))
device_mib=$((device_bytes / 1024 / 1024))

log_info "Device: $device (${device_mib} MiB)"
log_info "Partition: $partition (${current_mib} MiB)"
log_info "Plan: move partition $partition_number's end boundary to 100% of $device."

if [[ "$mode" == "dry-run" ]]; then
  log_warn "Dry-run mode: no changes were made."
  log_warn "Run with 'apply' as the third argument to resize the partition table."
  exit 0
fi

((EUID == 0)) || die "apply mode must be run as root."
log_info "Applying partition-table resize..."
parted -s -- "$device" resizepart "$partition_number" 100%

if command -v partprobe >/dev/null 2>&1; then
  partprobe "$device" || log_warn "partprobe could not notify the kernel; a reboot may be required."
fi

new_bytes="$(lsblk -bdno SIZE "$partition" 2>/dev/null || true)"
if [[ "$new_bytes" =~ ^[0-9]+$ ]]; then
  log_info "New partition size reported by the kernel: $((new_bytes / 1024 / 1024)) MiB"
fi

log_warn "The partition table is updated, but the filesystem is not."
log_warn "For ext2/3/4, use: resize2fs $partition"
log_warn "For XFS, mount it and pass its mount point to xfs_growfs."
log_warn "Use the filesystem-specific tool appropriate for your volume layout."
