# -- perl --

#************************************************************
#		  DHAVAL RAJ
#	LINACCESS PROJECT For PROFESSOR MANN
#
#
# The following script is for the command "df"
# It has following options
# -all : include file system having 0 blocks        
# -h   : print sizes in human readable format
# -H   : likewise, but use powers of 1000 not 1024
# -T   : print file system type
# -i   : list inode information instead of block usage
# 
#
#************************************************************

# Jun 14 2002
# modified by Suryadi Suryadi for distribution

$i=0;
$flag=0;
if (@ARGV[0] eq "--help")
{
    &ExtendedHelp;
    exit;
}

while(@ARGV[$i])
{
    &CheckOptions(@ARGV[$i]);
    $i=$i+1;
}

$try=`df -h @ARGV > ~/.garbage`;
print("\n");
&PrintOnScreen($flag);
print("\n");
$try=`rm ~/.garbage`;





#*************************************************
#****************** PROCEDURES *******************
#*************************************************


sub PrintOnScreen
{
    $DIR = $ENV{"HOME"};
    my($IncludeType)=@_;
    open(MYFILE,"$DIR/.garbage") or die "Can't Open File  \n";
    if ($IncludeType ==1)
    {
	print("FileSystem Type   Use% Mount\n"); 
    }
    else
    {
	print("FileSystem Used Avai Use% Moun\n");
    }
    $line=<MYFILE>;
    $line=<MYFILE>;
    while($line ne "")
    {
	$line=~s/( +)/ /g;
	@line1=split(/ /,$line);
	if ($IncludeType ==1)
	{
	    chomp($line1[0]);
	    chomp($line1[6]);
	    PrintFileName($line1[0],10);
	    if ($line1[1] eq "")
	    {
		$line=<MYFILE>;
		$line=~s/( +)/ /g;
		@line1=split(/ /,$line);

	    }
	    printf(" %-6s %-4s ",$line1[1],$line1[5]);
	    PrintFileName($line1[6],8);
	    print("\n");
	}
	else
	{
	    chomp($line1[5]);
	    PrintFileName($line1[0],10);
	    printf(" %-4s %-4s %-4s ",$line1[2],$line1[3],$line1[4]);
	    PrintFileName($line1[5],4);
	    print("\n");
	}
	$line=<MYFILE>;
    }
}


sub CheckOptions
{
    my($Option)=@_;
    if ($Option eq "-T")
    {
	$flag=1;
    }
    if (($Option ne "-all") && ($Option ne "-T") && ($Option ne "-h") && ($Option ne "-H") && ($Option ne "-i"))
    {
	&Help;
	exit;
    }
}

sub Help
{
    print("\nUSAGE: df [-all] [-i] [-h] [-H] [-T] [--help] \n\n");
}

sub ExtendedHelp
{
    print("\n\ndf - Report File System Disk space usage\n");
    print("It has following options\n");
    print("-all : include file system having 0 blocks\n");
    print("-h   : print sizes in human readable format\n");
    print("-H   : likewise, but use powers of 1000 not 1024\n");
    print("-T   : print file system type\n");
    print("-i   : list inode information instead of block usage\n\n");
}

sub PrintFileName {
	my($name,$NumberOfTimes)=@_;
	chomp($name);
	@name1=split(//,$name);
	for($i=0;$i<$NumberOfTimes;$i++)
	{
		if ($name1[$i] eq "")
		{
			print(" ");
		}
		else
		{
			printf("%s",$name1[$i]);
		}
	} 
}
