#!/usr/bin/perl
#
# MP3 - Ogg converter
#   usage: mp32ogg <files>
#
# Creates ogg files from the selected mp3 files
#
# Requires one of mpg321, mpg123 or mplayer, and oggenc
#
# Changelog:
#   0.2.1: Added new options to mplayer for mplayer's new features
#          Added support for m4a if using mplayer
#   0.2:   Aligned version with ds-audiotools
#   0.1.3: Added mplayer support, automatically select decoder
#          Fixed option passing to system() calls to be as secure as possible
#   0.1.2: added support for passing flags
#          Use a temp dir for storage of intermediate wav file
#          Cleaned up some syntax
#          Added version variable
#   0.1:   converted to perl
#          changed to use command-line args
#   0.0.1: Initial bash script

$VERSION = "0.2";
$HELP_MSG = "# MP3 - Ogg Converter v$VERSION\n\tusage: mp32ogg <files>";
$DEC_ERROR = "Couldn't find a decoder to use.  Please install one of mplayer, mpg321, or mpg123";
$ENC_ERROR = "Couldn't find oggenc, which is necessary for encoding.  Please install the package that provides oggenc (probably named 'oggenc' or 'vorbistools')";

chomp($tempdir = '/tmp/mp32ogg-' . `whoami`);


# Do we need help?
foreach (@ARGV)
{
    if (/^(--help)|(-h)$/) { die $HELP_MSG; }
}

# Select the encoder and decoder
chomp($decoder = `which mplayer`);
$decoder_options = '-vc null -vo null -ao pcm:fast:file=';

if (! -x $decoder)
{
    $decoder_options = '-w '; # The others both use this format
    chomp($decoder = `which mpg321`);
}
chomp($decoder = `which mpg123`) if (! -x $decoder);

chomp($encoder = `which oggenc`);

# Dependency checks
die $DEC_ERROR if (! -x $decoder);
die $ENC_ERROR if (! -x $encoder);

# Set up the temp directory
system('rm', '-rf', $tempdir) if (-d $tempdir);
mkdir($tempdir);

foreach(@ARGV)
{
    $filename = $_;
    $short_name = $_;
    if ($decoder =~ /mplayer/)
    {
	next unless (/\.(m4a|mp3)/);
	$short_name =~ s/\.(mp3|m4a)$//;
    }
    else
    {
	next unless (/\.mp3$/);
	$short_name =~ s/\.mp3$//;
    }
    
    # Transcode the file
    system("$decoder $filename $decoder_options$tempdir/$short_name.wav");
    system($encoder, "$tempdir/$short_name.wav", '-o', "$short_name.ogg");

    # Remove cruft
    system('rm', "$tempdir/$short_name.wav");
    system('rm', "$filename");
}

# Remove last of the cruft
system('rm', '-rf', $tempdir);

# Copyright (c) 2005 - 2008 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