BEGIN {

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

	print "Content-type: text/plain";
	print "";

	ret=0;
	
	port = "";
	#Use this path for development
        #path = "/home/ledocd/projects/ledocd_cli";
	#Empty path if it's installed with ledocd_cli location in PATH
        path = "";
	
	for(p=1; p<=8; p++)
	{
		profile_name[p] = "";
		for(b=1; b<=8; b++)
		{
			profile_bright[p,b] = -1;
		}
		profile_delay[p] = -1;
	}
	
	for(i=1; i<=88; i++)
	{
		lamp_profile[i] = -1;
	}
}

/^gametype/ {
	val = $2;
	sub(/\n/, "", val);
	gametype = val;
}

/^port1/ {
	val = $2;
	sub(/\n/, "", val);
	gsub(/\+/, " ", val);
	gsub(/\%2F/, "/", val);
	#printf("PORT = %s\n", val);
	port = val;
}

/^action/ {
	val = $2;
	sub(/\n/, "", val);
	#printf("action = %s\n", val);
	action = val;
}

/^profilename/ {
	profile = substr($1, 12)+0;
	val = $2;
	sub(/\n/, "", val);
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	gsub(" ", "_", val);
	profile_name[profile] = val;
}

/^profilebright/ {
	profile = substr($1, 14, 1)+0;
	bright = substr($1, 15, 1)+0;
	val = $2+0;
	profile_bright[profile, bright] = val;
}

/^profiledelay/ {
	profile = substr($1, 13, 1)+0;
	val = $2+0;
	profile_delay[profile] = val;
}

/^lampprofile/ {
	lamp = substr($1, 12, 2)+0;
	val = $2+0;
	lamp_profile[lamp] = val;
}

/^fadedelaymin/ {
	fade_delay_min = $2+0;
}

/^fadedelaymax/ {
	fade_delay_max = $2+0;
}

/^activityduration/ {
	activity_duration = $2+0;
}

/^fiftyhz/ {
	fifty_hz = $2+0;
}

/^stringinput/ {
	string = substr($1, 12, 1)+0;
	val = $2+0;
	string_input[string] = val;
}

/^stringactivity/ {
	string = substr($1, 15, 1)+0;
	val = $2+0;
	string_activity[string] = val;
}

/^stringbright/ {
	string = substr($1, 13, 1)+0;
	bright = substr($1, 14, 1)+0;
	val = $2+0;
	string_bright[string, bright] = val;
}

/^stringbrightactive/ {
	string = substr($1, 19, 1)+0;
	bright = substr($1, 20, 1)+0;
	val = $2+0;
	string_bright_active[string, bright] = val;
}

END {
	
	if((port != "") && (action != "")) {
		command = "";
		if (action == "VERSION") {
			if(gametype == "GI") {
				command = sprintf ("%sgiocd_cli -d %s -c VERSION", path, port);
			} else {
				command = sprintf ("%sledocd_cli -d %s -c VERSION -m %s", path, port, gametype);
			}
		} else if (action == "READSETTINGS") {
			if(gametype == "GI") {
				command = sprintf ("%sgiocd_cli -d %s -c READSETTINGS", path, port);
			} else {
				command = sprintf ("%sledocd_cli -d %s -c READSETTINGS -m %s", path, port, gametype);
			}
		} else if (action == "SENDSETTINGS") {
			if(gametype == "GI") {
				for(s=1; s<=6; s++)
				{
					if(string_input[s] >= 0)
					{
						command = sprintf ("%sgiocd_cli -d %s -c SETSI=%i,%i", path, port, s, string_input[s]);
						ret=system(command);
						command = "";
					}
					for(b=1; b<=8; b++)
					{
						if(string_bright[s,b] >= 0)
						{
							command = sprintf ("%sgiocd_cli -d %s -c SETSB=%i,N,%i,%i", path, port, s, b, string_bright[s,b]);
							ret=system(command);
							command = "";
						}
						if(string_bright_active[s,b] >= 0)
						{
							command = sprintf ("%sgiocd_cli -d %s -c SETSB=%i,A,%i,%i", path, port, s, b, string_bright_active[s,b]);
							ret=system(command);
							command = "";
						}
					}
					if(string_activity[s] >= 0)
					{
						command = sprintf ("%sgiocd_cli -d %s -c SETSA=%i,%i", path, port, s, string_activity[s]);
						ret=system(command);
						command = "";
					}
				}
				if((fade_delay_min >= 0) && (fade_delay_max >= 0))
				{
					command = sprintf ("%sgiocd_cli -d %s -c SETFD=%i,%i", path, port, fade_delay_min, fade_delay_max);
					ret=system(command);
					command = "";
				}
				if(activity_duration >= 0)
				{
					command = sprintf ("%sgiocd_cli -d %s -c SETAD=%i", path, port, activity_duration);
					ret=system(command);
					command = "";
				}
				if(fifty_hz >= 0)
				{
					command = sprintf ("%sgiocd_cli -d %s -c SET50=%i", path, port, fifty_hz);
					ret=system(command);
					command = "";
				}
			} else {
				for(p=1; p<=8; p++)
				{
					if(profile_name[p] != "")
					{
						command = sprintf ("%sledocd_cli -d %s -c SETPN=%i,%s -m %s", path, port, p, profile_name[p], gametype);
						ret=system(command);
						command = "";
					}
					for(b=1; b<=8; b++)
					{
						if(profile_bright[p,b] >= 0)
						{
							command = sprintf ("%sledocd_cli -d %s -c SETPB=%i,%i,%i -m %s", path, port, p, b, profile_bright[p,b], gametype);
							ret=system(command);
							command = "";
						}
					}
					if(profile_delay[p] >= 0)
					{
						command = sprintf ("%sledocd_cli -d %s -c SETPD=%i,%i -m %s", path, port, p, profile_delay[p], gametype);
						ret=system(command);
						command = "";
					}
				}
				for(i=1; i<=88; i++)
				{
					if(lamp_profile[i] >= 1)
					{
						command = sprintf ("%sledocd_cli -d %s -c SETLP=%i,%i -m %s", path, port, i, lamp_profile[i], gametype);
						ret=system(command);
						command = "";
					}
				}
			}
		} else if (action == "SAVESETTINGS") {
			if(gametype == "GI") {
				command = sprintf ("%sgiocd_cli -d %s -c SAVESETTINGS", path, port);
			} else {
				command = sprintf ("%sledocd_cli -d %s -c SAVESETTINGS -m %s", path, port, gametype);
			}
		} else if (action == "PASSTHROUGH") {
			command = sprintf ("%sledocd_cli -d %s -c PASSTHROUGH -m %s", path, port, gametype);
		} else if (action == "NORMALOCD") {
			if(gametype == "GI") {
				command = sprintf ("%sgiocd_cli -d %s -c NORMALOCD", path, port);
			} else {
				command = sprintf ("%sledocd_cli -d %s -c NORMALOCD -m %s", path, port, gametype);
			}
		} else if (action == "CHECK") {
			if(gametype == "GI") {
				command = sprintf ("%sgiocd_cli -d %s -c CHECK", path, port);
			} else {
				command = sprintf ("%sledocd_cli -d %s -c CHECK -m %s", path, port, gametype);
			}
		}
		if (command != "") {
			#once the binary is in place, remove the following line to actually run it
			#command=sprintf("echo '%s' >> /home/herg/log/ocd.txt", command);
			ret=system(command);
		}
	}
}
