# -- perl --

#************************************************************
#	   DHAVAL RAJ
# LINACCESS PROJECT For PROFESSOR MANN
#************************************************************

# Jun 14 2002
# modified by Suryadi Suryadi for distribution

&MainFunction;


#***************************************************************
#***************************************************************
#                    MAIN FUNCTION
#***************************************************************
#***************************************************************

sub MainFunction
{
 $FFlag=0;
 $SFlag=0;
 if (@ARGV[0] eq "--help")
 {
  &ExtendedHelp;
  exit;
 }


 $NoOfDir=0;
 $i=0;
 $NoOfOpt=0;

 while(@ARGV[$i])
 {
  @Input=split(//,@ARGV[$i]);
  if ($Input[0] eq "-")
  {
   &CheckOptions(@ARGV[$i]);
   $OptionList[$NoOfOpt]=@ARGV[$i];
   if (@ARGV[$i] eq "-F")
   {
    $FFlag=1;
   }
   elsif (@ARGV[$i] eq "-S")
   {
    $SFlag=1;
   }
   $NoOfOpt++;
  }
  else
  {
   $DirList[$NoOfDir]=@ARGV[$i];
   $NoOfDir++;
  }
  $i++;
 }


 if (($FFlag==1) && ($SFlag==1))
 {
  $Command="ls -F -S";
 }
 elsif ($FFlag==1)
 {
  $Command="ls -F";
 }
 elsif ($SFlag==1)
 {
  $Command="ls -S";
 }
 else
 {
  $Command="ls";
 }

 

 if (($NoOfOpt>1) && ($OptionList[0] eq "-F"))
 {
  $OptionList[0]=$OptionList[1];
 }

 if(($NoOfOpt==0) || (($NoOfOpt==1) && ($FFlag==1)))
 {
  @try=`$Command @DirList`;
  print(@try);
  exit;
 }
 else
 {
  &PrintOnScreen;
  exit;
 }
}








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



sub PrintOnScreen
{
# if ($OptionList[0] =~ /^-[klarSt]/)
 #{
  #@try=`$Command -ls $OptionList[0] @DirList > .garbagels`;
  #open(MYFILE,".garbagels") or die "Can't Open File  \n";
  #close(MYFILE);
 #}
 if ($OptionList[0] =~ /^-[larSt]/)
 {
  @try=`$Command $OptionList[0] @DirList > ~/.garbagels`;
  &PrintALL;
 }
# elsif ( $OptionList[0] eq [-alt]) || )
# {
#  @try=`$Command -all @DirList > .garbagels`;
#  &PrintALL;
# }
# elsif ($OptionList[0] eq "-S")
# {
#  @try=`$Command -all -S @DirList > .garbagels`;
#  &PrintALL;
# }
 elsif ($OptionList[0] =~ /^-[arSt]/)
 {
  @try=`$Command $OptionList[0] @DirList`;
  print(@try);
  exit;
 }
 @try=`rm ~/.garbagels`;
}


sub PrintALL
{
 $DIR = $ENV{"HOME"};
 open(MYFILE,"$DIR/.garbagels") or die "Can't Open File  \n";
 if ($NoOfDir > 0)
 {
  $line=<MYFILE>;
  $dir=$line;
  chomp($dir);
  $line=<MYFILE>;
  print("$dir : $line");
 }
 else
 {
  $line=<MYFILE>;
  $dir=`pwd`;
  chomp($dir);
  print("$dir : $line");
 }
 $line=<MYFILE>;
 while($line ne "")
 {
  $line=~s/( +)/ /g;
  @linesplit=split(/ /,$line);
  &PrintPermissions(@linesplit[0]);
  if ($linesplit[4] <= 0)
  { 
   $linesplit[0]=0; 
  }
  else
  { 
   $linesplit[4]=log($linesplit[4])/log(10); 
  }
  printf(" %-1d ",$linesplit[4]);
  &PrintOwner($linesplit[2]);
  &PrintDate(@linesplit[5],@linesplit[6]);
  &PrintFileName(@linesplit[8],12);
  $line=<MYFILE>;
  if (length($line) < 2)
  {
   print ("\n");
   $line=<MYFILE>;
   $dir=$line;
   $line=<MYFILE>;
   chomp($dir);
   print("$dir $line");
   $line=<MYFILE>;
  }
 }

 close(MYFILE);
}


sub CheckOptions {
    my($Option)=@_;
    foreach $char (split(//,$Option))
    {
         if ($char=~ /[^-alkFXSRrt]/)
         { 
   	   	&Help;
	   	   exit;            
         }
    }
}

#sub CheckOptions
#{
# my($Option)=@_;
# if (($Option ne "-all") && ($Option ne "-al") && ($Option ne "-l") && ($Option ne "-k")  &&
# ($Option ne "-F") && ($Option ne "-X") && ($Option ne "-a") && ($Option ne "-S") && ($Option ne "-R"))
# {
#  &Help;
#  exit;
# }
#

sub Help
{
    print("\nUSAGE: ls [-all] [-k] [-R] [-F] [-X] [--help] \n\n");
}

sub ExtendedHelp
{
    @try=`man ls`;
    print(@try);
}




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

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

sub PrintPermissions { 
 my($list) = @_;
 @list1=split(//,$list);
 print(@list1[0]);
 &Letter2Number($list1[1],$list1[2],$list1[3]);
 &Letter2Number($list1[4],$list1[5],$list1[6]);
 &Letter2Number($list1[7],$list1[8],$list1[9]);
}


sub Letter2Number {
 my($letter1,$letter2,$letter3)=@_;
 if ($letter1 eq "r"){
  $count = $count + 4;}
 if ($letter2 eq "w") {
  $count = $count + 2;}
   if ($letter3 eq "x"){
  $count = $count + 1;}
 print($count);
 $count=0;
}

sub PrintDate {
 my($month,$date)=@_;
 printf("%-3s%2s",$month,$date);
}

