/******************************************************************************
 * hiperiso_disk.c  ---- hiperiso disk
 * Copyright (c) 2021, longpanda <admin@hiperiso.net>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */
#include <windows.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <hiperiso_define.h>
#include <hiperiso_disk.h>
#include <hiperiso_util.h>
#include <fat_filelib.h>

static int g_disk_num = 0;
hiperiso_disk *g_disk_list = NULL;

int hiperiso_disk_init(void)
{
	char Letter = 'A';
	DWORD Drives = GetLogicalDrives();

    vlog("hiperiso disk init ...\n");   

    g_disk_list = zalloc(sizeof(hiperiso_disk) * MAX_DISK);

	while (Drives)
	{
		if (Drives & 0x01)
		{
			if (CheckRuntimeEnvironment(Letter, g_disk_list + g_disk_num) == 0)
			{
				g_disk_list[g_disk_num].devname[0] = Letter;
				g_disk_num++;
                vlog("%C: is hiperiso disk\n", Letter);
			}
			else
			{
				memset(g_disk_list + g_disk_num, 0, sizeof(hiperiso_disk));
                vlog("%C: is NOT hiperiso disk\n", Letter);
			}
		}

		Letter++;
		Drives >>= 1;
	}

    return 0;
}

void hiperiso_disk_exit(void)
{
    vlog("hiperiso disk exit ...\n");   

    check_free(g_disk_list);
    g_disk_list = NULL;
    g_disk_num = 0;
}

const hiperiso_disk * hiperiso_get_disk_list(int *num)
{
    *num = g_disk_num;
    return g_disk_list;
}

const hiperiso_disk * hiperiso_get_disk_node(int id)
{
    if (id >= 0 && id < g_disk_num)
    {
        return g_disk_list + id;
    }

    return NULL;
}

