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

public class CdeConfig extends Panel implements FrameBufferInfo {

	private static final String apply = new String(" Apply ");

	fbconf parent;
	Configuration config;
	Vector displays = null;
	Vector positions = null;
	Vector visuals = null;

	public CdeConfig (fbconf f, Configuration conf) {

		parent = f;
		config = conf;

		Choice display = null;
		Choice position = null;
		Choice visual = null;

		displays = new Vector();
		positions = new Vector();
		visuals = new Vector();

		int fbcount = config.fbcount();
		
//	Now update the panel

		Panel headpanel = new Panel();
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constrain = new GridBagConstraints();
        constrain.weightx = 1.0;
        constrain.fill = GridBagConstraints.BOTH;
        constrain.gridwidth = 1;
        headpanel.setLayout(gridbag);

		Label device_l = new Label("Device");
        gridbag.setConstraints(device_l, constrain);
		headpanel.add(device_l);

		Label display_l = new Label("Display");
        gridbag.setConstraints(display_l, constrain);
		headpanel.add(display_l);

		Label position_l = new Label("Position");
		gridbag.setConstraints(position_l, constrain);
		headpanel.add(position_l);

		Label visual_l = new Label("Visual");
        constrain.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(visual_l, constrain);
		headpanel.add(visual_l);

		for (int c = 0; c < fbcount; c++) {
			Node node = config.node(c);
			display = new Choice();

			Label device = new Label(config.node(c).identify());
			constrain.gridwidth = 1;
			gridbag.setConstraints(device, constrain); 
			headpanel.add(device);

			for (int d = 0; d < fbcount; d++) {
				display.add(":0."+d);
			}

			display.select(c);
			display.add("Not in use");
			gridbag.setConstraints(display, constrain);
			headpanel.add(display);
			displays.addElement(display);

			position = new Choice();
			position.add(" left ");
			position.add(" right ");
			position.add(" top ");
			position.add(" bottom ");
			if (c > 0)
				position.select(1);
			else
				position.select(0);
			gridbag.setConstraints(position, constrain);
			headpanel.add(position);
			positions.addElement(position);

			int depth = node.get_avail_depth();
			
			visual = new Choice();
			int select = 0;
			if (depth == 1) {
				visual.add(" 1-bit StaticGray");
			} else {	
				visual.add(" 8-bit PseudoColor");
				visual.add(" 8-bit StaticColor");
				visual.add(" 8-bit StaticGray");
				visual.add(" 8-bit GrayScale");
				visual.add(" 8-bit TrueColor");
				visual.add(" 8-bit DirectColor");
				if (depth == 24) {
					select = visual.getItemCount();
					visual.add("24-bit TrueColor");
					if (node.directcolor_24bit()) {
						visual.add("24-bit DirectColor");
					}
				}
			}
			visual.select(select);
			constrain.gridwidth = GridBagConstraints.REMAINDER;
			gridbag.setConstraints(visual, constrain);
			headpanel.add(visual);
			visuals.addElement(visual);
		}

		setLayout(new BorderLayout());
		add("North", headpanel);

		Panel footer = new Panel();
		footer.add(new Button(apply));
		add("South", footer);
	}

	public boolean handleEvent(Event ev) {
		String which = "";
		int count = config.fbcount();

		if (ev.id != Event.ACTION_EVENT)
			return(false);

		if (ev.target instanceof Button) {
			which = ((Button)ev.target).getLabel();
			if (which.equals(apply)) { 

//	If we can't read the Xconfig file, then CDE probably isn't installed.

				FileReader f;
				BufferedReader in;
				PrintWriter out;
				String line;

				try {

//	If /etc/dt/config/Xconfig exists, read it
//	otherwise read /usr/dt/config/Xconfig

					try {
						f = new FileReader("/etc/dt/config/Xconfig");
					} catch (FileNotFoundException e) {
						try {
							f = new FileReader("/usr/dt/config/Xconfig");
						} catch (FileNotFoundException x) {
							parent.showText("CDE is not yet installed on this machine.\nOperation aborted.");
							return true;
						}
					}
					in = new BufferedReader(f);

					try {
						out = new PrintWriter (
							new FileWriter("/tmp/Xconfig"));
					} catch (IOException x) {
						parent.showText("Cannot create temporary file /tmp/Xconfig.\nOperation aborted.");
						return true;
					}

					while ((line = in.readLine()) != null) {
						if (line.indexOf("servers") != -1)
							line = new String("Dtlogin.servers:	/etc/dt/config/Xservers\n");
						out.println(line);
					}
					in.close();
					out.close();
				} catch (IOException x) {
					String errmsg = new String("I/O Error occurred - Operation aborted.\n" + x);
					parent.showText(errmsg);
					return true;
				}
				
				try {
//	Similarly, If /etc/dt/config/Xservers exists, read it
//	otherwise read /usr/dt/config/Xservers 
					try {
						f = new FileReader("/etc/dt/config/Xservers");
					} catch (FileNotFoundException e) {
						try {
							f = new FileReader("/usr/dt/config/Xservers");
						} catch (FileNotFoundException x) {
							parent.showText("CDE is not yet installed on this machine.\nOperation aborted.");
							return true;
						}
					}
					in = new BufferedReader(f); 

					try {
						out = new PrintWriter (
							new FileWriter("/tmp/Xservers"));
					} catch (IOException x) {
						parent.showText("Cannot create temporary file /tmp/Xservers.\nOperation aborted.");
						return true;
					}

					while ((line = in.readLine()) != null) {
						if (line.startsWith("#") == false)
							out.print("### ");
							out.println(line);
					}

					String Xserver = new String("   :0   Local local_uid@console root /usr/openwin/bin/Xsun");

					for (int d = 0; d < count; d++) {
						String disp = new String(":0."+d);
						for (int c = 0; c < count; c++) {
							Choice ch = (Choice) displays.elementAt(c);
							if (disp.equals(ch.getSelectedItem())) {
								//	Got the d'th head.
								Node node = config.node(c);
								Choice pos = (Choice) positions.elementAt(c);
								Choice viz = (Choice) visuals.elementAt(c);
								String pos_s = pos.getSelectedItem();
								String viz_s = viz.getSelectedItem();

								Xserver = Xserver + " -dev " + node.devicename() +
									" defdepth " + viz_s.substring(0, 2) +
									" defclass " + viz_s.substring(7) + pos_s;
							}
						}
					}
					out.println(Xserver);
					in.close();
					out.close();
					
					String inst = new String("As root, execute the following commands:\n\n");

//	See if the user needs to create /etc/dt and /etc/dt/config

					FileReader test;
					try {
						test = new FileReader("/etc/dt");
						test.close();
					} catch (FileNotFoundException e) {
						inst = inst + "\tmkdir /etc/dt \n";
					}

					try {
						test = new FileReader("/etc/dt/config");
						test.close();
					} catch (FileNotFoundException e) {
						inst = inst + "\tmkdir /etc/dt/config \n";
					}

					inst = inst + "\tcp /tmp/Xconfig /etc/dt/config/Xconfig \n";
					inst = inst + "\tcp /tmp/Xservers /etc/dt/config/Xservers \n";
					inst = inst + "then reboot the system.\n";

					parent.showText(inst);
					return true;
				} catch (IOException x) {
					String errmsg = new String("I/O Error occurred - Operation aborted.\n" + x);
					parent.showText(errmsg);
					return true;
				}

			}
		}

		else if (ev.target instanceof Choice) {
			Choice target = (Choice) ev.target;
			for (int c = 0; c < count; c++) {
				Choice ch = (Choice) displays.elementAt(c);
				if (target.equals(ch))
					continue;
				if (target.getSelectedItem().equals(ch.getSelectedItem()))
					ch.select(count);
			}
			return true;
		}
		return false;
	}

}
