    #!/usr/bin/env perl
    # written by MelmothX, No copyright
    
    use strict;
    use warnings;
    use utf8;
    use File::Spec::Functions;
    
    my $target = $ARGV[0];
    my $musicdir = 'MUSIC';
    
    die "Missing argument\n" unless $target;
    die "Target not a dir\n" unless (-d $target);
    die "MUSIC not found" unless (-d $musicdir);
    
    # strip the slash, just to be sure
    $target =~ s!/*$!!;
    my $origin = ".";
    
    my ($avail, $ismounted) = check_fs($target);
    die "Something is not OK: $target doesn't look like a mountpoint\n" unless ($avail and $ismounted);
    unless (we_have_free_space($avail)) {
      print "Current dir cannot is larger than target, exiting\n";
      exit 2;
    }
    
    my @rsync = ('rsync',
                 '-rtLvh', # please note the L and the t
                 '--no-p', '--no-o', '--no-g', # no permission
                 '--modify-window', '1',
                 '--safe-links',
                 '--delete',
                 '--dry-run',
                 '--exclude="*.bmark"', '--exclude="*.m3u8"',
                 $musicdir, $target);
    
    print join(' ', @rsync), "\n";
    
    sub we_have_free_space {
      my $avail = shift;
      my $total;
      die "Can't fork: $!" unless defined(my $pid = open(KID, "-|"));
      if ($pid) {                   # parent
        while (<KID>) {
          if (m!^(\d+)\s+!) {
            $total = $1;
          }
        }
        close KID;  
      }
      else {
        my @command = ('du', '-L', '.'); # use -L to derefernce the symlinks
        exec @command
          or die "can't exec du: $!";
      }
      print "$avail > $total? ";
      if ($avail > $total) {
        print "yes!\n";
        return 1
      } else {
        return 0
      }
    }
    
    
    sub check_fs {
      my $target = shift;
      my ($free, $ismounted);
      die "Can't fork: $!" unless defined(my $pid = open(KID, "-|"));
      if ($pid) {                   # parent
        while (<KID>) {
          if (m![\w/]+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%\s+\Q$target\E/*!) {
            $free = $1;
            $ismounted = 1;
            last
          }
        }
        close KID;
      } else {
        my @command = ('df', '-P', $target);
        exec @command
          or die "can't exec df: $!";
      }
      return ($free, $ismounted);
    }