https://git.spwbk.site/swatson/dap-tool/raw/master/dap-tool
___________________________________
#!/usr/bin/perl
# Silly script to manage files on DAP
# Right now just removes .zips
use strict;
use warnings;
use Getopt::Long;

##
# Note for M11:
# jmtpfs /mnt/android/ ## Mount
# fusermount -u /mnt/android ## Umount
##

my %args;
GetOptions(
       \%args,
       'source-path=s',
       'clean-zips',
       'dest-path=s',
       'exclude-zips',
       'sync'
);

sub yn_prompt {

       print "Proceed? [y/n]: ";
       my $input = <STDIN>;
       chomp $input;
       if ( $input !~ m/^y|^Y/g ) {
               print "Aborting\n";
               exit 0;
       }

}

sub print_help {

       my $help = <<EOF
dac-tool
Usage:
       --source-path [Required]
       Path to search for music directories

       --dest-path
       Path to copy music directories to

       --clean-zips
       Search source-path for .zip files and rm the files

       --sync
       Copy all files from source-path to dest-path

       --exclude-zips
       Exclude copying .zip files when used with --sync
EOF
;
       print "$help\n";

}

sub check_args {

       if ( ! defined $args{'source-path'} || $args{'source-path'} eq "" ) {
               print_help();
               exit 1;
       }

       if ( defined $args{'sync'} ) {
               if ( ! defined $args{'dest-path'} ) {
                       print "--sync requires --source-path and --dest-path\n";
                       exit 1;
               }
       }

}

sub get_source_used($) {

       my $path = shift;
       my $used = `df -h $path | tail -n1 | awk '{print \$3}'`;
       chomp $used;
       $used =~ s/G//g;
       return $used;

}

sub clean_zips($) {

       # Probably a better way to do this but
       # `find` is still reasonably fast at ~75G of files
       my $path = shift;
       my $start_size = get_source_used($path);
       my @zips = split("\n", `find $path | grep .zip`);
       my $count = scalar @zips;
       if ( $count == 0 ) {
               print "Found no zips to clean in $path\n";
               exit 0;
       }
       print "Found $count zip files in $path\n";
       print "Delete $count zip files in $path?\n";
       yn_prompt();

       system("find $path -name '*.zip' -exec rm {} \\\;") == 0
               or die print "Find cmd failed : $?\n";

       my $end_size = get_source_used($path);
       my $diff = $start_size - $end_size;
       print "Cleaned up $diff (GB) of zip files\n";

}

sub sync($$) {

       my $source_path = shift;
       my $dest_path = shift;
       my $opts = "";
       if ( defined $args{'exclude-zips'} ) {
               $opts = $opts . "--exclude \'*.zip\'";
       }

       #print "rsync -avP $opts $source_path/* $dest_path\n"
       system("rsync -avP $opts $source_path/* $dest_path") == 0
               or die print "Rsync failed : $?\n";

}

# Main
check_args();
if ( defined $args{'clean-zips'} ) {
       clean_zips($args{'source-path'});
} elsif ( defined $args{'sync'} ) {
       sync($args{'source-path'}, $args{'dest-path'});
}