#include <gccore.h>
#include <fat.h>
#include <ogc/usbstorage.h>
#include <sdcard/wiisd_io.h>

#include "config.h"
#include "easywbfs.h"
#include "storage.h"
#include "font.h"

DISC_INTERFACE storage;


void storage_shutdown()
{
	fatUnmount("fat");
	
	//Do not shutdown usb when loading from usb
	if (storageselect == 0 || wbfsdevice != 1)
	{
		storage.shutdown();
	}
}

s32 storage_init()
{
	int ret;
	
	if (storageselect == 0)
	{
		storagename[0] = 'S';
		storagename[1] = 'D';
		storagename[2] = 0;
		storagename[3] = 0;
		storage = __io_wiisd;
	} else
	{
		storagename[0] = 'U';
		storagename[1] = 'S';
		storagename[2] = 'B';
		storagename[3] = 0;
		storage = __io_usbstorage;
	}

	ret = storage.startup();
	if (ret < 0) 
	{
		//printf("%s Error\n", storagename);
		write_font(185, 346, "%s Error", storagename);
		return ret;
	}
	ret = fatMountSimple("fat", &storage);

	if (ret < 0) 
	{
		storage_shutdown();
		//printf("FAT Error\n");
		write_font(185, 346, "FAT Error");
		return ret;
	}
	return 0;
}


