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

class chappe{
    //Map mapSystem = new HashMap(); 
    Map mapSystem = new TreeMap();

    public chappe() {
      
        String osType = System.getProperty("os.name" );
       	mapSystem.put("osType",osType);
	if ( osType.equals("SunOS") )  {

		try {
			BufferedReader in = new BufferedReader(new FileReader("/etc/system"));
			String str;
			String secName = "";
               	 	String varName = "";
			String varVal = "";
			while ((str = in.readLine()) != null) {
               	        	int astriLoc=str.indexOf("*");
               	        	int colonLoc=str.indexOf(":");
				int equalLoc=str.indexOf("=");
				int blankLoc=str.indexOf(" ");
				if ( equalLoc > 0 && astriLoc < 0) {
					if ( colonLoc > 0 ) {
					// Kernel Setting
					   secName = str.substring(4,colonLoc);
					   varName = str.substring(colonLoc+1,equalLoc);
					   varVal = str.substring(equalLoc+1,str.length());
					   
					} else {
					// Simple setting
					  secName = "All";
					  varName = str.substring(4,equalLoc);
					  varVal = str.substring(equalLoc+1,str.length());
	
       	                         }
       	                         mapSystem.put(secName+":"+varName,varVal);
				}
					
			}
			in.close();
		} catch (IOException e) {
                }
	} else if ( osType.equals("HP-UX") ) { 
		System.out.println("HP-UX"); 

		try {
			BufferedReader in = new BufferedReader(new FileReader("/stand/system"));
			String strLine;
			boolean foundTunable=false;  
			String strVariable="";
			String strValue="";
			int blankLoc=0;
			int lastBlk=0;
			while ((strLine = in.readLine()) != null) {
				if ( foundTunable ) {
					blankLoc=strLine.indexOf(" ");
					lastBlk=strLine.lastIndexOf(" ");
					if ( blankLoc > 0 ) {
					  strVariable = strLine.substring(0,blankLoc);
					  strValue=strLine.substring(lastBlk,strLine.length());
       	                                  mapSystem.put(strVariable,strValue);
					}
				}  else if ( strLine.indexOf("Tunable parameters") > 0 ) {
					foundTunable=true;
				}
       	                } // end of while
			in.close();
		} catch (IOException e) { System.out.println("Error " + e);
                }
	} else if ( osType.equals("AIX") ) {
		System.out.println("AIX");  
	} else if ( osType.equals("Linux") ) {
		try {
			String strLine;
			int tabLoc=0;
			int strStr=0;
			String varVal="";
			int semInx=0;
			String varName="";
			BufferedReader in = new BufferedReader(new FileReader("/proc/sys/kernel/sem"));
			while ((strLine = in.readLine()) != null) {
				do {
					tabLoc=strLine.indexOf('	',strStr); 
					if ( tabLoc == -1) {
						varVal=strLine.substring(strStr);
					} else {
						varVal=strLine.substring(strStr,tabLoc);
					}
					switch (semInx) {
					case 0: 
					  varName="SEMMSL";
					  break;
					case 1: 
					  varName="SEMMNS";
					  break;
					case 2: 
					  varName="SEMOPM";
					  break;
					case 3:
					  varName="SEMMNI";
					  break;
					default: 
					  varName="";
					}
					semInx++;
					strStr=tabLoc+1;
       	                         	mapSystem.put(varName,varVal);
				}
				while (tabLoc  > 0 );
				
			}
		} catch (IOException e) { System.out.println("Error " + e);
		}
	} else { System.out.println("Error osType = " + osType); }
     	
    } //eo contructor chappe

    public void  prtSystem() {
                 Iterator it = mapSystem.keySet().iterator();
                 while ( it.hasNext() ) {
                     String curKey = it.next().toString();
                     String curVal = mapSystem.get(curKey).toString();
		     System.out.println("Key:" + curKey + " Value:" + curVal);	
                 }

    }//eo prtSystem

    public String getSystem(String key) {
         String tmpStr = "";
         try { tmpStr = mapSystem.get(key).toString(); 
         } catch (Exception e) {
         }
         return tmpStr;
    }//eo getSystem

    public String setSystem(String key, String value) {
      String rtnStr = "";
      try {
               rtnStr = mapSystem.put(key,value).toString();
      }  catch (Exception e) {
      }
      return rtnStr;
    } //eo setSystem

    public int getSize() {
        return mapSystem.size();
    }
   
    public String getList() {
      StringBuffer strBuf = new StringBuffer();
      String curKey="";
      String curVal="";
      Iterator it = mapSystem.keySet().iterator();
      while ( it.hasNext() ) {
        curKey = it.next().toString();
        curVal = mapSystem.get(curKey).toString();
		  	strBuf.append(curKey).append("=").append(curVal).append("\n");
      }
      return strBuf.toString();

    } // end of getList

    public Map getMap() {
      return mapSystem;
    }

	public static void main(String [] args) {

		chappe myChappe = new chappe();
                System.out.println("The size is: " + myChappe.getSize());
		myChappe.prtSystem();
                if ( args.length > 0 ) {
                	System.out.println("Insert Key: " + args[0] + " Value: " + args[1]);
                	String myStr = myChappe.setSystem(args[0],args[1]);
                	System.out.println("Return Value: " + myStr );
                	System.out.println("Key: " + args[0] + " Value: " + myChappe.getSystem(args[0]));
			myChappe.prtSystem();
		}
      System.out.println(myChappe.getList());
               
	} //eo main
} //eo chappe