#!/usr/bin/perl

##################################################
#
# GiantDisc mp3 Jukebox
# 
# © 2000 - 2002, Rolf Brugger
#
##################################################

#
# Upload mp3 tracks of outbox to specified external mp3 player
#

use strict;
#use lib '/home/music/bin';
#use gdparams;
use Getopt::Long;


### main
my $outboxdir = "/home/music/outbox";
my ($help, $info, $add);
my ($riopmp300, $otherplayer);


GetOptions(
	"riopmp300" 	 => \$riopmp300,
	# add other drivers here

	"add" 		 => \$add,
	"info" 		 => \$info,
	"help" 		 => \$help 
	);



if($riopmp300) {
  if($info){
    riopmp300_info();
  }
  else{
    riopmp300_upload($add);
  }
}
elsif($otherplayer){
}
else{
  print <<EOF;

gdupload2player.pl

  Uploads the mp3 tracks in the directory ~music/outbox to the
  specified external mp3 player.

Options:
--------

player drivers (one must be selected)

--riopmp300      Uploads mp3 tracks to a Rio PMP300 player which is attached 
                 to the prallel port. (Requires the Rio utility of The Snowblind 
                 Alliance: http://www.world.co.uk/sba/ )
                 


The following options can be combined with one of the above player drivers

--info      Writes an info string which is directly displayed in the 
            Palm client.
            To be properly handled by the Palm client, this must be a single
            text line ended by a newline character.

--add       Adds tracks to mp3 player. if this option is not set, the
            player memory is emptied before tracks are uploaded.
                 
--help      This help text.
EOF
}
exit 0;






###############################################################################
### Rio PMP300
###############################################################################

sub riopmp300_info{
  my ($totalmem, $freemem);
  $totalmem = `rio -d | grep "total memory" | tr -d 'A-Za-z :'`;
  chop $totalmem;
  $freemem = `rio -d | grep "unused memory" | tr -d 'A-Za-z :'`;
  chop $freemem;

  print ("You have chosen to upload ".nb_files_in_outbox());
  print (" files (".kbytes_in_outbox()."kb) to a Rio PMP300. It has ");
  print ($totalmem."kb total memory and ".$freemem."kb free memory. ");
  print ("Make sure the Rio is switched on and attached to the parallel port.\n");
}

sub riopmp300_upload{
  my ($add) = @_;
  my $command;

  if ($add){
    $command = "rio -u $outboxdir/*.mp3 -d"  
  }
  else{
    $command = "rio -za -u $outboxdir/*.mp3 -d"  
  }
  print ("$command\n");
  system ("$command");
}





###############################################################################
# tool routines
sub nb_files_in_outbox{
  my $resstr;
  $resstr = `ls $outboxdir |wc -l|tr -d ' '`;
  chop $resstr;
  return $resstr;
}

sub kbytes_in_outbox{
  my $resstr;
  $resstr = `du -L -k $outboxdir| tr -d ' a-z/\t'`;
  chop $resstr;
  return $resstr;
}

###############################################################################




