#!/usr/bin/bash
#
# (C) 2025      Axel Konrad <ak-li@siduction.org>
#     2008,2009 Joaquim Boura <x-un-i@berlios.de>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# On Debian GNU/Linux systems, the text of the GPL license can be
# found in /usr/share/common-licenses/GPL.
#
#--------------------------------------------------------------------------
# Shell replacement for disk.py
#
# We emulate the different calls for disk.py
# Only option -u needs a parameter.
#
#
#
# exec 2>/dev/null
myName=$(basename "$0")
#--------------------------------------------------------------
# functions
#--------------------------------------------------------------
usage()
{
	echo ""
	echo "Usage: $myName [-d|--disk]|[-p|--partiton]|[-n|--nousb]|[-u <sdX> | --usb=<sdX>]|[-h|--help]"
	echo "-d|--disk           print all disk devices"
	echo "-p|--partition      print all partition devices"
	echo "-n|--nousb          print all disks without usb"
	echo "-u|--usb <device>   print usb device (-u sdX or --usb=sdX)"
	echo "-h|--help           print this help"
	echo ""
}
#--------------------------------------------------------------
error()
{
    echo "$@"
}

#--------------------------------------------------------------
find_disks()
{
disks=$(lsblk -no PATH -Q 'TYPE=="disk"')
}

#--------------------------------------------------------------
partition_or_volume_info()
{
	local dev="$1"
	local mypar="$2"
	DEVNAME="$(/sbin/blkid -o device "$dev")"
	if [ -z "$DEVNAME" ] ; then
		return
	fi

	ID_FS_TYPE="$(/sbin/blkid -s TYPE -o value "$dev")"
	if [ "$ID_FS_TYPE" = "LVM2_member" ]; then
		return
	fi

	ID_FS_UUID="$(/sbin/blkid -o value -s UUID "$dev")"

	echo "$DEVNAME,$ID_FS_TYPE,filesystem,$ID_FS_UUID,$mypar"
}

#--------------------------------------------------------------
parameter_p()
{
	find_disks
	for disk in $disks
	do
		parts=$(lsblk -no PATH -Q 'TYPE=="part"' "$disk")

		for part in $parts
		do
			partition_or_volume_info "$part" norm
		done
	done

	volumes=$(ls /dev/mapper/* | grep -E -iv "\/CONTROL$|$FLL_DISTRO_NAME-live")
	for vol in $volumes; do
		partition_or_volume_info "$vol" lvm
	done
}

#--------------------------------------------------------------
parameter_d()
{
	find_disks
	echo "$disks" | grep -v "/dev/dm"
}

#--------------------------------------------------------------
parameter_n()
{
	disks=$(parameter_d)
	for disk in $disks; do
		readlink -f /sys/block/"${disk##/dev/}"/device | \
			grep -q usb || printf "%s\n" "$disk"
	done
}

#--------------------------------------------------------------
parameter_u()
{
	local mydisk=$1
	readlink -f /sys/block/"$mydisk"/device | \
		grep -q usb && printf "/dev/%s\n" "$mydisk"
}
#--------------------------------------------------------------

TEMP=$(getopt -o dhnpu: \
        --long disk,help,nousb,partition,usb: \
        -n "$(basename "$0")" -- "${@}")

if [ "$?" -ne 0 ]; then
	error 255 "getopt terminated abnormally"
	exit 255
fi

eval set -- "$TEMP"

while true ; do
	case "$1" in
		-d|--disk)
			PRINT_DISK=1
			shift
			;;
		-n|--nousb)
			PRINT_NOUSB=1
			shift
			;;
		-p|--partition)
			PRINT_PARTITION=1
			shift
			;;
		-u|--usb)
			PRINT_USB=1
			IS_USB_DISK="$2"
			shift 2
			;;
		-h|--help)
			usage
			exit 0
			;;
		--)
			shift
			break
			;;
		*)
			error 255 "getopt internal error"
			exit 255
			;;
	esac
done

#--------------------------------------------------------------
# read distro defaults
. /etc/default/distro

if [ -n "$PRINT_DISK" ]; then
	parameter_d
elif [ -n "$PRINT_NOUSB" ]; then
	parameter_n
elif [ -n "$PRINT_PARTITION" ]; then
	parameter_p
elif [ -n "$PRINT_USB" ]; then
	parameter_u "$IS_USB_DISK"
else 
	usage
fi
