#!/usr/bin/perl

use strict;
use warnings;

my $choice;
my ($after_pct, $after_amount);

# CONFIGURATION
my $large_archive_size = '200M';


while (1) {
    do_options();

    if ($choice eq '0') {
        exit 0;
    }

    elsif ($choice eq '1') {
        print "\n\n-----\n";
        print "\nFinding (not deleting) large archives that are >$large_archive_size in size...\n";
        do_archives();
    }

    elsif ($choice eq '2') {
        print "\n\n-----\n";
        print "Finding (not deleting) all backup archives from /home ...\n";
        do_backups();
    }

    do_summary();
}


sub do_options {
    print "\n";
    print "============================================================\n";
    print " /home CLEANUP SCRIPT \n";
    print "============================================================\n\n";
    print "-> What do you want to do?

    [ 1 ] FIND large archives (>$large_archive_size)
    [ 2 ] FIND all backup archives from /home
    [ 0 ] Exit...

    Your choice (0 to 2): ";

    chomp($choice = <STDIN>);

    while ($choice !~ /^[0-2]$/) {
        print "\nInvalid choice. Please enter a number from 0 to 2: ";
        chomp($choice = <STDIN>);
    }
}


sub do_summary {
    $after_pct = `df -h /home | awk '{print \$5}' | sed '1d'`;
    $after_amount = `df -h /home | awk '{print \$4}' | sed '1d'`;

    chomp($after_pct, $after_amount);

    print "\n\n============================================================\n";
    print "\t /home is $after_pct used ($after_amount available)\n";
    print "============================================================\n\n";
}


sub do_archives {
    system('find /home -type f -regextype posix-extended -iregex ".*\.(rar|zip|gz|tar|tgz|bz2|tbz2|xz|txz|7z)$" -not -path "/home/virtfs/*" -size +' . $large_archive_size . ' -exec ls -shqQ {} \\;');
    print "\nDone.\n";
    print "\n-----\n\n";
}


sub do_backups {
    system('find /home -maxdepth 2 -type f -name "backup-*.tar.gz" -not -path "/home/virtfs/*" -ls');
    print "\nDone.\n";
    print "\n-----\n\n";
}


#EOF
