1 #!/usr/bin/perl
2 # Silly script to manage files on DAP
3 # Right now just removes .zips
4 use strict;
5 use warnings;
6 use Getopt::Long;
7
8 ##
9 # Note for M11:
10 # jmtpfs /mnt/android/ ## Mount
11 # fusermount -u /mnt/android ## Umount
12 ##
13
14 my %args;
15 GetOptions(
16 \%args,
17 'source-path=s',
18 'clean-zips',
19 'dest-path=s',
20 'exclude-zips',
21 'sync'
22 );
23
24 sub yn_prompt {
25
26 print "Proceed? [y/n]: ";
27 my $input = <STDIN>;
28 chomp $input;
29 if ( $input !~ m/^y|^Y/g ) {
30 print "Aborting\n";
31 exit 0;
32 }
33
34 }
35
36 sub print_help {
37
38 my $help = <<EOF
39 dac-tool
40 Usage:
41 --source-path [Required]
42 Path to search for music directories
43
44 --dest-path
45 Path to copy music directories to
46
47 --clean-zips
48 Search source-path for .zip files and rm the files
49
50 --sync
51 Copy all files from source-path to dest-path
52
53 --exclude-zips
54 Exclude copying .zip files when used with --sync
55 EOF
56 ;
57 print "$help\n";
58
59 }
60
61 sub check_args {
62
63 if ( ! defined $args{'source-path'} || $args{'source-path'} eq "" ) {
64 print_help();
65 exit 1;
66 }
67
68 if ( defined $args{'sync'} ) {
69 if ( ! defined $args{'dest-path'} ) {
70 print "--sync requires --source-path and --dest-path\n";
71 exit 1;
72 }
73 }
74
75 }
76
77 sub get_source_used($) {
78
79 my $path = shift;
80 my $used = `df -h $path | tail -n1 | awk '{print \$3}'`;
81 chomp $used;
82 $used =~ s/G//g;
83 return $used;
84
85 }
86
87 sub clean_zips($) {
88
89 # Probably a better way to do this but
90 # `find` is still reasonably fast at ~75G of files
91 my $path = shift;
92 my $start_size = get_source_used($path);
93 my @zips = split("\n", `find $path | grep .zip`);
94 my $count = scalar @zips;
95 if ( $count == 0 ) {
96 print "Found no zips to clean in $path\n";
97 exit 0;
98 }
99 print "Found $count zip files in $path\n";
100 print "Delete $count zip files in $path?\n";
101 yn_prompt();
102
103 system("find $path -name '*.zip' -exec rm {} \\\;") == 0
104 or die print "Find cmd failed : $?\n";
105
106 my $end_size = get_source_used($path);
107 my $diff = $start_size - $end_size;
108 print "Cleaned up $diff (GB) of zip files\n";
109
110 }
111
112 sub sync($$) {
113
114 my $source_path = shift;
115 my $dest_path = shift;
116 my $opts = "";
117 if ( defined $args{'exclude-zips'} ) {
118 $opts = $opts . "--exclude \'*.zip\'";
119 }
120
121 #print "rsync -avP $opts $source_path/* $dest_path\n"
122 system("rsync -avP $opts $source_path/* $dest_path") == 0
123 or die print "Rsync failed : $?\n";
124
125 }
126
127 # Main
128 check_args();
129 if ( defined $args{'clean-zips'} ) {
130 clean_zips($args{'source-path'});
131 } elsif ( defined $args{'sync'} ) {
132 sync($args{'source-path'}, $args{'dest-path'});
133 }