#!/usr/bin/perl
#
# Music Burner
#   usage: burnmusic <cdspeed> <device> <files>
# 
# Burns the specified ogg, mp3, and wav files to a cd
# The track order is as passed on the commandline
#
# Requires oggdec, mpg321, cdrecord
#
# ChangeLog:
#  0.2:   Aligned version with ds-audiotools
#  0.1.2: Added version variable
#  0.1.1: Added command-line wrapper, cleaned code
#  0.1:   Initial code

$VERSION = "0.2.3";
$HELP_MSG = "# Music Burner v$VERSION\n\tusage: burnmusic <cdspeed> <device> <files>\n";
$temp_dir = '/tmp/burnmusic-' . `whoami`;

die $HELP_MSG if ($#ARGV < 2);
foreach (@ARGV)
{
    die $HELP_MSG if (/^(--help)|(-h)$/);
}

system("rm -rf $temp_dir 2>/dev/null");
system("mkdir $temp_dir");

# Parse required arguments
$cdspeed = shift(@ARGV);
die "Fatal: Invalid CD Speed.\n" if ($cdspeed =~ /\D/);
$device = shift(@ARGV);

#Parse and handle files
$track = '01';
foreach (@ARGV)
{
    if (/\.ogg$/)    { system('oggdec', "-o $temp_dir/$track.wav", $_); }
    elsif (/\.mp3$/) { system('mpg321', "-w $temp_dir/$track.wav", $_); }
    elsif (/\.wav$/) { system('cp', $_, "$temp_dir/$track.wav"); }
    else { print STDERR 'Invalid argument (non-fatal): ' . $_ . '\n'; }
    $track++;
}

# Burn and clean
system('cdrecord', '-v -pad -audio', "speed=$cdspeed", "dev=$device", "$temp_dir/*.wav");
system("rm -rf $temp_dir");


# Copyright (c) 2005 John Wiggins
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA