BEGIN {

	RS="&";
	FS="=";

	JQCSSPATH="/jquery.mobile-1.2.1.min.css";
	JQBASEPATH="/jquery-1.8.3.min.js";
	JQMOBILEPATH="/jquery.mobile-1.2.1.min.js";
	JQPLUGINPATH="/plugin.js";

	project_path="/usr/share/";

        constants["copyright"] = "&copy;2018 LED OCD | All rights reserved.";
	constants["titlemain"] = "GI OCD Configuration";
	constants["titlehome"] = "GI OCD Configuration";
	constants["lblsubmit"] = "Apply";

	print "Content-type: text/html";
	print "";
	print "<!DOCTYPE html>";
	print "<html>";

	print "<head>";
	printf "<title>%s</title>\n", constants["titlemain"];
	print "<meta name='viewport' content='width=device-width, initial-scale=1'>\n";
	printf "<link rel='stylesheet' href='%s'/>\n", JQCSSPATH;
	printf "<script type='text/javascript' src='%s'></script>\n", JQBASEPATH;
	printf "<script type='text/javascript' src='%s'></script>\n", JQMOBILEPATH;

	port="/dev/ttyUSB0";
	
	fade_delay_min = 3;
	fade_delay_max = 10;
	activity_duration = 60;
	fifty_hz = 0;
	
	string_input[1] = 1;
	string_input[2] = 2;
	string_input[3] = 3;
	string_input[4] = 4;
	string_input[5] = 5;
	string_input[6] = 1;
	
	string_activity[1] = 0;
	string_activity[2] = 0;
	string_activity[3] = 0;
	string_activity[4] = 0;
	string_activity[5] = 0;
	string_activity[6] = 0;

	string_bright[1,1] = 1;
	string_bright[1,2] = 2;
	string_bright[1,3] = 3;
	string_bright[1,4] = 5;
	string_bright[1,5] = 9;
	string_bright[1,6] = 20;
	string_bright[1,7] = 28;
	string_bright[1,8] = 35;
	
	string_bright[2,1] = 1;
	string_bright[2,2] = 2;
	string_bright[2,3] = 3;
	string_bright[2,4] = 5;
	string_bright[2,5] = 9;
	string_bright[2,6] = 20;
	string_bright[2,7] = 28;
	string_bright[2,8] = 35;
	
	string_bright[3,1] = 1;
	string_bright[3,2] = 2;
	string_bright[3,3] = 3;
	string_bright[3,4] = 5;
	string_bright[3,5] = 9;
	string_bright[3,6] = 20;
	string_bright[3,7] = 28;
	string_bright[3,8] = 35;
	
	string_bright[4,1] = 1;
	string_bright[4,2] = 2;
	string_bright[4,3] = 3;
	string_bright[4,4] = 5;
	string_bright[4,5] = 9;
	string_bright[4,6] = 20;
	string_bright[4,7] = 28;
	string_bright[4,8] = 35;
	
	string_bright[5,1] = 1;
	string_bright[5,2] = 2;
	string_bright[5,3] = 3;
	string_bright[5,4] = 5;
	string_bright[5,5] = 9;
	string_bright[5,6] = 20;
	string_bright[5,7] = 28;
	string_bright[5,8] = 35;

	string_bright[6,1] = 5;
	string_bright[6,2] = 7;
	string_bright[6,3] = 10;
	string_bright[6,4] = 16;
	string_bright[6,5] = 28;
	string_bright[6,6] = 57;
	string_bright[6,7] = 81;
	string_bright[6,8] = 100;

	string_bright_active[1,1] = 1;
	string_bright_active[1,2] = 2;
	string_bright_active[1,3] = 3;
	string_bright_active[1,4] = 5;
	string_bright_active[1,5] = 9;
	string_bright_active[1,6] = 20;
	string_bright_active[1,7] = 28;
	string_bright_active[1,8] = 35;
	
	string_bright_active[2,1] = 1;
	string_bright_active[2,2] = 2;
	string_bright_active[2,3] = 3;
	string_bright_active[2,4] = 5;
	string_bright_active[2,5] = 9;
	string_bright_active[2,6] = 20;
	string_bright_active[2,7] = 28;
	string_bright_active[2,8] = 35;
	
	string_bright_active[3,1] = 1;
	string_bright_active[3,2] = 2;
	string_bright_active[3,3] = 3;
	string_bright_active[3,4] = 5;
	string_bright_active[3,5] = 9;
	string_bright_active[3,6] = 20;
	string_bright_active[3,7] = 28;
	string_bright_active[3,8] = 35;
	
	string_bright_active[4,1] = 1;
	string_bright_active[4,2] = 2;
	string_bright_active[4,3] = 3;
	string_bright_active[4,4] = 5;
	string_bright_active[4,5] = 9;
	string_bright_active[4,6] = 20;
	string_bright_active[4,7] = 28;
	string_bright_active[4,8] = 35;
	
	string_bright_active[5,1] = 1;
	string_bright_active[5,2] = 2;
	string_bright_active[5,3] = 3;
	string_bright_active[5,4] = 5;
	string_bright_active[5,5] = 9;
	string_bright_active[5,6] = 20;
	string_bright_active[5,7] = 28;
	string_bright_active[5,8] = 35;

	string_bright_active[6,1] = 5;
	string_bright_active[6,2] = 7;
	string_bright_active[6,3] = 10;
	string_bright_active[6,4] = 16;
	string_bright_active[6,5] = 28;
	string_bright_active[6,6] = 57;
	string_bright_active[6,7] = 81;
	string_bright_active[6,8] = 100;

}

/^fade-delay-min/ {
	val = $2+0;
	fade_delay_min = val;
}

/^fade-delay-max/ {
	val = $2+0;
	fade_delay_max = val;
}

/^activity-duration/ {
	val = $2+0;
	activity_duration = val;
}

/^50hz-check/ {
	fifty_hz = 0;
	if(substr($2, 1, 2) == "on")
	{
		fifty_hz = 1;
	}
}

/^menu-input/ {
	#menu-input#
	string = substr($1,11)+0;
	#Input # or Always On
	if(substr($2, 1, 6) == "Always")
	{
		string_input[string] = 6;
	}
	else
	{
		string_input[string] = substr($2, 7, 1);
	}
}

/^activity-check/ {
	string = substr($1,15)+0;
	string_activity[string] = 0;
	if(substr($2, 1, 2) == "on")
	{
		string_activity[string] = 1;
	}
}

/^s1-b1/ {
	string_bright[1,1] = $2;
}

/^s1-b2/ {
	string_bright[1,2] = $2;
}

/^s1-b3/ {
	string_bright[1,3] = $2;
}

/^s1-b4/ {
	string_bright[1,4] = $2;
}

/^s1-b5/ {
	string_bright[1,5] = $2;
}

/^s1-b6/ {
	string_bright[1,6] = $2;
}

/^s1-b7/ {
	string_bright[1,7] = $2;
}

/^s1-b8/ {
	string_bright[1,8] = $2;
}

/^s2-b1/ {
	string_bright[2,1] = $2;
}

/^s2-b2/ {
	string_bright[2,2] = $2;
}

/^s2-b3/ {
	string_bright[2,3] = $2;
}

/^s2-b4/ {
	string_bright[2,4] = $2;
}

/^s2-b5/ {
	string_bright[2,5] = $2;
}

/^s2-b6/ {
	string_bright[2,6] = $2;
}

/^s2-b7/ {
	string_bright[2,7] = $2;
}

/^s2-b8/ {
	string_bright[2,8] = $2;
}

/^s3-b1/ {
	string_bright[3,1] = $2;
}

/^s3-b2/ {
	string_bright[3,2] = $2;
}

/^s3-b3/ {
	string_bright[3,3] = $2;
}

/^s3-b4/ {
	string_bright[3,4] = $2;
}

/^s3-b5/ {
	string_bright[3,5] = $2;
}

/^s3-b6/ {
	string_bright[3,6] = $2;
}

/^s3-b7/ {
	string_bright[3,7] = $2;
}

/^s3-b8/ {
	string_bright[3,8] = $2;
}

/^s4-b1/ {
	string_bright[4,1] = $2;
}

/^s4-b2/ {
	string_bright[4,2] = $2;
}

/^s4-b3/ {
	string_bright[4,3] = $2;
}

/^s4-b4/ {
	string_bright[4,4] = $2;
}

/^s4-b5/ {
	string_bright[4,5] = $2;
}

/^s4-b6/ {
	string_bright[4,6] = $2;
}

/^s4-b7/ {
	string_bright[4,7] = $2;
}

/^s4-b8/ {
	string_bright[4,8] = $2;
}

/^s5-b1/ {
	string_bright[5,1] = $2;
}

/^s5-b2/ {
	string_bright[5,2] = $2;
}

/^s5-b3/ {
	string_bright[5,3] = $2;
}

/^s5-b4/ {
	string_bright[5,4] = $2;
}

/^s5-b5/ {
	string_bright[5,5] = $2;
}

/^s5-b6/ {
	string_bright[5,6] = $2;
}

/^s5-b7/ {
	string_bright[5,7] = $2;
}

/^s5-b8/ {
	string_bright[5,8] = $2;
}

/^s6-b1/ {
	string_bright[6,1] = $2;
}

/^s6-b2/ {
	string_bright[6,2] = $2;
}

/^s6-b3/ {
	string_bright[6,3] = $2;
}

/^s6-b4/ {
	string_bright[6,4] = $2;
}

/^s6-b5/ {
	string_bright[6,5] = $2;
}

/^s6-b6/ {
	string_bright[6,6] = $2;
}

/^s6-b7/ {
	string_bright[6,7] = $2;
}

/^s6-b8/ {
	string_bright[6,8] = $2;
}

/^s1a-b1/ {
	string_bright_active[1,1] = $2;
}

/^s1a-b2/ {
	string_bright_active[1,2] = $2;
}

/^s1a-b3/ {
	string_bright_active[1,3] = $2;
}

/^s1a-b4/ {
	string_bright_active[1,4] = $2;
}

/^s1a-b5/ {
	string_bright_active[1,5] = $2;
}

/^s1a-b6/ {
	string_bright_active[1,6] = $2;
}

/^s1a-b7/ {
	string_bright_active[1,7] = $2;
}

/^s1a-b8/ {
	string_bright_active[1,8] = $2;
}

/^s2a-b1/ {
	string_bright_active[2,1] = $2;
}

/^s2a-b2/ {
	string_bright_active[2,2] = $2;
}

/^s2a-b3/ {
	string_bright_active[2,3] = $2;
}

/^s2a-b4/ {
	string_bright_active[2,4] = $2;
}

/^s2a-b5/ {
	string_bright_active[2,5] = $2;
}

/^s2a-b6/ {
	string_bright_active[2,6] = $2;
}

/^s2a-b7/ {
	string_bright_active[2,7] = $2;
}

/^s2a-b8/ {
	string_bright_active[2,8] = $2;
}

/^s3a-b1/ {
	string_bright_active[3,1] = $2;
}

/^s3a-b2/ {
	string_bright_active[3,2] = $2;
}

/^s3a-b3/ {
	string_bright_active[3,3] = $2;
}

/^s3a-b4/ {
	string_bright_active[3,4] = $2;
}

/^s3a-b5/ {
	string_bright_active[3,5] = $2;
}

/^s3a-b6/ {
	string_bright_active[3,6] = $2;
}

/^s3a-b7/ {
	string_bright_active[3,7] = $2;
}

/^s3a-b8/ {
	string_bright_active[3,8] = $2;
}

/^s4a-b1/ {
	string_bright_active[4,1] = $2;
}

/^s4a-b2/ {
	string_bright_active[4,2] = $2;
}

/^s4a-b3/ {
	string_bright_active[4,3] = $2;
}

/^s4a-b4/ {
	string_bright_active[4,4] = $2;
}

/^s4a-b5/ {
	string_bright_active[4,5] = $2;
}

/^s4a-b6/ {
	string_bright_active[4,6] = $2;
}

/^s4a-b7/ {
	string_bright_active[4,7] = $2;
}

/^s4a-b8/ {
	string_bright_active[4,8] = $2;
}

/^s5a-b1/ {
	string_bright_active[5,1] = $2;
}

/^s5a-b2/ {
	string_bright_active[5,2] = $2;
}

/^s5a-b3/ {
	string_bright_active[5,3] = $2;
}

/^s5a-b4/ {
	string_bright_active[5,4] = $2;
}

/^s5a-b5/ {
	string_bright_active[5,5] = $2;
}

/^s5a-b6/ {
	string_bright_active[5,6] = $2;
}

/^s5a-b7/ {
	string_bright_active[5,7] = $2;
}

/^s5a-b8/ {
	string_bright_active[5,8] = $2;
}

/^s6a-b1/ {
	string_bright_active[6,1] = $2;
}

/^s6a-b2/ {
	string_bright_active[6,2] = $2;
}

/^s6a-b3/ {
	string_bright_active[6,3] = $2;
}

/^s6a-b4/ {
	string_bright_active[6,4] = $2;
}

/^s6a-b5/ {
	string_bright_active[6,5] = $2;
}

/^s6a-b6/ {
	string_bright_active[6,6] = $2;
}

/^s6a-b7/ {
	string_bright_active[6,7] = $2;
}

/^s6a-b8/ {
	string_bright_active[6,8] = $2+0;
}



END {
	
	
	print "<script type='text/javascript'>";
	print "</script>";
	printf "<script type='text/javascript' src='%s'></script>\n", JQPLUGINPATH;
	print "</head>";

	print "<body>";
	print "<form id='gi-config-form' data-ajax='false' action='giocd.html' method='post'/>";

	#############################################################################
	# Home
	#############################################################################
	
	print "<div data-role='page' id='home' data-theme='b'>";
		print "<div data-role='header' data-theme='b'>";
			printf "<a href='giocd.html' class='button' data-icon='home' data-theme='b'>%s</a>\n", "Home";
			printf "<h1>%s</h1>\n", constants["titlehome"];
			printf "<a href='#' data-icon='check' data-theme='b' class='submitButtonGI'>%s</a>\n", constants["lblsubmit"];
		print "</div>"; #end header

		print "<div data-role='content'>";

			printf "<input type='hidden' name='txt-gametype' id='txt-gametype' value='GI'/>\n";

			printf "<tr><td>Port</td><td width="150"><input type='text' name='txt-port' id='txt-port' value='%s'/></td></tr>\n", port;
			printf "<input type='button' data-theme='b' value='%s' class='testConnectionButton'/>\n", "Version";
			printf "<input type='button' data-theme='b' value='Read' class='readButton'/>\n";
			printf "<a href='#strings' data-role='button' data-icon='arrow-r' data-iconpos='right'>Strings</a>\n";
			printf "<a href='#timing' data-role='button' data-icon='arrow-r' data-iconpos='right'>Timing</a>\n";
			#printf "<input type='button' data-theme='b' value='Export...' class='exportButton'/>\n";
			#printf "<input type='button' data-theme='b' value='Import...' class='importButton'/>\n";
			printf "<input type='button' data-theme='b' value='Send' class='sendButtonGI'/>\n";
			printf "<input type='button' data-theme='b' value='Save' class='saveButton'/>\n";
			#printf "<input type='button' data-theme='b' value='Manual Test' class='manualTestButton'/>\n";
			printf "<input type='button' data-theme='b' value='Normal OCD' class='normalOCDButton'/>\n";
			printf "<input type='button' data-theme='b' value='Checkerboard' class='checkButton'/>\n";

		print "</div>"; #end content

		print "<div data-role='footer' data-theme='b'>";
			printf "<h6>%s</h6>\n", constants["copyright"];
		print "</div>"; #end footer
	print "</div>"; #end page

	#############################################################################
	# Timing Settings
	#############################################################################
	
	printf "<div data-role='page' id='timing'>\n";
		printf "<div data-role='header' data-theme='b'>\n";
			printf "<a href='giocd.html' class='button' data-icon='home' data-theme='b'>%s</a>\n", "Home";
			printf "<h1>%s</h1>\n", constants["titlehome"];
			printf "<a href='#' data-icon='check' data-theme='b' class='submitButtonGI'>%s</a>\n", constants["lblsubmit"];
		printf "</div>\n"; #end header
		printf "<div data-role='content'>\n";
			printf "<h4>Timing Settings</h4>\n";

						printf "<p>Fade Delay Min:</p>\n";
						printf "<div id='fade-delay-min-slider'>\n";
						#printf "<label for='fade-delay-min-slider'>Fade Delay Min:</label>\n";
						printf "<input type='range' name='fade-delay-min' id='fade-delay-min' data-highlight='true' value='%s' min='0' max='49' data-track-theme='b'/>\n", fade_delay_min;
						printf "</div>\n";

						printf "<p>Fade Delay Max:</p>\n";
						printf "<div id='fade-delay-max-slider'>\n";
						#printf "<label for='fade-delay-max-slider'>Fade Delay Max:</label>\n";
						printf "<input type='range' name='fade-delay-max' id='fade-delay-max' data-highlight='true' value='%s' min='0' max='49' data-track-theme='b'/>\n", fade_delay_max;
						printf "</div>\n";

						printf "<p>Activity Duration:</p>\n";
						printf "<div id='activity-duration-slider'>\n";
						#printf "<label for='activity-duration-slider'>Activity Duration:</label>\n";
						printf "<input type='range' name='activity-duration' id='activity-duration' data-highlight='true' value='%s' min='0' max='250' data-track-theme='b'/>\n", activity_duration;
						printf "</div>\n";

					checked= "";
					if (fifty_hz) {
						checked = "checked='checked'";
					}

 							printf "<fieldset data-role='controlgroup'>";
								printf "<div id='50hz-checkbox'>\n";
									printf "<label for='50hz-check'>50HZ Timing</label>\n";
									printf "<input type='checkbox' %s name='50hz-check' id='50hz-check' class='includesbox' data-theme='b'/>\n", checked;
								printf "</div>\n";
							printf "</fieldset>";

		printf "</div>\n"; #end content
		printf "<div data-role='footer' data-theme='b'>\n";
			printf "<h6>%s</h6>\n", constants["copyright"];
		printf "</div>\n"; #end footer
	printf "</div>\n"; #end page

	#############################################################################
	# String Settings
	#############################################################################
	
	printf "<div data-role='page' id='strings'>\n";
		printf "<div data-role='header' data-theme='b'>\n";
			printf "<a href='giocd.html' class='button' data-icon='home' data-theme='b'>%s</a>\n", "Home";
			printf "<h1>%s</h1>\n", constants["titlehome"];
			printf "<a href='#' data-icon='check' data-theme='b' class='submitButtonGI'>%s</a>\n", constants["lblsubmit"];
		printf "</div>\n"; #end header
		printf "<div data-role='content'>\n";
			printf "<h4>Profile Settings</h4>\n";
                                printf "<div data-role='collapsible-set' data-iconpos='right' data-theme='b'>"
 				for (string=1; string<=6; string++) {
			  	printf "<div data-role='collapsible' data-iconpos='right' data-inset='false' data-theme='b'>"
    			if(string <= 5) printf "<h2>String %s</h2>", string;
    			else printf "<h2>Mod Control</h2>", string;
						printf "<label for='menu-input%s' class='select'>Controlled by</label>", string;
						printf "<select name='menu-input%s' id='menu-input%s' data-theme='b'>", string, string;
							for (input=1; input<=5; input++) {
								selected= "";
								if (input == string_input[string]) {
									selected = "selected='selected'";
								}
								printf "<option %s>Input %s</option>", selected, input;
							}
								selected= "";
								if (6 == string_input[string]) {
									selected = "selected='selected'";
								}
								printf "<option %s>Always On</option>", selected;
						printf "</select>\n"
						for (bright=1; bright<=8; bright++) {
							printf "<div id='string%s-b%s-slider'>\n", string, bright;
							printf "<label for='string%s-b%s-slider'>B%s:</label>\n", string, bright, bright;
							printf "<input type='range' name='s%s-b%s' id='s%s-b%s' data-highlight='true' value='%s' min='0' max='100' data-track-theme='b'/>\n", string, bright, string, bright, string_bright[string, bright];
							printf "</div>\n";
						}
					checked= "";
					if (string_activity[string]) {
						checked = "checked='checked'";
					}

 							printf "<fieldset data-role='controlgroup'>";
								printf "<div id='activity-checkbox%s'>\n", string;
									printf "<label for='activity-check%s'>Activity</label>\n", string;
									printf "<input type='checkbox' %s name='activity-check%s' id='activity-check%s' class='includesbox' data-theme='b'/>\n", checked, string, string;
								printf "</div>\n";
							printf "</fieldset>";

						for (bright=1; bright<=8; bright++) {
							printf "<div id='string%sactive-b%s-slider'>\n", string, bright;
							printf "<label for='string%sactive-b%s-slider'>B%s:</label>\n", string, bright, bright;
							printf "<input type='range' name='s%sa-b%s' id='s%sa-b%s' data-highlight='true' value='%s' min='0' max='100' data-track-theme='b'/>\n", string, bright, string, bright, string_bright_active[string, bright];
							printf "</div>\n";
						}

				  print "</div>"
				}
                                  print "</div>"
		printf "</div>\n"; #end content
		printf "<div data-role='footer' data-theme='b'>\n";
			printf "<h6>%s</h6>\n", constants["copyright"];
		printf "</div>\n"; #end footer
	printf "</div>\n"; #end page


        #############################################################################
        # Busy Dialog
        #############################################################################

	printf "<div data-role='page' id='busyDialog' data-role='page' data-theme='b'>"
		printf "<div data-role='header' data-backbtn='false' data-close-btn='none' data-theme='b'>"
			printf "<h1>Busy</h1>"
		printf "</div>"
		printf "<div data-role='content' data-theme='b'>";
			printf "The software is working on your request.  Please be patient.";
		printf "</div>";
	printf "</div>";

	#############################################################################

	print "</form>";
	print "</body>";
	print "</html>";
}
