/*
  macres:  A program to extract data from Macintosh (tm) resource forks
  Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

/* Test program to list and extract Macintosh resources from a resource fork */

#include <stdlib.h>
#include <limits.h> // For PATH_MAX

#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

#include "Mac_Resource.h"
#include "Mac_Wave.h"

#ifndef PATH_MAX
#define PATH_MAX    256
#endif

static SDL_Color colors[256] = {
	/* Gamma level 3 */
	{ 0xff, 0xff, 0xff, 0xFF },  /* Pixel 0 */
	{ 0xff, 0xff, 0xcc, 0xFF },  /* Pixel 1 */
	{ 0xff, 0xff, 0x99, 0xFF },  /* Pixel 2 */
	{ 0xff, 0xff, 0x66, 0xFF },  /* Pixel 3 */
	{ 0xff, 0xff, 0x33, 0xFF },  /* Pixel 4 */
	{ 0xff, 0xff, 0x00, 0xFF },  /* Pixel 5 */
	{ 0xff, 0xcc, 0xff, 0xFF },  /* Pixel 6 */
	{ 0xff, 0xcc, 0xcc, 0xFF },  /* Pixel 7 */
	{ 0xff, 0xcc, 0x99, 0xFF },  /* Pixel 8 */
	{ 0xff, 0xcc, 0x66, 0xFF },  /* Pixel 9 */
	{ 0xff, 0xcc, 0x33, 0xFF },  /* Pixel 10 */
	{ 0xff, 0xcc, 0x00, 0xFF },  /* Pixel 11 */
	{ 0xff, 0x99, 0xff, 0xFF },  /* Pixel 12 */
	{ 0xff, 0x99, 0xcc, 0xFF },  /* Pixel 13 */
	{ 0xff, 0x99, 0x99, 0xFF },  /* Pixel 14 */
	{ 0xff, 0x99, 0x66, 0xFF },  /* Pixel 15 */
	{ 0xff, 0x99, 0x33, 0xFF },  /* Pixel 16 */
	{ 0xff, 0x99, 0x00, 0xFF },  /* Pixel 17 */
	{ 0xff, 0x66, 0xff, 0xFF },  /* Pixel 18 */
	{ 0xff, 0x66, 0xcc, 0xFF },  /* Pixel 19 */
	{ 0xff, 0x66, 0x99, 0xFF },  /* Pixel 20 */
	{ 0xff, 0x66, 0x66, 0xFF },  /* Pixel 21 */
	{ 0xff, 0x66, 0x33, 0xFF },  /* Pixel 22 */
	{ 0xff, 0x66, 0x00, 0xFF },  /* Pixel 23 */
	{ 0xff, 0x33, 0xff, 0xFF },  /* Pixel 24 */
	{ 0xff, 0x33, 0xcc, 0xFF },  /* Pixel 25 */
	{ 0xff, 0x33, 0x99, 0xFF },  /* Pixel 26 */
	{ 0xff, 0x33, 0x66, 0xFF },  /* Pixel 27 */
	{ 0xff, 0x33, 0x33, 0xFF },  /* Pixel 28 */
	{ 0xff, 0x33, 0x00, 0xFF },  /* Pixel 29 */
	{ 0xff, 0x00, 0xff, 0xFF },  /* Pixel 30 */
	{ 0xff, 0x00, 0xcc, 0xFF },  /* Pixel 31 */
	{ 0xff, 0x00, 0x99, 0xFF },  /* Pixel 32 */
	{ 0xff, 0x00, 0x66, 0xFF },  /* Pixel 33 */
	{ 0xff, 0x00, 0x33, 0xFF },  /* Pixel 34 */
	{ 0xff, 0x00, 0x00, 0xFF },  /* Pixel 35 */
	{ 0xcc, 0xff, 0xff, 0xFF },  /* Pixel 36 */
	{ 0xcc, 0xff, 0xcc, 0xFF },  /* Pixel 37 */
	{ 0xcc, 0xff, 0x99, 0xFF },  /* Pixel 38 */
	{ 0xcc, 0xff, 0x66, 0xFF },  /* Pixel 39 */
	{ 0xcc, 0xff, 0x33, 0xFF },  /* Pixel 40 */
	{ 0xcc, 0xff, 0x00, 0xFF },  /* Pixel 41 */
	{ 0xcc, 0xcc, 0xff, 0xFF },  /* Pixel 42 */
	{ 0xe0, 0xe0, 0xe0, 0xFF },  /* Pixel 43 */
	{ 0xe0, 0xe0, 0xa8, 0xFF },  /* Pixel 44 */
	{ 0xe0, 0xe0, 0x70, 0xFF },  /* Pixel 45 */
	{ 0xe0, 0xe0, 0x38, 0xFF },  /* Pixel 46 */
	{ 0xe0, 0xe0, 0x00, 0xFF },  /* Pixel 47 */
	{ 0xcc, 0x99, 0xff, 0xFF },  /* Pixel 48 */
	{ 0xe0, 0xa8, 0xe0, 0xFF },  /* Pixel 49 */
	{ 0xe0, 0xa8, 0xa8, 0xFF },  /* Pixel 50 */
	{ 0xe0, 0xa8, 0x70, 0xFF },  /* Pixel 51 */
	{ 0xe0, 0xa8, 0x38, 0xFF },  /* Pixel 52 */
	{ 0xe0, 0xa8, 0x00, 0xFF },  /* Pixel 53 */
	{ 0xcc, 0x66, 0xff, 0xFF },  /* Pixel 54 */
	{ 0xe0, 0x70, 0xe0, 0xFF },  /* Pixel 55 */
	{ 0xe0, 0x70, 0xa8, 0xFF },  /* Pixel 56 */
	{ 0xe0, 0x70, 0x70, 0xFF },  /* Pixel 57 */
	{ 0xe0, 0x70, 0x38, 0xFF },  /* Pixel 58 */
	{ 0xe0, 0x70, 0x00, 0xFF },  /* Pixel 59 */
	{ 0xcc, 0x33, 0xff, 0xFF },  /* Pixel 60 */
	{ 0xe0, 0x38, 0xe0, 0xFF },  /* Pixel 61 */
	{ 0xe0, 0x38, 0xa8, 0xFF },  /* Pixel 62 */
	{ 0xe0, 0x38, 0x70, 0xFF },  /* Pixel 63 */
	{ 0xe0, 0x38, 0x38, 0xFF },  /* Pixel 64 */
	{ 0xe0, 0x38, 0x00, 0xFF },  /* Pixel 65 */
	{ 0xcc, 0x00, 0xff, 0xFF },  /* Pixel 66 */
	{ 0xe0, 0x00, 0xe0, 0xFF },  /* Pixel 67 */
	{ 0xe0, 0x00, 0xa8, 0xFF },  /* Pixel 68 */
	{ 0xe0, 0x00, 0x70, 0xFF },  /* Pixel 69 */
	{ 0xe0, 0x00, 0x38, 0xFF },  /* Pixel 70 */
	{ 0xe0, 0x00, 0x00, 0xFF },  /* Pixel 71 */
	{ 0x99, 0xff, 0xff, 0xFF },  /* Pixel 72 */
	{ 0x99, 0xff, 0xcc, 0xFF },  /* Pixel 73 */
	{ 0x99, 0xff, 0x99, 0xFF },  /* Pixel 74 */
	{ 0x99, 0xff, 0x66, 0xFF },  /* Pixel 75 */
	{ 0x99, 0xff, 0x33, 0xFF },  /* Pixel 76 */
	{ 0x99, 0xff, 0x00, 0xFF },  /* Pixel 77 */
	{ 0x99, 0xcc, 0xff, 0xFF },  /* Pixel 78 */
	{ 0xa8, 0xe0, 0xe0, 0xFF },  /* Pixel 79 */
	{ 0xa8, 0xe0, 0xa8, 0xFF },  /* Pixel 80 */
	{ 0xa8, 0xe0, 0x70, 0xFF },  /* Pixel 81 */
	{ 0xa8, 0xe0, 0x38, 0xFF },  /* Pixel 82 */
	{ 0xa8, 0xe0, 0x00, 0xFF },  /* Pixel 83 */
	{ 0x99, 0x99, 0xff, 0xFF },  /* Pixel 84 */
	{ 0xa8, 0xa8, 0xe0, 0xFF },  /* Pixel 85 */
	{ 0xbe, 0xbe, 0xbe, 0xFF },  /* Pixel 86 */
	{ 0xbe, 0xbe, 0x7f, 0xFF },  /* Pixel 87 */
	{ 0xbe, 0xbe, 0x3f, 0xFF },  /* Pixel 88 */
	{ 0xbe, 0xbe, 0x00, 0xFF },  /* Pixel 89 */
	{ 0x99, 0x66, 0xff, 0xFF },  /* Pixel 90 */
	{ 0xa8, 0x70, 0xe0, 0xFF },  /* Pixel 91 */
	{ 0xbe, 0x7f, 0xbe, 0xFF },  /* Pixel 92 */
	{ 0xbe, 0x7f, 0x7f, 0xFF },  /* Pixel 93 */
	{ 0xbe, 0x7f, 0x3f, 0xFF },  /* Pixel 94 */
	{ 0xbe, 0x7f, 0x00, 0xFF },  /* Pixel 95 */
	{ 0x99, 0x33, 0xff, 0xFF },  /* Pixel 96 */
	{ 0xa8, 0x38, 0xe0, 0xFF },  /* Pixel 97 */
	{ 0xbe, 0x3f, 0xbe, 0xFF },  /* Pixel 98 */
	{ 0xbe, 0x3f, 0x7f, 0xFF },  /* Pixel 99 */
	{ 0xbe, 0x3f, 0x3f, 0xFF },  /* Pixel 100 */
	{ 0xbe, 0x3f, 0x00, 0xFF },  /* Pixel 101 */
	{ 0x99, 0x00, 0xff, 0xFF },  /* Pixel 102 */
	{ 0xa8, 0x00, 0xe0, 0xFF },  /* Pixel 103 */
	{ 0xbe, 0x00, 0xbe, 0xFF },  /* Pixel 104 */
	{ 0xbe, 0x00, 0x7f, 0xFF },  /* Pixel 105 */
	{ 0xbe, 0x00, 0x3f, 0xFF },  /* Pixel 106 */
	{ 0xbe, 0x00, 0x00, 0xFF },  /* Pixel 107 */
	{ 0x66, 0xff, 0xff, 0xFF },  /* Pixel 108 */
	{ 0x66, 0xff, 0xcc, 0xFF },  /* Pixel 109 */
	{ 0x66, 0xff, 0x99, 0xFF },  /* Pixel 110 */
	{ 0x66, 0xff, 0x66, 0xFF },  /* Pixel 111 */
	{ 0x66, 0xff, 0x33, 0xFF },  /* Pixel 112 */
	{ 0x66, 0xff, 0x00, 0xFF },  /* Pixel 113 */
	{ 0x66, 0xcc, 0xff, 0xFF },  /* Pixel 114 */
	{ 0x70, 0xe0, 0xe0, 0xFF },  /* Pixel 115 */
	{ 0x70, 0xe0, 0xa8, 0xFF },  /* Pixel 116 */
	{ 0x70, 0xe0, 0x70, 0xFF },  /* Pixel 117 */
	{ 0x70, 0xe0, 0x38, 0xFF },  /* Pixel 118 */
	{ 0x70, 0xe0, 0x00, 0xFF },  /* Pixel 119 */
	{ 0x66, 0x99, 0xff, 0xFF },  /* Pixel 120 */
	{ 0x70, 0xa8, 0xe0, 0xFF },  /* Pixel 121 */
	{ 0x7f, 0xbe, 0xbe, 0xFF },  /* Pixel 122 */
	{ 0x7f, 0xbe, 0x7f, 0xFF },  /* Pixel 123 */
	{ 0x7f, 0xbe, 0x3f, 0xFF },  /* Pixel 124 */
	{ 0x7f, 0xbe, 0x00, 0xFF },  /* Pixel 125 */
	{ 0x66, 0x66, 0xff, 0xFF },  /* Pixel 126 */
	{ 0x70, 0x70, 0xe0, 0xFF },  /* Pixel 127 */
	{ 0x7f, 0x7f, 0xbe, 0xFF },  /* Pixel 128 */
	{ 0x97, 0x97, 0x97, 0xFF },  /* Pixel 129 */
	{ 0x97, 0x97, 0x4c, 0xFF },  /* Pixel 130 */
	{ 0x97, 0x97, 0x00, 0xFF },  /* Pixel 131 */
	{ 0x66, 0x33, 0xff, 0xFF },  /* Pixel 132 */
	{ 0x70, 0x38, 0xe0, 0xFF },  /* Pixel 133 */
	{ 0x7f, 0x3f, 0xbe, 0xFF },  /* Pixel 134 */
	{ 0x97, 0x4c, 0x97, 0xFF },  /* Pixel 135 */
	{ 0x97, 0x4c, 0x4c, 0xFF },  /* Pixel 136 */
	{ 0x97, 0x4c, 0x00, 0xFF },  /* Pixel 137 */
	{ 0x66, 0x00, 0xff, 0xFF },  /* Pixel 138 */
	{ 0x70, 0x00, 0xe0, 0xFF },  /* Pixel 139 */
	{ 0x7f, 0x00, 0xbe, 0xFF },  /* Pixel 140 */
	{ 0x97, 0x00, 0x97, 0xFF },  /* Pixel 141 */
	{ 0x97, 0x00, 0x4c, 0xFF },  /* Pixel 142 */
	{ 0x97, 0x00, 0x00, 0xFF },  /* Pixel 143 */
	{ 0x33, 0xff, 0xff, 0xFF },  /* Pixel 144 */
	{ 0x33, 0xff, 0xcc, 0xFF },  /* Pixel 145 */
	{ 0x33, 0xff, 0x99, 0xFF },  /* Pixel 146 */
	{ 0x33, 0xff, 0x66, 0xFF },  /* Pixel 147 */
	{ 0x33, 0xff, 0x33, 0xFF },  /* Pixel 148 */
	{ 0x33, 0xff, 0x00, 0xFF },  /* Pixel 149 */
	{ 0x33, 0xcc, 0xff, 0xFF },  /* Pixel 150 */
	{ 0x38, 0xe0, 0xe0, 0xFF },  /* Pixel 151 */
	{ 0x38, 0xe0, 0xa8, 0xFF },  /* Pixel 152 */
	{ 0x38, 0xe0, 0x70, 0xFF },  /* Pixel 153 */
	{ 0x38, 0xe0, 0x38, 0xFF },  /* Pixel 154 */
	{ 0x38, 0xe0, 0x00, 0xFF },  /* Pixel 155 */
	{ 0x33, 0x99, 0xff, 0xFF },  /* Pixel 156 */
	{ 0x38, 0xa8, 0xe0, 0xFF },  /* Pixel 157 */
	{ 0x3f, 0xbe, 0xbe, 0xFF },  /* Pixel 158 */
	{ 0x3f, 0xbe, 0x7f, 0xFF },  /* Pixel 159 */
	{ 0x3f, 0xbe, 0x3f, 0xFF },  /* Pixel 160 */
	{ 0x3f, 0xbe, 0x00, 0xFF },  /* Pixel 161 */
	{ 0x33, 0x66, 0xff, 0xFF },  /* Pixel 162 */
	{ 0x38, 0x70, 0xe0, 0xFF },  /* Pixel 163 */
	{ 0x3f, 0x7f, 0xbe, 0xFF },  /* Pixel 164 */
	{ 0x4c, 0x97, 0x97, 0xFF },  /* Pixel 165 */
	{ 0x4c, 0x97, 0x4c, 0xFF },  /* Pixel 166 */
	{ 0x4c, 0x97, 0x00, 0xFF },  /* Pixel 167 */
	{ 0x33, 0x33, 0xff, 0xFF },  /* Pixel 168 */
	{ 0x38, 0x38, 0xe0, 0xFF },  /* Pixel 169 */
	{ 0x3f, 0x3f, 0xbe, 0xFF },  /* Pixel 170 */
	{ 0x4c, 0x4c, 0x97, 0xFF },  /* Pixel 171 */
	{ 0x66, 0x66, 0x66, 0xFF },  /* Pixel 172 */
	{ 0x66, 0x66, 0x00, 0xFF },  /* Pixel 173 */
	{ 0x33, 0x00, 0xff, 0xFF },  /* Pixel 174 */
	{ 0x38, 0x00, 0xe0, 0xFF },  /* Pixel 175 */
	{ 0x3f, 0x00, 0xbe, 0xFF },  /* Pixel 176 */
	{ 0x4c, 0x00, 0x97, 0xFF },  /* Pixel 177 */
	{ 0x66, 0x00, 0x66, 0xFF },  /* Pixel 178 */
	{ 0x66, 0x00, 0x00, 0xFF },  /* Pixel 179 */
	{ 0x00, 0xff, 0xff, 0xFF },  /* Pixel 180 */
	{ 0x00, 0xff, 0xcc, 0xFF },  /* Pixel 181 */
	{ 0x00, 0xff, 0x99, 0xFF },  /* Pixel 182 */
	{ 0x00, 0xff, 0x66, 0xFF },  /* Pixel 183 */
	{ 0x00, 0xff, 0x33, 0xFF },  /* Pixel 184 */
	{ 0x00, 0xff, 0x00, 0xFF },  /* Pixel 185 */
	{ 0x00, 0xcc, 0xff, 0xFF },  /* Pixel 186 */
	{ 0x00, 0xe0, 0xe0, 0xFF },  /* Pixel 187 */
	{ 0x00, 0xe0, 0xa8, 0xFF },  /* Pixel 188 */
	{ 0x00, 0xe0, 0x70, 0xFF },  /* Pixel 189 */
	{ 0x00, 0xe0, 0x38, 0xFF },  /* Pixel 190 */
	{ 0x00, 0xe0, 0x00, 0xFF },  /* Pixel 191 */
	{ 0x00, 0x99, 0xff, 0xFF },  /* Pixel 192 */
	{ 0x00, 0xa8, 0xe0, 0xFF },  /* Pixel 193 */
	{ 0x00, 0xbe, 0xbe, 0xFF },  /* Pixel 194 */
	{ 0x00, 0xbe, 0x7f, 0xFF },  /* Pixel 195 */
	{ 0x00, 0xbe, 0x3f, 0xFF },  /* Pixel 196 */
	{ 0x00, 0xbe, 0x00, 0xFF },  /* Pixel 197 */
	{ 0x00, 0x66, 0xff, 0xFF },  /* Pixel 198 */
	{ 0x00, 0x70, 0xe0, 0xFF },  /* Pixel 199 */
	{ 0x00, 0x7f, 0xbe, 0xFF },  /* Pixel 200 */
	{ 0x00, 0x97, 0x97, 0xFF },  /* Pixel 201 */
	{ 0x00, 0x97, 0x4c, 0xFF },  /* Pixel 202 */
	{ 0x00, 0x97, 0x00, 0xFF },  /* Pixel 203 */
	{ 0x00, 0x33, 0xff, 0xFF },  /* Pixel 204 */
	{ 0x00, 0x38, 0xe0, 0xFF },  /* Pixel 205 */
	{ 0x00, 0x3f, 0xbe, 0xFF },  /* Pixel 206 */
	{ 0x00, 0x4c, 0x97, 0xFF },  /* Pixel 207 */
	{ 0x00, 0x66, 0x66, 0xFF },  /* Pixel 208 */
	{ 0x00, 0x66, 0x00, 0xFF },  /* Pixel 209 */
	{ 0x00, 0x00, 0xff, 0xFF },  /* Pixel 210 */
	{ 0x00, 0x00, 0xe0, 0xFF },  /* Pixel 211 */
	{ 0x00, 0x00, 0xbe, 0xFF },  /* Pixel 212 */
	{ 0x00, 0x00, 0x97, 0xFF },  /* Pixel 213 */
	{ 0x00, 0x00, 0x66, 0xFF },  /* Pixel 214 */
	{ 0xf5, 0x00, 0x00, 0xFF },  /* Pixel 215 */
	{ 0xeb, 0x00, 0x00, 0xFF },  /* Pixel 216 */
	{ 0xd6, 0x00, 0x00, 0xFF },  /* Pixel 217 */
	{ 0xca, 0x00, 0x00, 0xFF },  /* Pixel 218 */
	{ 0xb2, 0x00, 0x00, 0xFF },  /* Pixel 219 */
	{ 0xa5, 0x00, 0x00, 0xFF },  /* Pixel 220 */
	{ 0x88, 0x00, 0x00, 0xFF },  /* Pixel 221 */
	{ 0x78, 0x00, 0x00, 0xFF },  /* Pixel 222 */
	{ 0x51, 0x00, 0x00, 0xFF },  /* Pixel 223 */
	{ 0x36, 0x00, 0x00, 0xFF },  /* Pixel 224 */
	{ 0x00, 0xf5, 0x00, 0xFF },  /* Pixel 225 */
	{ 0x00, 0xeb, 0x00, 0xFF },  /* Pixel 226 */
	{ 0x00, 0xd6, 0x00, 0xFF },  /* Pixel 227 */
	{ 0x00, 0xca, 0x00, 0xFF },  /* Pixel 228 */
	{ 0x00, 0xb2, 0x00, 0xFF },  /* Pixel 229 */
	{ 0x00, 0xa5, 0x00, 0xFF },  /* Pixel 230 */
	{ 0x00, 0x88, 0x00, 0xFF },  /* Pixel 231 */
	{ 0x00, 0x78, 0x00, 0xFF },  /* Pixel 232 */
	{ 0x00, 0x51, 0x00, 0xFF },  /* Pixel 233 */
	{ 0x00, 0x36, 0x00, 0xFF },  /* Pixel 234 */
	{ 0x00, 0x00, 0xf5, 0xFF },  /* Pixel 235 */
	{ 0x00, 0x00, 0xeb, 0xFF },  /* Pixel 236 */
	{ 0x00, 0x00, 0xd6, 0xFF },  /* Pixel 237 */
	{ 0x00, 0x00, 0xca, 0xFF },  /* Pixel 238 */
	{ 0x00, 0x00, 0xb2, 0xFF },  /* Pixel 239 */
	{ 0x00, 0x00, 0xa5, 0xFF },  /* Pixel 240 */
	{ 0x00, 0x00, 0x88, 0xFF },  /* Pixel 241 */
	{ 0x00, 0x00, 0x78, 0xFF },  /* Pixel 242 */
	{ 0x00, 0x00, 0x51, 0xFF },  /* Pixel 243 */
	{ 0x00, 0x00, 0x36, 0xFF },  /* Pixel 244 */
	{ 0xf5, 0xf5, 0xf5, 0xFF },  /* Pixel 245 */
	{ 0xeb, 0xeb, 0xeb, 0xFF },  /* Pixel 246 */
	{ 0xd6, 0xd6, 0xd6, 0xFF },  /* Pixel 247 */
	{ 0xca, 0xca, 0xca, 0xFF },  /* Pixel 248 */
	{ 0xb2, 0xb2, 0xb2, 0xFF },  /* Pixel 249 */
	{ 0xa5, 0xa5, 0xa5, 0xFF },  /* Pixel 250 */
	{ 0x88, 0x88, 0x88, 0xFF },  /* Pixel 251 */
	{ 0x78, 0x78, 0x78, 0xFF },  /* Pixel 252 */
	{ 0x51, 0x51, 0x51, 0xFF },  /* Pixel 253 */
	{ 0x36, 0x36, 0x36, 0xFF },  /* Pixel 254 */
	{ 0x00, 0x00, 0x00, 0xFF },  /* Pixel 255 */
};

static void ExportFont(Mac_Resource *res, const char *type, Uint16 id, const char *basepath)
{
	char path[PATH_MAX];
	SDL_snprintf(path, sizeof(path), "%s/Fonts", basepath);
	SDL_CreateDirectory(path);
	SDL_snprintf(path, sizeof(path), "%s/Fonts/%s#%d", basepath, type, id);

	FILE *output;
	Mac_ResData *entry;
	if ( (output=fopen(path, "w")) != NULL ) {
		entry = res->Resource(type, id);
		fwrite(entry->data, entry->length, 1,  output);
		fclose(output);			   
	}
}

static void ExportSound(Mac_Resource *res, const char *type, Uint16 id, const char *basepath)
{
	char path[PATH_MAX];
	SDL_snprintf(path, sizeof(path), "%s/Sounds", basepath);
	SDL_CreateDirectory(path);
	SDL_snprintf(path, sizeof(path), "%s/Sounds/snd#%d.wav", basepath, id);

	Mac_ResData *entry = res->Resource(type, id);
	Wave wave;
	wave.Load(entry, 0);
	wave.Save(path);
}

static void ExportSprite(Mac_Resource *res, const char *type, Uint16 id, const char *basepath)
{
	char path[PATH_MAX];
	SDL_snprintf(path, sizeof(path), "%s/Sprites", basepath);
	SDL_CreateDirectory(path);
	SDL_snprintf(path, sizeof(path), "%s/Sprites/%s#%d.png", basepath, type, id);

	bool large = (SDL_strcmp(type, "icl8") == 0);
	Mac_ResData *sprite_entry = res->Resource(type, id);
	Mac_ResData *mask_entry = res->Resource(large ? "ICN#" : "ics#", id);
	Uint8 *mask = mask_entry ? (mask_entry->data + (large ? 128 : 32)) : NULL;
	int size = (large ? 32 : 16);

	SDL_Surface *surface = SDL_CreateSurface(size, size, SDL_PIXELFORMAT_INDEX8);
	SDL_Palette *palette = SDL_CreateSurfacePalette(surface);
	SDL_SetPaletteColors(palette, colors, 0, 256);

	int i, j, used[256];
	Uint8 colorkey, m;
	Uint8 *src, *dst;

	/* Look for an unused palette entry */
	SDL_zeroa(used);
	src = sprite_entry->data;
	for ( i = 0; i < (size * size); ++i ) {
		++used[*src];
		++src;
	}
	for ( i = 0; used[i] && i < 256; ++i ) {
		/* Keep looking */;
	}
	colorkey = (Uint8)i;

	SDL_Color transparent = { 0, 0, 0, SDL_ALPHA_TRANSPARENT };
	SDL_SetPaletteColors(palette, &transparent, colorkey, 1);

	/* Copy over the pixels */
	src = sprite_entry->data;
	if ( mask ) {
		for ( i = 0; i < size; ++i ) {
			dst = (Uint8 *)surface->pixels + i*surface->pitch;
			for ( j = 0; j < size; ++j ) {
				if ( (j%8) == 0 ) {
					m = *mask++;
				}
				if ( m & 0x80 ) {
					*dst = *src;
				} else {
					*dst = colorkey;
				}
				m <<= 1;
				dst += 1;
				src += 1;
			}
		}
	} else {
		for ( i = 0; i < size; ++i ) {
			dst = (Uint8 *)surface->pixels + i*surface->pitch;
			for ( j = 0; j < size; ++j ) {
				*dst++ = *src++;
			}
		}
	}

	SDL_SavePNG(surface, path);
}

static void ExportImage(Mac_Resource *res, const char *type, Uint16 id, const char *basepath)
{
	char path[PATH_MAX];
	SDL_snprintf(path, sizeof(path), "%s/Images", basepath);
	SDL_CreateDirectory(path);
	SDL_snprintf(path, sizeof(path), "%s/Images/Maelstrom_Titles#%d.pict", basepath, id);

	Mac_ResData *entry = res->Resource(type, id);

	SDL_IOStream *stream = SDL_IOFromFile(path, "w");
	if (!stream) {
		SDL_Log("Couldn't write to %s: %s", path, SDL_GetError());
		return;
	}

	// PICT files have 512 bytes of zero preceding the actual data
	const size_t header_size = 512;
	void *header = SDL_calloc(1, header_size);
	SDL_WriteIO(stream, header, header_size);
	SDL_WriteIO(stream, entry->data, entry->length);
	SDL_CloseIO(stream);
}

static void Export(Mac_Resource *res, const char *type, Uint16 id, const char *basepath)
{
	if ( SDL_strcmp(type, "FOND") == 0 || SDL_strcmp(type, "NFNT") == 0 ) {
		ExportFont(res, type, id, basepath);
	} else if ( SDL_strcmp(type, "snd ") == 0 ) {
		ExportSound(res, type, id, basepath);
	} else if (SDL_strcmp(type, "PICT") == 0) {
		ExportImage(res, type, id, basepath);
	} else if ( SDL_strcmp(type, "ics8") == 0 || SDL_strcmp(type, "icl8") == 0 ) {
		ExportSprite(res, type, id, basepath);
	}
}

int main(int argc, char *argv[])
{
	Mac_Resource *res;
	char      **types;
	Uint16     *ids;
	int         arg, i, j;
	bool        export_res = false;

	if ( ! argv[1] ) {
		fprintf(stderr, "Usage: %s [--export] <Mac Resource File> [output_path]\n", argv[0]);
		exit(1);
	}
	arg = 1;

	if ( SDL_strcmp(argv[arg], "--export") == 0 ) {
		export_res = true;
		++arg;
	}

	res = new Mac_Resource(argv[arg]);
	if ( res->Error() ) {
		fprintf(stderr, "Mac_Resource: %s\n", res->Error());
		delete res;
		exit(2);
	}
	++arg;
	
	types = res->Types();
	for ( i=0; types[i]; ++i ) {
		ids = res->ResourceIDs(types[i]);
		printf("Resource set: type = '%s', contains %hd resources\n",
					types[i], res->NumResources(types[i]));
		for ( j=0; ids[j] < 0xFFFF; ++j ) {
			printf("\tResource %d (ID = %d): \"%s\"\n", j+1,
				ids[j], res->ResourceName(types[i], ids[j]));
			if ( argv[arg] ) {
				if ( export_res ) {
					Export(res, types[i], ids[j], argv[arg]);
				} else {
					char path[23];
					SDL_snprintf(path, sizeof(path), "%s/%s:%hu", argv[arg], types[i], ids[j]);
					FILE *output;
					Mac_ResData *D;
					if ( (output=fopen(path, "w")) != NULL ) {
						D = res->Resource(types[i], ids[j]);
						fwrite(D->data, D->length, 1,  output);
						fclose(output);			   
					}
				}
			}
		}
		delete[]  ids;
	}
	delete[] types;
	delete res;
	exit(0);
}
