#!/usr/bin/perl
#
# Ogg converter
#   usage: conv2ogg <files>
#
# Creates ogg files from the selected audio files
#
# Requires one of mpg321, mpg123 or mplayer, and oggenc
# Requires mplayer for support for non-mp3 filetypes
#
# Changelog:
#   0.2.4: Allow mplayer to handle any non-ogg file
#          Make invocations of files in other directories work
#          Use File::Path where appropriate
#   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.4";
$HELP_MSG = "# Audio file - Ogg Converter v$VERSION\n\tusage: conv2ogg <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')";

use File::Path qw(make_path remove_tree);

chomp($tempdir = '/tmp/conv2ogg-' . `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
remove_tree $tempdir;
make_path $tempdir;

foreach(@ARGV)
{
    my $directory;
    if (/\//)
    {
	$directory = $_;
	$directory =~ s|^(.*)/.*?$|\1|;
    }
    else { $directory = '.'; }

    $filename = $_;
    $filename =~ s|^.*/(.*)?$|\1|;

    $short_name = $filename;

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

    # Remove old file
    system('rm', "$directory/$filename");
}

# Remove last of the cruft
remove_tree $tempdir;




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