import java.io.File;
import java.io.RandomAccessFile;

public class CounterCgi {
    public static void main( String[] args ) {
	int count = -1;
	try {
	    File file = new File("counter");
	    boolean flag =  file.exists() ;
	    RandomAccessFile fw = new RandomAccessFile( file, "rws" );
	    // handle an initially empty file
	    if ( flag ) {
		fw.seek( 0 );
		count = fw.readInt();
	    }
	    else {
		count = 0;
	    }
	    fw.seek( 0 );
	    fw.writeInt( count+1 );
	    fw.close();
	}
	catch( Exception ex ) {
	    // ignore exception, use count -1 to indicate error
	}
	System.out.print("Content-Type: text/html\r\n\r\n");
	System.out.println("<html>" );
	System.out.println("<body>" );
	System.out.println("<h1>" );
	if ( count < 0 ) {
	    System.out.println( "Error in accessing count file" );
	}
	else {
	    System.out.printf( "Count is %07d", count );
	}
	System.out.println("</h1>" );
	System.out.println("</body>" );
	System.out.println("</html>" );
    }
}
