#include <common.h>
#include <bootmenu.h>
#include <linux/ctype.h>

DECLARE_GLOBAL_DATA_PTR;

int bootmenu_developer()
{
	char	key;
	u8	quit;
	char 	buf[512];

	quit = 0;
	
	while (!quit)
	{

/*		Display the menu
 */
		serial_puts( "Developer menu:\n\n"
				"  0. Return to previous menu\n"
				"  ------------------------------------------------------\n"
				"  1. Setup Quick update\n"
				"  2. Quick update\n"
				"  3. Set loglevel\n"
				"  4. Specify additional kernel arguments\n"
				"  5. U-Boot debug messages on\n"
				"  6. U-Boot debug messages off\n"
				"  7. Save settings\n"
				"  8. Fan speed control on (effective next boot)\n"
				"  9. Fan speed control off (power cycle required)\n"
			   );
		serial_puts(
				"\nEnter selection ==> " );

/*		Wait for a key to be pressed
 */
		key = serial_getc();

/*		If it's a menu character then echo the character and spit out a couple 
		*		of blank lines
 */
		if ( key >= '0' && key <= '9' )
		{
			serial_putc( key );
			serial_putc('\n');
			serial_putc('\n');
		}

/*		Action the selection
 */
		switch ( key )
		{
			case '0':
				quit = 1;
				break;
			case '1':
				if (bootmenu_one_off_boot(1, 1))
				{
#if !defined(CONFIG_SYS_NO_FLASH) || defined (CONFIG_SPI_FLASH)
					bootmenu_printf("  Saving settings... ");
					saveenv();
					bootmenu_printf("Complete\n");
#endif
				}	
				break;
			case '2':
				if (getenv("developer"))
				{
					gd->flags |= GD_FLG_ONEOFFBOOT_FORCED;
					bootmenu_load_file(1);	
					bootmenu_system_update();
				}
				break;
			case '3':	
				bootmenu_string_prompt(buf, "Enter loglevel", getenv("loglevel"), 2);
				setenv("loglevel", buf);
				bootmenu_printf("\n\n");
				break;
			case '4':
				bootmenu_string_prompt(buf, "Enter additional kernel args", getenv("relargs"), 512);
				setenv("relargs", buf);
				bootmenu_printf("\n\n");
				break;
			case '5':
				setenv("silent", NULL);
				bootmenu_printf("Done\n\n");
				break;
			case '6':
				setenv("silent", "1");
				bootmenu_printf("Done\n\n");
				break;
			case '7':
#if !defined(CONFIG_SYS_NO_FLASH) || defined (CONFIG_SPI_FLASH)
				bootmenu_printf("  Saving settings... ");
				saveenv();
				bootmenu_printf("Complete\n\n");
#endif
				break;
            case '8':
				setenv("fancontrol", "1");
				bootmenu_printf("Done\n\n");
				break;
			case '9':
				setenv("fancontrol", NULL);
				bootmenu_printf("Done\n\n");
				break;
			default:	
				break;			/* Ignore any other character */
		}
	}
	
	return 0;
}
