#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include "entry.h"
#define MOUNT_POINT "/tmp/mountpoint/"
#define CONFIG_FILE "wemlinux.cfg"
const char* fstypes[]={"ext2", "ext3", "ext4", "btrfs", "fat", "vfat", "ntfs"};

//#define STUB



typedef struct 
{
	char device_name[16][16];
	char device_type[16][16];
	int n;
} device_list_t;

typedef struct 
{
	device_list_t installed;
	device_list_t not_installed;
} config_device_t;

int test_file(const char* base)
{
	char tf[256];
	FILE* f = NULL;
	sprintf(tf,"%s/%s", base, CONFIG_FILE);
	f=fopen(tf, "r");
	if(f) fclose(f);
	return f!=NULL;
}

int testdevice(struct dirent *ent, config_device_t* cd)
{
	char tmp[256], tmp2[256], mp[256];
	int i;
	if(!strncmp("sd", ent->d_name, 2) && strncmp("sda", ent->d_name, 3) && strlen(ent->d_name)==4 && ent->d_name[3]>='0' && ent->d_name[3]<='9')
	{
		sprintf(tmp, "/dev/%s",ent->d_name);
		sprintf(mp, MOUNT_POINT "%s", ent->d_name);
		mkdir(mp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
		
		for(i=0; i<sizeof(fstypes)/sizeof(*fstypes); i++)
		{
			int res = mount(tmp, mp, fstypes[i], 0, NULL);
			perror("");
			if(!res)
			{
				int ok = test_file(mp);
				if(!strcmp(fstypes[i],"ntfs"))
				{
					char command[1024];
					sprintf(command, "ntfs-3g %s %s", tmp, mp);
					umount(mp);
					if(system(command))
					{
						printf("%s\n",command);
						perror("system()");
					}
				}
				if(ok)
				{
					strcpy(cd->installed.device_name[cd->installed.n], ent->d_name);
					strcpy(cd->installed.device_type[cd->installed.n], fstypes[i]);
					cd->installed.n++;
				}else
				{
					strcpy(cd->not_installed.device_name[cd->not_installed.n], ent->d_name);
					strcpy(cd->not_installed.device_type[cd->not_installed.n], fstypes[i]);
					cd->not_installed.n++;
				}
				
			}
		}
	}
}

int filter(config_device_t *cd)
{
	
	
	DIR *dir;
	struct dirent *ent;
	mkdir(MOUNT_POINT, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
	if ((dir = opendir ("/dev/")) != NULL) {
		/* print all the files and directories within directory */
		while ((ent = readdir (dir)) != NULL) {
			if(testdevice(ent, cd))
			{
			}
		}
		closedir (dir);
	} else {
		/* could not open directory */
		perror ("");
		return EXIT_FAILURE;
	}
}



int main(int argc, char** argv)
{
	int i;
	os_entries_t os;
#ifndef STUB
	config_device_t cd;
	cd.installed.n=cd.not_installed.n=0;
	if(argc>1 && !strcmp(argv[1], "-l"))
		filter(&cd);
	
	printf("Installes (%d):\n", cd.installed.n);
	for(i=0; i<cd.installed.n; i++)
		printf("\t(%s,%s) : /tmp/mountpoint/%s/wemlinux.cfg\n", cd.installed.device_name[i], cd.installed.device_type[i], cd.installed.device_name[i]);
	printf("Non nstalles (%d):\n", cd.not_installed.n);
	for(i=0; i<cd.not_installed.n; i++)
		printf("(%s,%s)\n", cd.not_installed.device_name[i], cd.not_installed.device_type[i]);
#endif
	os_entries_init(&os);
	
	if(cd.installed.n==1)
	{
		char file[256];
		sprintf(file, "/tmp/mountpoint/%s/wemlinux.cfg",  cd.installed.device_name[0]);
		if(!os_entries_parse(&os,file))
		{
			int choice;
			printf("Select kernel\n");
			for(i=0; i<os.n; i++)
			{
				printf("%d : %s\n", i, os.entries[i].name);
				printf("\t%s\n", os.entries[i].location);
				printf("\t%s\n", os.entries[i].rootfs);
				printf("\t%s\n", os.entries[i].kernel);
				/*printf("\t%s\n", os.entries[i].initrd);
				printf("\t%s\n\n", os.entries[i].append);*/
			}
			printf("%d : exit\n", i);
			printf("Your choice ...\n");
			scanf("%d", &choice);
			if(choice>=0 && choice<os.n) os_exec(cd.installed.device_name[0], &os.entries[choice]);
			else return 0;
		}
	}
}
