/*
 * readdisk
 * 
 * Read files from Amiga disk files
 *
 * Copyright 1996 Bernd Schmidt
 *
 * Change History:
 *  Date    Person	  Action
 * --------  ------------- -----------------
 * 1996----  Bernd Schmidt Created
 * 19980409  Jim Cooper    Changed some error messages to give more
 *			   details as to what went wrong.
 * 19980412  Jim Cooper    Made it recognise DOS\1, DOS\2, DOS\3.  Much
 *			   more useful, since most diskfiles seem to be
 *			   in DOS\1 format... especially DMS files.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <fcntl.h>

unsigned char filemem[901120];

typedef struct afile
{
    struct afile *sibling;
    unsigned char *data;
    unsigned long size;
    char name[32];
} afile;

typedef struct directory
{
    struct directory *sibling;
    struct directory *subdirs;
    struct afile *files;
    char name[32];
} directory;

int secdatasize;
int secdataoffset;

static unsigned long readlong (unsigned char *buffer, int pos)
{
    return (unsigned long)((*(buffer + pos) << 24) +
    			   (*(buffer + pos + 1) << 16) +
			   (*(buffer + pos + 2) << 8) +
			   *(buffer + pos + 3));
}

static afile *read_file (unsigned char *filebuf)
{
    afile *a = (afile *)malloc(sizeof(afile));
    int sizeleft;
    unsigned char *datapos;
    unsigned long numblocks, blockpos;
    
    /* BCPL strings... Yuk. */
    memset (a->name, 0, 32);
    strncpy (a->name, (const char *)filebuf + 0x1B1, *(filebuf + 0x1B0));
    sizeleft = a->size = readlong (filebuf, 0x144);
    a->data = (unsigned char *)malloc(a->size);

    numblocks = readlong (filebuf, 0x8);
    blockpos = 0x134;
    datapos = a->data;
    while (numblocks)
    {
	unsigned char *databuf = filemem + 512*readlong (filebuf, blockpos);
	int readsize = sizeleft > secdatasize ? secdatasize : sizeleft;
	memcpy (datapos, databuf + secdataoffset, readsize);
	datapos += readsize;
	sizeleft -= readsize;
	
	blockpos -= 4;
	numblocks--;
	if (!numblocks)
	{
	    unsigned long nextflb = readlong (filebuf, 0x1F8);
	    if (nextflb)
	    {
		filebuf = filemem + 512*nextflb;
		blockpos = 0x134;
		numblocks = readlong (filebuf, 0x8);
		if (!filebuf)
		{
		    fprintf(stderr, "Disk structure corrupted. Use DISKDOCTOR to correct it.\n");
		    abort ();
		}
	    }
	}
    }
    return a;
}

static directory *read_dir (unsigned char *dirbuf)
{
    directory *d = (directory *)malloc(sizeof(directory));
    unsigned long hashsize;
    unsigned long i;
    
    memset (d->name, 0, 32);
    strncpy (d->name, (const char *)dirbuf + 0x1B1, *(dirbuf + 0x1B0));
    d->sibling = 0;
    d->subdirs = 0;
    d->files = 0;
    hashsize = readlong (dirbuf, 0xc);
    if (!hashsize)
    {
    	hashsize = 72;
    }
    if (hashsize != 72)
    {
    	fprintf(stderr, "Warning: Hash table with %d entries (should be 72!).\n", hashsize);
    }
    for (i = 0; i < hashsize; i++)
    {
	unsigned long subblock = readlong (dirbuf, 0x18 + 4*i);
	long dirtype;

	while (subblock)
	{
	    directory *subdir;
	    afile *subfile;
	    unsigned char *subbuf = filemem + 512*subblock;

	    dirtype = readlong(subbuf, 0x1FC);
	    if (dirtype > 0)
	    {
		subdir = read_dir (subbuf);
		subdir->sibling = d->subdirs;
		d->subdirs = subdir;
	    }
	    else if (dirtype < 0)
	    {
		subfile = read_file (subbuf);
		subfile->sibling = d->files;
		d->files = subfile;
	    }
	    else
	    {
		fprintf(stderr, "Directory type 0 is invalid!\n");
		abort ();
	    }
	    subblock = readlong (subbuf, 0x1F0);
	}
    }
    return d;
}

static void writedir(directory *dir)
{
    directory *subdir;
    afile *f;
    
    if (mkdir (dir->name, 0777) < 0 && errno != EEXIST)
    {
	fprintf(stderr, "Could not create directory \"%s\". Giving up.\n", dir->name);
	exit (20);	
    }
    if (chdir (dir->name) < 0)
    {
	fprintf(stderr, "Could not enter directory \"%s\". Giving up.\n", dir->name);
	exit (20);
    }
    for (subdir = dir->subdirs; subdir; subdir = subdir->sibling)
    {
    	writedir (subdir);
    }
    for (f = dir->files; f; f = f->sibling)
    {
	int fd = creat (f->name, 0666);
	if (fd < 0)
	{
	    fprintf(stderr, "Could not create file. Giving up.\n");
	    exit (20);
	}
	write (fd, f->data, f->size);
	close (fd);
    }    
    chdir ("/");
}

int main(int argc, char **argv)
{
    directory *root;
    FILE *inf;
    unsigned long disktype;

    if (argc < 2 || argc > 3)
    {
	fprintf(stderr, "Usage: readdisk <file> [<destdir>]\n");
	exit (20);
    }
    inf = fopen(argv[1], "rb");
    if (inf == NULL)
    {
	fprintf(stderr, "can't open file\n");
	exit (20);
    }
    fread(filemem, 1, 901120, inf);

    disktype = *(unsigned long *)filemem;

    switch(disktype)
    {
	case 0x444F5300L:	/* "DOS\0" */
	case 0x444F5302L:	/* "DOS\2" */
	    secdatasize = 488;
	    secdataoffset = 24;
	    break;

	case 0x444F5301L:	/* "DOS\1" */
	case 0x444F5303L:	/* "DOS\3" */
	    secdatasize = 512;
	    secdataoffset = 0;
	    break;

	default:
	    fprintf(stderr, "Don't know disktype %04x.\n", *(long *)filemem);
	    exit (20);
    }
    root = read_dir (filemem + 880*512);

    if (argc == 3)
    {
    	if (chdir (argv[2]) < 0)
	{
	    fprintf(stderr, "Couldn't change to %s. Giving up.\n", argv[2]);
	    exit (20);
	}
    }
    writedir (root);
    return 0;
}
