//
// $Id: ID3Tagger.java,v 1.1 2002/03/03 05:25:09 mdb Exp $

package robodj.convert;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.util.StreamUtils;

/**
 * A tagger implementation that uses 'id3' to do it's job.
 */
public class ID3Tagger
{
    // documentation inherited
    public void idTrack (String target, String artist, String album,
                         String title, int trackNo)
	throws ConvertException
    {
        String[] cmdarray = new String[] {
            "id3", "-t", title, "-a", artist, "-A", album,
            "-T", Integer.toString(trackNo), target };

        try {
            Runtime rt = Runtime.getRuntime();
            Process ripproc = rt.exec(cmdarray);

	    InputStream in = ripproc.getErrorStream();
            String output = StreamUtils.streamAsString(in);

            // check the return value of the process
            try {
                int retval = ripproc.waitFor();
                if (retval != 0) {
                    // ship off the error output from id3
                    throw new ConvertException(
                        "id3 failed: " + output);
                }

            } catch (InterruptedException ie) {
                // why we were interrupted I can only speculate, but we'll
                // go ahead and freak out anyway
                throw new ConvertException("Interrupted while waiting for " +
                                           "encoder process to exit.");
            }

	} catch (IOException ioe) {
	    throw new ConvertException(
		"Error communicating with encoder:\n" + ioe);
	}
    }

    public static void main (String[] args)
    {
        ID3Tagger tagger = new ID3Tagger();
        if (args.length < 5) {
            System.err.println("Usage: ID3Tagger target artist album " +
                               "title trackNo");
            System.exit(-1);
        }

        int trackNo = 0;
        try {
            trackNo = Integer.parseInt(args[4]);
        } catch (NumberFormatException nfe) {
            System.err.println("Invalid track number: " + args[4]);
            System.exit(-1);
        }

        try {
            tagger.idTrack(args[0], args[1], args[2], args[3], trackNo);
        } catch (ConvertException ce) {
            ce.printStackTrace(System.err);
        }
    }
}

@@ -23,17 +23,17 @@
 ava,v 1.
-1
+2
  2002/03
@@ -41,15 +41,15 @@
 03 0
-5:25:09
+6:04:23
  mdb
@@ -228,16 +228,18 @@
 ses 'id3
+v2
 ' to do 
@@ -274,16 +274,34 @@
 D3Tagger
+ implements Tagger
 %0A%7B%0A    /
@@ -541,14 +541,20 @@
 %22id3
+v2
 %22, %22-
-t
+-song
 %22, t
@@ -561,17 +561,23 @@
 itle, %22-
-a
+-artist
 %22, artis
@@ -581,17 +581,22 @@
 tist, %22-
-A
+-album
 %22, album
@@ -611,17 +611,22 @@
       %22-
-T
+-track
 %22, Integ
@@ -1166,16 +1166,18 @@
     %22id3
+v2
  failed:

Use id3v2 instead of v1 as v2 is way better.