#!/bin/sh
# 
# partinfo      copyright Ralf Flaxa 1993
#
# A tool to obtain information on harddisk partitions
# 

callname=`basename $0 `

#
# Functions
#

function usage() {
echo " "
echo "$callname prints information on harddisk partitions"
echo "  Usage: $callname [-s] [-tn] [-tw] [-v] [partition]"
echo "         $callname [-s] [-tn] [-tw] [-v] [-a] [disk]"
echo "  If partition/disk is omitted then the current partition/disk is used"
echo "  Example: $callname -s -tn /dev/hda1"
echo "       Prints size and numeric type of partition /dev/hda1"
echo "  Example: $callname -s -a"
echo "       Prints size of all partitions on current disk"
echo " "
}

function badoption() {
echo " "
echo "Bad Option $@"
usage;
}

function wholedisk() {
echo $1 | cut -c 1-8
}

function validdisk() {
(fdisk $disk << EOF
q
EOF
) >& /dev/null
return $?
}

function partdetect() {
(fdisk $disk << EOF
p
q
EOF
) | grep "$1" >& /dev/null 
return $?
}

function partshow() {
(fdisk $disk << EOF
p
q
EOF
echo "") | fgrep "$1" | sed '/Disk/d'
}

function whichpart() {
(fdisk $disk << EOF
p
q
EOF
echo "") | grep "$disk" | sed '/Disk/d' | cut -c 1-10
}

function allpart() {
(fdisk $disk << EOF
p
q
EOF
echo "") | grep "$disk" | sed '/Disk/d' | cut -c 1-10 | xargs show
}


function partsize () { 
partshow $1 | fgrep $1 | cut -c 38-45 
}

function parttypeword () { 
partshow $1 | fgrep $1 | cut -c 53- 
}

function parttypenum () { 
partshow $1 | fgrep $1 | cut -c 49-51 
}

function show () {

	validdisk $1 || (echo "Can not open $disk to read partition table";exit 1)

 	partdetect $1 || (echo "No Partition $1 found on $disk ";exit 1)

	if [ "$VERBOSE" = "true" ]; then
		echo "Partition $1 "
	fi


	if [ "$SHOWTYPENUM" = "true" ]; then
		if [ "$VERBOSE" = "true" ]; then
			echo -n "Numeric Type : "
		fi 
		echo `parttypenum $1`
	fi

	if [ "$SHOWSIZE" = "true" ]; then
		if [ "$VERBOSE" = "true" ]; then
			echo -n "Size (in KB) : "
		fi
		echo `partsize $1` 
	fi

	if [ "$SHOWTYPEWORD" = "true" ]; then
		if [ "$VERBOSE" = "true" ]; then
			echo -n "Verbose Type : "
		fi
		echo `parttypeword $1` 
	fi
}


#
# Program 
#

# default values

SHOWTYPEWORD=false
SHOWTYPENUM=false
SHOWSIZE=false
VERBOSE=false

# Parse Arguments

while [ 0 ]; do
	if [ "$1" = '-h' -o "$1" = '-?' ]; then
		usage;
		exit 1
        elif [ $# -gt 0 -a "$1" = "-s" ]; then
                SHOWSIZE=true;
                shift;
                continue;
        elif [ $# -gt 0 -a "$1" = "-v" ]; then
                VERBOSE=true;
                shift;
                continue;
        elif [ $# -gt 0 -a "$1" = "-tn" ]; then
                SHOWTYPENUM=true;
                shift;
                continue;
        elif [ $# -gt 0 -a "$1" = "-a" ]; then
                SHOWALL=true;
		disk=$2;
		whichpart;
                exit 0;
        elif [ $# -gt 0 -a "$1" = "-tw" ]; then
                SHOWTYPEWORD="true";
                shift;
                continue;
        elif [ $# -gt 0 ]; then
                show $1;
                shift;
                continue;
        else
                exit 0
        fi
done;



if [ ! -b $1 ]; then
	badoption;
	exit 1
fi

if [ "$SHOWALL" = "true" ]; then
	disk=$1
	whichpart $1 | xargs echo;
	exit 0
else
	disk=`wholedisk $1`
	show $1
	exit 0
fi




