#!/usr/bin/perl

use strict;
use warnings;

use POSIX qw(floor);

print "\n===== Deletinator =====\n";
print "For deleting directories whose contents are too numerous for regular\n";
print "'rm' to delete without help and taking over the tri-state area. \n";
print "Used when you see:\n";
print "bash: /bin/rm: Argument list too long\n";
print "Run 'du' to generate a list of files/directories to nuke, then feed\n";
print "to thisinator.\n";
print "=======================\n\n";

if (!defined $ARGV[0] || !-f $ARGV[0])
{
  print "Usage: deletinator.pl <filelist>\n\n";
}
else
{
  my $filename = $ARGV[0];

  my $total_filesize = -s $filename;
  if (open(FIN, '<:utf8', $filename))
  {
    my $read_so_far = 0;
    my $line = '';
    while ($line = <FIN>)
    {
      if ($line =~ /([\d\.GMKB]+)\s+(.*)/)
      {
        my $path = './' . $2;
        $read_so_far += length($line);
        print 'Deleting "' . $path . '" ';
        `rmdir $path`;
        my $progress = 100 * ($read_so_far / $total_filesize);
        print 'Done [' . floor($progress) . "%]\n";
      }
    }
  }
  else
  {
    print "Error! Failed to open file for reading: " . $filename . "\n";
  }
}
print "Complete!\n\n";

exit;
1;
