#!/usr/bin/perl
# $Header: /home/dodge/cvs/www.dmumford.com/htdocs/tech/stockrio.pl,v 1.1 2002/05/19 02:51:17 dodge Exp $
# $Name:  $
# Stock the diamond rio with random tunes
# requires the "rio" package

###############################################################################
# CONFIGURABLES
###############################################################################
# The paths to check for mp3's. Will do so recursively (without following
# symlinks)
@mp3_paths = ( "/mp3" );
###############################################################################
# A file containing an anti-playlist. Ie, songs you never want to be chosen.
# Will compare using regexes. 
@mp3_antiplay = $ENV{"HOME"} . "/.antiplay";
###############################################################################
# Set to a truth value for vebose output
$verbose = 1;
###############################################################################

###############################################################################
# End of configurables
###############################################################################

# Go through the args & interpret
$delete = $initialize = 0;
foreach $a ( @ARGV ) {
	if ( $a eq "-h" ){ 
		print("Usage: stockrio.pl [-h] ([-d]|[-i])\n");
		print("	-h: help\n");
		print("	-d: delete all files\n");
		print("	-i: initialize memory\n");
		print("	-d and -i are mutually exclusive\n");
		exit(0);
	} elsif ( $a eq "-d" ) {
		if ( $initialize ) {
			print("Initializing, which will delete everything anyway\n");
		} else {
			$delete = 1;
		}
	} elsif ( $a eq "-i" ) {
		$initialize = 1;
		if ( $delete ) {
			print("Initializing, which will delete everything anyway\n");
			$delete = 0;
		}
	}
}

foreach $d ( @mp3_paths ) {
	$verbose && print("Getting filenames from $d\n");
	@files = ( @files, `find $d -type f -name "*.mp3"` );
}

$numfiles = 0;
$smallest = 0;
$biggest = 0;
foreach $f ( @files ) {
	chop $f;
	($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime,
		$ctime, $blksize, $blocks) = stat($f);
	$b = $blocks / ($blksize/1024);
	$b = int($size / 1024);
	$filenames[$numfiles] = $f;
	$blocksizes[$numfiles] = $b;
	if ( $smallest == 0 || $b < $smallest ) {
		$smallest = $b;
	}
	if ( $b > $biggest ) {
		$biggest = $b;
	}
	$numfiles++;
}
if ( $numfiles == 0 ) {
	print("No files to add to rio. Quitting\n");
	exit(0);
}

@myfiles = @filenames;
@myblocks = @blocksizes;
for ( $i = 0; $i < $numfiles; $i++ ) {
	@myorig[$i] = $i;
}
$mynum = $numfiles;
if ( $initialize == 1 ) {
	$verbose && print("Initializing rio main memory\n");
	`rio -iy`;
}
if ( $delete == 1 ) {
	$verbose && print("deleting all files in the rio main memory\n");
	`rio -za`;
}
$verbose && print("Adding new files (main memory)\n");
$CHECKRIO = 1;
while ( ($left = &spaceleft) >= $smallest ) {
	$i = int(rand() * $mynum);
	if ( $myblocks[$i] <= $left ) {
		$CHECKRIO = 1;
		printf("Uploading %d - %s\n", @myblocks[$i], @myfiles[$i]);
		open(FD, "rio -u \"@myfiles[$i]\" |");
		while ( <FD> ) {
			$verbose && print("$_");
		}
	} else {
		$verbose && printf("Rejecting %s -- not enough space\n", 
			@myfiles[$i]);
		$CHECKRIO = 0;
	}
	for ( $j = $i; $j <= $mynum; $j++ ) {
		@myfiles[$j] = @myfiles[$j+1];
		@myblocks[$j] = @myblocks[$j+1];
		@myorig[$j] = @myorig[$j+1];
	}
	$mynum--;
}
End;

@myfiles = @filenames;
@myblocks = @blocksizes;
$mynum = $numfiles;
if ( $initialize == 1 ) {
	$verbose && print("Initializing rio external memory\n");
	`rio -x -iy`;
}
if ( $delete == 1 ) {
	$verbose && print("deleting all files in the rio external memory\n");
	`rio -x -za`;
}
$verbose && print("Adding new files (external memory)\n");
$CHECKRIO = 1;
while ( ($left = &spaceleftx) >= $smallest ) {
	$i = int(rand() * $mynum);
	if ( $myblocks[$i] <= $left ) {
		printf("Uploading %d - %s\n", @myblocks[$i], @myfiles[$i]);
		open(FD, "rio -x -u \"@myfiles[$i]\" |");
		while ( <FD> ) {
			$verbose && print("$_");
		}
		$CHECKRIO = 1;
	} else {
		$verbose && printf("Rejecting %s -- not enough space\n", 
			@myfiles[$i]);
		$CHECKRIO = 0;
	}
	for ( $j = $i; $j <= $mynum; $j++ ) {
		@myfiles[$j] = @myfiles[$j+1];
		@myblocks[$j] = @myblocks[$j+1];
		@myorig[$j] = @myorig[$j+1];
	}
	$mynum--;
}
End;

sub spaceleft { 
	if ( $CHECKRIO == 0 ) {
		return($left);
	}
	$left = -1;
	open(FD, "rio -d |");
	while ( <FD> ) {
		chop;
		if ( $left == -1 && index($_, "unused memory:") > -1 ) {
			($null, $null, $null, $left, @null) = split(/ /, $_);
		}
	}
	close(FD);
	return($left);
}

sub spaceleftx { 
	my $left = -1;
	open(FD, "rio -x -d |");
	while ( <FD> ) {
		chop;
		if ( $left == -1 && index($_, "unused memory:") > -1 ) {
			($null, $null, $null, $left, @null) = split(/ /, $_);
		}
	}
	close(FD);
	return($left);
}
