/*
 *   Copyright (c) 2003, Richard Koolish 
 *   All Rights Reserved.
 *
 */


/*********************************************************************
*                                                                    * 
*  Plotter - Create a JPG image of a plot                            * 
*                                                                    * 
*********************************************************************/  

import java.util.*;
import java.io.*;
import java.awt.*;


public class Plotter  {

    private static boolean verbose = false;
    private static String source = null;
    private static String dest = null;
    private static String indir = null;
    private static String outdir = null;
    private static String usage = "usage: java Plotter -s <source> -d <dest> -indir <dir> -outdir <dir>";
    private static int canvas_w = 500;
    private static int canvas_h = 500;
    private static Hashtable colors;


    private static void parseArgs(String[] args) {

        for (int i = 0; i < args.length; i++) {
	      String arg = args[i];

	      if (arg.equals("-v")) {
		    verbose = true;
            } else if (arg.equals ("-s")) {
                i++;
                source = args[i];
            } else if (arg.equals ("-d")) {
                i++;
                dest = args[i];
            } else if (arg.equals ("-indir")) {
                i++;
                indir = args[i];
            } else if (arg.equals ("-outdir")) {
                i++;
                outdir = args[i];
            } else if (arg.equals ("-w")) {
                i++;
                canvas_w = Integer.parseInt(args[i]);
            } else if (arg.equals ("-h")) {
                i++;
                canvas_h = Integer.parseInt(args[i]);
            } else {
                System.out.println ("Unknown arg: " + arg);
            }
	  } 

        if (source == null) {
            System.err.println ("No source file specified");
            System.err.println (usage);
            System.exit (-1);
        }

        if (dest == null) {
            System.err.println ("No destination file specified");
            System.err.println (usage);
            System.exit (-1);
        }

        if (indir == null) {
            System.err.println ("No input directory specified");
            System.err.println (usage);
            System.exit (-1);
        }

        if (outdir == null) {
            System.err.println ("No output directory specified");
            System.err.println (usage);
            System.exit (-1);
        }
    }



    public static void main (String[] args) { 

        colors = new Hashtable ();
        colors.put ("black", Color.black);
        colors.put ("red", Color.red);
        colors.put ("blue", Color.blue);
        colors.put ("green", Color.green);

        parseArgs (args);

        process_file (source, dest);

        System.exit (0);
    }

    
    private static void process_file (String in_filename, String out_filename) {

        BufferedReader in;
        String line;
        String new_line;

        System.err.println ("processing '" + in_filename + "'");

        try {
            in = new BufferedReader (new FileReader (indir + "/" + in_filename));
        } catch (Exception e) {
            System.err.println ("Can't open input file '" + indir + "/" + in_filename + "'");
            e.toString ();
            return;
        }

        BufferedImageCanvas canvas = new BufferedImageCanvas (canvas_w, canvas_h);

        while (true) {
            try {
                line = in.readLine ();
                if (line == null)
                    break;
                if (line.trim().length() == 0)
                    continue;
                if (line.startsWith ("#"))
                    continue;
                Vector v = tokenize (line, null);
                if (v.size() < 6)
                    continue;
                int x1 = Integer.parseInt ((String)v.elementAt (0));
                int y1 = Integer.parseInt ((String)v.elementAt (1));
                int x2 = Integer.parseInt ((String)v.elementAt (2));
                int y2 = Integer.parseInt ((String)v.elementAt (3));
                int lw = Integer.parseInt ((String)v.elementAt (4));
                String color = (String)v.elementAt (5);
                Color c = (Color)colors.get (color);
                if (c == null)
                    c = Color.black;
                canvas.drawLine (x1, y1, x2, y2, lw, c);
            } catch (Exception e) {
                e.printStackTrace ();
                break;
            }
        }

        canvas.write (outdir + "/" + out_filename);
    }


    // tokenize

    private static Vector tokenize (String s, String delim) {

        StringTokenizer st;
        if (delim == null)
            st = new StringTokenizer (s);
        else
            st = new StringTokenizer (s, delim);

        Vector tokens = new Vector ();
        while (st.hasMoreTokens ())        
            tokens.add (st.nextToken ());

        return tokens;
    }


}
   