#!/usr/bin/ksh

# See what prtvtoc thinks is free (not always reliable).
for DISK in /dev/dsk/c[0-9]*t[0-9]*d[0-9]*s2
do
	echo "${DISK} \c"
	prtvtoc -f ${DISK} 2>&1
done

# Calculate what really is free.
for DISK in /dev/dsk/c[0-9]*t[0-9]*d[0-9]*s2
do
	prtvtoc ${DISK} 2>/dev/null
done | awk 'BEGIN{True=0;False=1}{
if ( ( $3 == "partition" ) && ( $4 == "map" ) )
	{
	split($2, Path, "/")
	DiskName = Path[4]
	}
else
if ( ( $2 == "Unallocated" ) && ( $3 == "space:" ) )
	{
	# Start of Unallocated Space: data.
	FreeSpace = True
	}
else
if ( $0 == "*" )
	{
	# End of Unallocated Space: data.
	# This matches several lines but that does not matter.
	FreeSpace = False
	}
else
if ( ( $2 == "First" ) || ( $2 == "Sector" ) )
        {
        # Ignore useless lines.
        # *       First     Sector    Last
        # *       Sector     Count    Sector
        }
else
if ( FreeSpace == True )
	{
	# Within Unallocated Space: data.
	First = $2
	Count = $3
	Last = $4
	TotalCount = TotalCount + Count
	# Assuming 512 byte sectors.
	FreeSize = Count / 2
	printf("Disk %s free space %4d.%03d MB\n", DiskName, FreeSize / 1000, FreeSize % 1000)
	}
else
	{
	# Not interested.
	}
}
END {
	FreeSize = TotalCount / 2
	printf("\nTotal free space %4d.%03d MB\n", FreeSize / 1000, FreeSize % 1000)
}'

