package org.gnu.grub.fonttool;

import java.io.File;
import java.io.IOException;
import javax.swing.*;

/**
 * Program to view fonts.
 * <p/>
 * Supports PFF2 and BDF format font files.
 */
public class Viewer {
    public static void main(String[] args) {
        if (args.length < 1) {
            printUsageAndExit();
        }

        boolean verbose = false;
        String format = null;
        String filename = null;

        try {
            for (String arg : args) {
                if (arg.startsWith("--")) {
                    String option;
                    String value;
                    int equalsPos = arg.indexOf('=');
                    if (equalsPos < 0) {
                        option = arg.substring(2);
                        value = null;
                    } else {
                        option = arg.substring(2, equalsPos);
                        value = arg.substring(equalsPos + 1);
                    }

                    if ("format".equals(option)) {
                        if (value == null)
                            throw new CommandLineException(option + " option requires a value.");
                        format = value;
                    } else if ("verbose".equals(option)) {
                        verbose = true;
                    }
                } else {
                    if (filename == null) {
                        filename = arg;
                    } else {
                        throw new CommandLineException(
                                "Extra argument after filename: `" + arg + "'.");
                    }
                }
            }
            if (filename == null) {
                throw new CommandLineException("A filename must be specified.");
            }
            if (!(format == null || "bdf".equals(format) || "pff2".equals(format))) {
                throw new CommandLineException("Invalid format: must be `bdf' or `pff2'.");
            }
        } catch (CommandLineException e) {
            System.err.println("Error: " + e.getMessage());
            System.exit(1);
        }

        if (!new File(filename).exists()) {
            System.err.println("Error: File `" + filename + "' not found.");
            System.exit(1);
        }

        if (format == null) {
            // Autodetect the format.
            if (PFF2Loader.isPFF2File(filename))
                format = "pff2";
            else if (BDFLoader.isBDFFile(filename))
                format = "bdf";
            else {
                System.err.println("Error: Unable to detect font file format " +
                                   "for `" + filename + "'.");
                System.exit(1);
            }
            if (verbose)
                System.out.println("Detected file format `" + format + "'.");
        }

        Font font = null;
        try {
            if ("bdf".equals(format)) {
                if (verbose)
                    System.out.println("Loading BDF font.");
                font = BDFLoader.loadFontFile(filename);
            } else if ("pff2".equals(format)) {
                if (verbose)
                    System.out.println("Loading PFF2 font.");
                font = PFF2Loader.loadFontFile(filename);
            } else {
                System.err.println("Error: Bad format `" + format + "'");
                System.exit(1);
            }
        } catch (IOException e) {
            System.err.println("I/O error loading font: " + e);
            e.printStackTrace();
            System.exit(1);
        }

        if (font == null) {
            System.err.println("Error: Font not loaded.");
            System.exit(1);
        }

        if (verbose) {
            System.out.println("Loaded font: '" + font.getStandardName() + "'");
        }

        ViewerFrame f = new ViewerFrame(font);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);   // Center the frame on screen.
        f.setVisible(true);
    }

    private static class CommandLineException extends Exception {
        public CommandLineException(String message) {
            super(message);
        }
    }

    private static void printUsageAndExit() {
        System.err.println("GNU GRUB Font Viewer");
        System.err.println();
        System.err.println("Usage: Viewer [--format=FORMAT] FILE");
        System.err.println("    --format=bdf   The font is in BDF format.");
        System.err.println("    --format=pff2  The font is in PFF2 format.");
        System.err.println("  If no --format option is given, the type will be");
        System.err.println("  automatically detected.");
        System.err.println();
        System.exit(1);
    }
}
