/*
 *  Copyright (C) 2002, 2003 Mikhail A. Kryshen
 *
 *  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
 */
 
import java.io.*;
import java.util.*;
import java.awt.Color;
import java.awt.Font;

public class ResourceBuilder {
    static final byte[] MAGIC = "(BM Resources)".getBytes();
    static final int VERSION = 3;
    
    static final int DATA_INT = 4;
    static final int DATA_IMAGE = 8;
    static final int DATA_COLOR = 9;
    static final int DATA_FONT = 10;

    private static int sumLength = 0;
    private static int length = 0;

    public static void main(String args[]) throws Exception {
	System.err.println("Building " + args[1]); 
    
	File base = new File(args[2]);
	String[] sfiles = base.list();
	File[] files = new File[sfiles.length];
	
	Arrays.sort(sfiles);
		
	DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(args[1])));
	
	out.write(MAGIC);
	out.writeInt(VERSION);
	out.writeUTF(args[0]);
	
	for (int i = 0; i <= files.length - 1; i++) {
	    files[i] = new File(base, sfiles[i]);
	    sumLength += files[i].length();
	}

	for (int i = 0; i <= files.length - 1; i++) {	    
	    packFile(files[i], out);
	}
    
	out.writeByte(0xFF);
	out.close();	
    }
    
    private static void packFile(File file, DataOutputStream out) throws Exception {
	if (file.isDirectory()) return;

	String ext;
	String name = file.getName().toLowerCase();
	if (name.length() < 8) return;	
	ext = name.substring(name.length() - 4);
	name = name.substring(4, name.length() - 4);
        //System.err.println(file + "; " + length);

	int progress = (length * 0xFE / sumLength) & 0xFE;
	length += file.length();

	if (ext.equalsIgnoreCase(".gif") || 
	    ext.equalsIgnoreCase(".jpg") ||
	    ext.equalsIgnoreCase(".png")) {
	    
	    out.writeByte(progress);
	    packImage(file, name, out);
	} else if (ext.equalsIgnoreCase(".clr")) {
	    out.writeByte(progress);
	    packColor(file, name, out);
	} else if (ext.equalsIgnoreCase(".fnt")) {
	    out.writeByte(progress);
	    packFont(file, name, out);
	} else if (ext.equalsIgnoreCase(".int")) {
	    out.writeByte(progress);
	    packInt(file, name, out);
	}
    }

    private static void packFont(File file, String name, DataOutputStream out) throws Exception {
	BufferedReader in = new BufferedReader(new FileReader(file));
	Font font = Font.decode(in.readLine());
	in.close();

	out.writeInt(DATA_FONT);
	out.writeUTF(name);
	out.writeInt(-1);

	out.writeUTF(font.getName());
	out.writeInt(font.getStyle());
	out.writeInt(font.getSize());
    }

    private static void packColor(File file, String name, DataOutputStream out) throws Exception {
	BufferedReader in = new BufferedReader(new FileReader(file));
	int rgb = Color.decode(in.readLine()).getRGB();
	in.close();

	out.writeInt(DATA_COLOR);
	out.writeUTF(name);
	out.writeInt(4);
	out.writeInt(rgb);
    }
    
    private static void packInt(File file, String name, DataOutputStream out) throws Exception {
	BufferedReader in = new BufferedReader(new FileReader(file));
	int value = Integer.parseInt(in.readLine());
	in.close();

	out.writeInt(DATA_INT);
	out.writeUTF(name);
	out.writeInt(4);
	out.writeInt(value);
    }

    private static void packImage(File file, String name, DataOutputStream out) throws Exception {
	int length = (int)file.length();

	out.writeInt(DATA_IMAGE);	
	out.writeUTF(name);
	out.writeInt(length);

    	InputStream in = new BufferedInputStream(new FileInputStream(file));
	for (int i = 0; i <= length - 1; i++) {
	    out.write(in.read());
	}
	in.close();
    } 
}
