#!/usr/bin/perl
##################################################
#                 FLACOGG v1.0.1                 ##
#       convert flac files to ogg vorbis         ##
# requires flac & oggenc                         ##
# (c) 2004 Trey Hunner - treyhunner@gmail.com    ##
# Based on flac2ogg for Windows by Jason Buberel ##
# (c) 2004 Jason L. Buberel - jason@buberel.org  ##
#                                                ##
# Permission is granted to freely distribute     ##
# as long as this copyright notice is attached.  ##
#                                                ##
# 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.     ##
#                                                ## 
# 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.                                       ##
###################################################
 ##################################################

# v1.0.1, 2004-12-16
#    - Deleted unnecessary directory copying section
#    - Merged the source and destination vars to one
#    - Directory variable is set to current directory

# v1.0.0, 2004-12-15
#    - Converted flac2ogg to Linux
#    - Destination directory and source directory are equal
#    - Reformatted indentation style from K&R to Allman

use Getopt::Long;
use File::stat;

$dir = $ENV{'PWD'};
$quality = 6;
GetOptions ( "source:s" => \$dir,
             "dest:s" => \$dir,
             "quality:i" => \$dir,
             "force" => \$dir );

# Commands
$oggCommand = "oggenc";


@dirs = `cd "$dir" && find . -type d -print`;
@files = `cd "$dir" && find . -type f -name "*.flac*" -print`;

# Check to see if our find command found anything to convert.
if ( scalar @files == 0 )
{
    die "Found no .flac files to convert!";
}

# now process the actual sound files.
foreach $file (@files)
{
    $file =~ s/\n$//;
    $file =~ s/^\.\///;
    
    # figure out what the destination file would be...
    $destinationFile = $file;
    $destinationFile =~ s/\.flac/\.ogg/;
    #print "D: $destinationFile\n";
    
    # now stat the destinationFile, and see if it's date is more recent
    # than that of the original file. If so, we re-encode.
    # we also re-encode if the user supplied --force
    $srcInfo = stat ("$dir/$file");
    $srcModTime = $srcInfo->mtime;
    $destInfo = stat ("$dir/$destinationFile");
    if ( $destInfo )
    {
        $destModTime = $destInfo->mtime;
        print "DEST_MOD: $destModTime :: SRC_MOD: $srcModTime :: FORCE: $force\n"; 
    }
    # if the destination file does not exist, or the user specified force,
    # or the srcfile is more recent then the dest file, we encode.
    if ( !$destInfo || $force || ( $srcModTime > $destModTime) )
    {
        $file =~ s/\`/\'/g;

        $inFile = "$dir/$file";
        $outFile = "$dir/$destinationFile";
        print "ENCODE: $inFile ==> $outFile\n"; 
        $inFile =~ s/\0//g; $outFile =~ s/\0//g;
        $result =  `$oggCommand -q $quality -o "$outFile" "$inFile"`;
    }
        
}
