#!/bin/sh
#
# (C) 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 "$myName is a temporarly replacement for disk.py"
	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 $@
}
#--------------------------------------------------------------

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 [ "x${ID_FS_TYPE}" = "xLVM2_member" ]; then
		return
	fi

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

	printf "${DEVNAME},${ID_FS_TYPE},filesystem,${ID_FS_UUID},$mypar\n"
}

#--------------------------------------------------------------
parameter_p()
{
	disks=`LANG=C fdisk -l  2>/dev/null | grep "Disk /dev" | cut -d: -f1 | cut -d" " -f2`
	# parts=`gdisk -l $disk | fgrep -v "Extended" | fgrep '^/dev/' | cut -d " " -f1 | cut -d "/" -f 3`

	for disk in ${disks}
	do
		parts=$(fdisk -l $disk 2>/dev/null | grep -v "Extended" | \
			grep '^/dev/' | cut -d " " -f1)

		# GPT disks appear in fdisk as a single partition of System GPT
		if [ "${parts}" = "${disk}1" ] && \
		   [ -n "$(fdisk -l $disk 2>/dev/null | awk '/^\/dev\//{if($6 == "GPT"){print "GPT"}}')" ] ; then
			# Extract partition numbers from gdisk
			parts=$(gdisk -l ${disk} 2>/dev/null | awk '/^ +[[:digit:]]/{print "'${disk}'"$1}')
		fi

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

	volumes=`ls /dev/mapper/* | egrep -iv "\/CONTROL$|${FLL_DISTRO_NAME}-live"`
	for vol in $volumes; do
		partition_or_volume_info $vol lvm
	done
}
#--------------------------------------------------------------
parameter_d()
{
	LANG=C fdisk -l 2>/dev/null | grep "Disk /dev" | \
		cut -d: -f1 | cut -d" " -f2 | \
		fgrep -v "/dev/dm"
}
#--------------------------------------------------------------
parameter_n()
{
	disks=`LANG=C fdisk -l 2>/dev/null | grep "Disk /dev" |\
	grep -v "/dev/dm" | cut -d: -f1 | cut -d" " -f2`
	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
