#!/usr/bin/perl # This script copies a random selection of mp3s to your memorystick for playback on a Clie. You'll need to run it as root # if you want it to mount and unmount the stick for you automatically. # You can define the number of files to copy on the command line (e.g. "cpmp3 20"), or define a default below. # This script assumes that you can already mount your memorystick somewhere - so you'll need to know the # device name and have already created a mountpoint for it. It further assumes that you're not going to # try and copy more mp3s than your stick can hold! # This script is provided "as is" etc. You use it at your own risk and I accept no responsibility for damage caused etc. blah # In particular, don't remove the stick before you see the "Transfer Complete" message! # sjb, ottaky@ottaky.com #### config #### my $count = $ARGV[0] || 10; # if you don't supply a number on the command line, this is the default number of files to copy (10) my $dev = '/dev/sda1'; # the device name that corresponds to your stick reader my $stick = "/media/memstick"; # where your memory stick gets mounted my $msaudio = "/Palm/Programs/msaudio"; # where you want the mp3s to end up (this is the default directory for the Clie) my $mp3dir = "/home/ottaky/music/mp3"; # the top level directory of where your mp3s live (sorry, the script only handles one) ################ $stick =~ s|/$||g; $msaudio =~ s|/$||g; my (@mp3s,$stdout); foreach (`find $mp3dir -name *.mp3`) { chomp; s| |\\ |g; s|\(|\\(|g; s|\)|\\)|g; # you may need to add your own regexs here if your mp3 filenames have *ix shell metcharacters in them push (@mp3s, $_); } if ($#mp3s >= $count) { unless (-d "$stick$msaudio") { print "Mounting stick .. "; $stdout = `mount -t auto $dev $stick`; die "$?\n" if ($?); print "done.\n"; sleep 2; } print "Deleting existing files .. "; $stdout = `rm $stick$msaudio/*`; die "$?\n" if ($?); print "done.\nCopying $count files ..\n"; while ($count > 0) { my $pick = int(rand(@mp3s)); if ($mp3s[$pick]) { print "Copying $mp3s[$pick] .."; $stdout = `cp $mp3s[$pick] $stick$msaudio/`; die "$?\n" if ($?); print " done.\n"; $mp3s[$pick] = 0; $count--; } } print "Unmounting stick .. "; $stdout = `umount /dev/sda1`; die "$?\n" if ($?); print "done.\nTransfer complete.\n"; } else { die "Can't transfer $count files because only $#mp3s were found. Try 'cpmp3 $#mp3s'.\n"; }