/*
 *  Copyright (C) 2008 Nuke (wiinuke@gmail.com)
 *  Copyright (c) 2009 WiiPower
 *
 *  this file is part of GeckoOS for USB Gecko
 *  http://www.usbgecko.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <sys/unistd.h>
#include "geckomenu.h"
#include "font.h"
#include "geckodvd.h"
#include "apploader.h"
#include "patchcode.h"
#include "tools.h"
#include "libcios.h"
#include "config.h"
#include "tools.h"

#include "easywbfs.h"


#define logogfx_size 563200


//extern u32 *xfb;
u8 gecko_command = 0;

extern s32 __SYS_LoadMenu();

static void power_cb () 
{
	Power_Flag = true;
}

static void reset_cb () 
{
	Reset_Flag = true;
}

int main(int argc, char **argv) 
{
	sleep(1);	// Maybe helps with the blackscreen issue from HBC...
	
	drive_state = 0;

	s32 di_error_code;

	Load_Config();
	
	if (Load_IOS(CIOS_VERSION) < 0)
	{
		CIOS_VERSION = 249;
		Load_IOS(CIOS_VERSION);
	}

	*(vu32*)0xCD00643C = 0x00000000;	// 32Mhz on Bus

	init_video();
	
	Power_Flag = false;
	Reset_Flag = false;
	SYS_SetPowerCallback (power_cb);
    SYS_SetResetCallback (reset_cb);	
	
/*	// Autoboot code part 1
	time_t t;
	t = time(NULL) + 1; // Wait 1 to finish wpad init process 
	while (time(NULL) < t) ;
*/

	show_banner();
	
/*	// Autoboot code part 2
	t = time(NULL) + 3; // Wait 3 seconds for button press to cancel autoboot
	bool autoboot = true;

	while (time(NULL) < t)
	{
		WPAD_ScanPads();
		PAD_ScanPads();

		if (WPAD_ButtonsDown(0) || PAD_ButtonsDown(0)) 
		{
			autoboot = false;
			break;
		}
	}
*/
	if (IOS_GetVersion() == 249)
	{
		if (IOS_GetRevision() == 8 || IOS_GetRevision() == 12 || IOS_GetRevision() == 15)
		{
			write_font(80, 140,true, "Your cIOS has revison %u", IOS_GetRevision());
			write_font(80, 164,true, "If you get 001 error, it's the");
			write_font(80, 188,true, "cIOS' fault");
			
			write_font(80, 236,true, "Press any button to continue");
			
			waitforbuttonpress(NULL, NULL);
		} else
		if (IOS_GetRevision() == 16)
		{
			write_font(80, 140,true, "Your cIOS has revison %u", IOS_GetRevision());
			write_font(80, 164,true, "Happy timeouts...");
			
			write_font(80, 236,true, "Press any button to continue");
			
			waitforbuttonpress(NULL, NULL);
		} else
		//if (IOS_GetRevision() == 14)
		//{
		//	write_font(80, 140,true, "Your cIOS has revison %u", IOS_GetRevision());
		//	write_font(80, 164,true, "Not DL compatible");
		//	
		//	write_font(80, 236,true, "Press any button to continue");
		//	
		//	waitforbuttonpress(NULL, NULL);
		//} else
		if (IOS_GetRevision() < 17 && IOS_GetRevision() != 14)
		{
			write_font(80, 140,true, "Your cIOS has revison %u", IOS_GetRevision());
			write_font(80, 164,true, "To achieve best compatibility");
			write_font(80, 188,true, "use revision 17");
			//write_font(80, 212,true, "");
			
			write_font(80, 236,true, "Press any button to continue");
			
			waitforbuttonpress(NULL, NULL);
		}
	}

	clearmenu();
	
	drawmenu(0);
	drawselected(0);
	
	/* Stop Drive */
	di_error_code = dvd_openios();
	di_error_code = dvd_stopdrive();
	dvd_closeios();
	
/*	// Autoboot code part 3
	if (autoboot)
	{
		clearmenu();
		write_font(185, 125 + 1*24,true, "Launching Game");
		write_font(80, screenheight - 60,true, "NeoGamma R8 RC1, IOS%u (Rev %u)", IOS_GetVersion(), IOS_GetRevision());
		apploader();
		sleep(6);
		clearmenu();
	} else
	{
		write_font(80, screenheight - 60,true, "Autoboot canceled");
		VIDEO_WaitVSync();
		t = time(NULL) + 3; // Wait 3 seconds for button pressing to stop 
		while (time(NULL) < t) ;
		clearscreen(xfb, 20, screenheight - 60, 278, 24, 0x00800080);
	}
*/	

	/* Main Loop */
	while(1)
	{
		/*
		if(geckoattached)
		{
		}	
		*/

		processwpad();
	}

	return 0;
}
