dap-tool 2.4 KB

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