commit ba89cb6f62c692dd1df1f544dfccd13cc6ed1938
Author: spesk1 <spesk@pm.me>
Date: Wed Apr 15 22:21:05 2020 -0400
Initial Commit
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..179e9f9
--- /dev/null
+++ b/README.md
@@ -0,0 +1,6 @@
+# dac-tool
+
+Little script used to wrap common CLI operations I use with my music players
+and associated SD cards.
+
+Mainly for syncing music between music drives and music player.
diff --git a/dap-tool b/dap-tool
new file mode 100755
index 0000000..900baec
--- /dev/null
+++ b/dap-tool
@@ -0,0 +1,132 @@
+#!/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
+ 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";
+
+}
+
+check_args();
+if ( defined $args{'clean-zips'} ) {
+ clean_zips($args{'source-path'});
+} elsif ( defined $args{'sync'} ) {
+ sync($args{'source-path'}, $args{'dest-path'});
+}