import java.io.*;

public class FortServer {
	public static final int majversion = 0;
	public static final int minversion = 0;
	public static final int microversion = 1;

	public static final int minport = 1025;
	public static final int minplayers = 2;
	public static final int maxplayers = 6;
    
	public static BufferedReader stdin;
	
	public static String getVersion() {
    	return String.valueOf( majversion ) + "." +
		       String.valueOf( minversion ) + "." +
			   String.valueOf( microversion );
	}
    
	public static String readString() {
		String s;
	
		try {
			s = stdin.readLine();
		} catch (IOException e) {
			s = "";
		}
		
		return s;
	}
	
	public static int readInt() {
		int i;
		
		try {
			i = Integer.parseInt( stdin.readLine() );
		} catch (IOException e) {
			i = 0;
		} catch (NumberFormatException e) {
			i = 0;
		}
		
		return i;
	}
    
	public static String genName( String sdefault ) {
		String name = "";
	
		System.out.println( "Enter game name [" + sdefault + "]:" );
		name = readString();
		if (name.equals( "" )) {
			name = sdefault;
		}
		
		return name;
	}
	
	public static int genPort( int idefault ) {
		int port = 0;
		
		while (port < minport) {
			System.out.println( "Enter port to run server on [" + idefault + "]:" );
			port = readInt();
			if (port == 0) {
				port = idefault;
			}
			
			if (port < minport) {
				System.out.println( "Port must be >= " + minport + "." );
			}
		}
		
		return port;
	}
    
	public static int genNumPlayers( int idefault ) {
		int numplayers = 0;
		
		while (numplayers < minplayers || numplayers > maxplayers) {
			System.out.println( "Enter number of players [" + idefault + "]:" );
			numplayers = readInt();
			if (numplayers == 0) {
				numplayers = idefault;
			}
			
			if (numplayers < minplayers || numplayers > maxplayers) {
				System.out.println( "Number of players must be between " +
				                    minplayers + " and " + maxplayers + " inclusive." );
			}
		}
		
		return numplayers;
	}
	
	public static boolean genWatchers( boolean bdefault ) {
		return bdefault;
	}

	public static String getUsage() {
		return "Usage: java FortServer <config filename>\n" +
			   "   or: java FortServer -\n" +
			   "   or: java FortServer --help\n" +
			   "   or: java FortServer --version";
	}
	
	public static void main( String[] potato ) {
		String name = "";
		int port = 0;
		int numplayers = 0;
		boolean watchers = false;
		
		boolean startserver = false;
	
		if (potato.length < 1) {
			System.out.println( getUsage() );
		} else if (potato[ 0 ].equals( "-" )) {
			stdin = new BufferedReader( new InputStreamReader( System.in ) );
	    
			System.out.println( "Setting up Fortresses server..." );
			name = genName( "Untitled Game" );
			port = genPort( 18843 );
			numplayers = genNumPlayers( 2 );
			watchers = genWatchers( false );
			startserver = true;
		} else if (potato[ 0 ].equals( "--help" )) {
			System.out.println( getUsage() );
		} else if (potato[ 0 ].equals( "--version" )) {
			System.out.println( "Fortresses server version " + getVersion() );
		} else {
			try {
				boolean readname = false;
				boolean readport = false;
				boolean readplayers = false;
				boolean readwatchers = true;
				String s;
				
				stdin = new BufferedReader( new FileReader( potato[ 0 ] ) );
				
				while (!(readname && readport && readplayers && readwatchers)) {
					s = stdin.readLine();
					if ((s.trim()).equals( "" ) || s.startsWith( "#" )) {
						// NOP
					} else if (s.startsWith( "name " )) {
						name = s.substring( 5 );
						readname = true;
					} else if (s.startsWith( "port " )) {
						port = Integer.parseInt( s.substring( 5 ) );
						if (port < minport) {
							throw new NumberFormatException( "Port must be >= " + minport );
						}
						readport = true;
					} else if (s.startsWith( "numplayers " )) {
						numplayers = Integer.parseInt( s.substring( 11 ) );
						if (numplayers < minplayers || numplayers > maxplayers) {
							throw new NumberFormatException( "Number of players must be " +
							    "between " + minplayers + " and " + maxplayers + " inclusive." );
						}
						readplayers = true;
					} else {
						throw new IOException( "Malformed configuration file" );
					}
				}
				
				watchers = false;
				stdin.close();
				startserver = true;
			} catch (IOException e) {
				System.out.println( "Unable to read configuration file\n" +
				                    "Reason: " + e.getMessage() );
			} catch (NumberFormatException e) {
				System.out.println( "Unable to read configuration file\n" +
				                    "Reason: " + e.getMessage() );
			}
		}
	
		if (startserver) {
			FortSrv srv = new FortSrv( name, port, numplayers, watchers );
		}
	}
}
