
#include <stdio.h>
#include <windows.h>


#include "bmp.h"
#include "quant.h"
#include "color.h"

#include "cmdlib.h"
#include "lbmlib.h"

#define NUMSKIPFILES	3
char	skipfiles[NUMSKIPFILES][64] = 
{
	"origin.bmp",
	"clip.bmp",
	"region.bmp"
};

#ifdef REF
typedef struct _WIN32_FIND_DATA { // wfd  
    DWORD dwFileAttributes; 
    FILETIME ftCreationTime; 
    FILETIME ftLastAccessTime; 
    FILETIME ftLastWriteTime; 
    DWORD    nFileSizeHigh; 
    DWORD    nFileSizeLow; 
    DWORD    dwReserved0; 
    DWORD    dwReserved1; 
    TCHAR    cFileName[ MAX_PATH ]; 
    TCHAR    cAlternateFileName[ 14 ]; 
} WIN32_FIND_DATA; 

FILE_ATTRIBUTE_ARCHIVE	
	The file is an archive file. Applications use this value to mark files for backup or removal.
FILE_ATTRIBUTE_COMPRESSED	
	The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_DIRECTORY	
	The file is a directory.
FILE_ATTRIBUTE_HIDDEN	
	The file is hidden. It is not included in an ordinary directory listing.
FILE_ATTRIBUTE_NORMAL	
	The file has no other attributes set. This value is valid only if used alone.
FILE_ATTRIBUTE_OFFLINE	
	The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage.
FILE_ATTRIBUTE_READONLY	
	The file is read-only. Applications can read the file but cannot write to it or delete it.
FILE_ATTRIBUTE_SYSTEM	
	The file is part of the operating system or is used exclusively by it.
FILE_ATTRIBUTE_TEMPORARY	
	The file is being used for temporary storage. Applications should write to the file only if absolutely necessary. Most of the file's data remains in memory without being flushed to the media because the file will soon be deleted.

#endif
 


#ifdef NEW
HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPSTR lpszPath)  
{ 
    HRESULT hres; 
    IShellLink* psl; 
    char szGotPath[MAX_PATH]; 
    char szDescription[MAX_PATH]; 
    WIN32_FIND_DATA wfd; 
 
    *lpszPath = 0; // assume failure 
 
    // Get a pointer to the IShellLink interface. 
    hres = CoCreateInstance(&CLSID_ShellLink, NULL, 
            CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl); 
    if (SUCCEEDED(hres)) { 
        IPersistFile* ppf; 
 
        // Get a pointer to the IPersistFile interface. 
        hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, 
            &ppf); 
        if (SUCCEEDED(hres)) { 
            WORD wsz[MAX_PATH]; 
 
            // Ensure that the string is Unicode. 
            MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, 
                MAX_PATH); 
 
            // Load the shortcut. 
            hres = ppf->lpVtbl->Load(ppf, wsz, STGM_READ); 
            if (SUCCEEDED(hres)) { 
 
                // Resolve the link. 
                hres = psl->lpVtbl->Resolve(psl, hwnd, SLR_ANY_MATCH); 
                if (SUCCEEDED(hres)) { 
 
                    // Get the path to the link target. 
                    hres = psl->lpVtbl->GetPath(psl, szGotPath, 
                        MAX_PATH, (WIN32_FIND_DATA *)&wfd, 
                        SLGP_SHORTPATH ); 
                    if (!SUCCEEDED(hres) 
                        HandleErr(hres); // application-defined function 
 
                    // Get the description of the target. 
                    hres = psl->lpVtbl->GetDescription(psl, 
                        szDescription, MAX_PATH); 
                    if (!SUCCEEDED(hres)) 
                        HandleErr(hres); 
                    lstrcpy(lpszPath, szGotPath); 
                } 
            } 
        // Release the pointer to the IPersistFile interface. 
        ppf->lpVtbl->Release(ppf); 
        } 
    // Release the pointer to the IShellLink interface. 
    psl->lpVtbl->Release(psl); 
    } 
    return hres; 
} 
#endif





static void DirMiptex(void *clist, char *path, int fileType, int fileWeight, int type)
{
	bitmap_t bit;
	rgb_t  *pal;
	u_char *pic;
	int w, h;

    if (fileType == FILE_BMP)
	{
//        LoadBMP(Sys_Base(path), &bit);
        LoadBMP(path, &bit);

		w = bit.width;
		h = bit.height;

        FreeBMP(&bit);
	}
	else if (fileType == FILE_LBM)
	{
//        LoadLBM(Sys_Base(path), &pic, (u_char **) &pal);
        LoadLBM(path, &pic, (u_char **) &pal);

	    w = bit.width;
		h = bit.height;

		free(pic);
		free(pal);
	}
	else
	{
		Error("DirMiptex: Unknown filetype.");
	}

	LoadMiptex(clist, path, 0, 0, w, h, fileWeight, type);
}


static int ValidKrunchFile(char *fileName, int fileType)
{
	// skip colormaps
	if (!memcmp(fileName, "colormap", 8))                 return(0);
	if ((fileType != FILE_BMP) && (fileType != FILE_LBM)) return(0);

	return(1);
}


static int GetFileWeight(char *fileName)
{
	int i;
	int len = strlen(fileName);

	// see if file has a prefix
    for (i = 0; i < len; i++)
	{
		if (fileName[i] == '_') break;
	}

	// default to 1 for files that don't start with a prefex
	if (i >= len) return(1);

	// makes sure string is long enough
	if (i < 2) return(1);

	if ((fileName[1] >= '0') && (fileName[1] <= '9'))
	{
		return(fileName[1] - '0');
	}

	// shouldn't get here?
	return(1);
}



// this function needs to be reentrant
int LoadDir(void *pal, char *path, int type)
{
	HANDLE           dirHandle;
	WIN32_FIND_DATA  findData;
	int              found = 1;
	char             fullpath[100];
	char             fullpath2[200];
	int              fileType;
	int              fileWeight;
	int				i;

	sprintf(fullpath, "%s\\*.*", path);

	// get first dll file
	dirHandle = FindFirstFile(fullpath, &findData);

	if (dirHandle == INVALID_HANDLE_VALUE)
	{
		return 0;
	}

	while (found)
	{
		// if this is a direcory then recuruse
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (findData.cFileName[0] != '.')
			{
			    sprintf(fullpath2, "%s\\%s", path, findData.cFileName);
				LoadDir(pal, fullpath2, type);
			}
		}
		else
		{
			sprintf(fullpath2, "%s\\%s", path, findData.cFileName);

			fileType = Lump_FileType(fullpath2);

			if (ValidKrunchFile(findData.cFileName, fileType))
			{
				// get weight of file
				fileWeight = GetFileWeight(findData.cFileName);

				for (i =0 ; i < NUMSKIPFILES; i++)
				{
					if (!strcmp (findData.cFileName, skipfiles[i]))
						goto skipfile;
				}

                // load file
				DirMiptex(pal, fullpath2, fileType, fileWeight, type);
			}
			else
			{
skipfile:
				printf("Skipping %s\n", fullpath2);
			}
		}

		found = FindNextFile(dirHandle, &findData);
	}

	FindClose(dirHandle);

	return 1;
}


int Lump_FileType(char *file_nm)
{
	DWORD ret;

	ret = GetFileAttributes(file_nm);

	if (ret == 0xffffffff) return(FILE_UNKNOWN);
    if (ret & FILE_ATTRIBUTE_DIRECTORY) return(FILE_DIR);

	strlwr(file_nm);

    if (!strcmp(&file_nm[strlen(file_nm) - 3], "bmp")) return(FILE_BMP);
	if (!strcmp(&file_nm[strlen(file_nm) - 3], "lbm")) return(FILE_LBM);
	if (!strcmp(&file_nm[strlen(file_nm) - 2], "ls"))  return(FILE_LS);

	return(FILE_UNKNOWN);
}