#include <alloca.h>
#include <ctype.h>
#include <errno.h>
#include <linux/fs.h>
#include <newt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <unistd.h>

#include "devices.h"
#include "fs.h"
#include "install.h"
#include "log.h"
#include "mkswap.h"
#include "perror.h"
#include "run.h"
#include "windows.h"

int nfsmount(const char *spec, const char *node, int *flags,
	     char **extra_opts, char **mount_opts);

static int selectRootPartition(struct partitionTable table, int * rootPartNum);
static int fstabCmp(const void * a, const void * b);
static int mkdirChain(char * chain);
static int mkdirIfNone(char * directory);

int canEnableSwap = 1;
int badBlocks = 0;

static int fstabCmp(const void * a, const void * b) {
    const struct fstabEntry * first = a;
    const struct fstabEntry * second = b;

    return strcmp(first->mntpoint, second->mntpoint);
}

newtComponent addPartitionListbox(struct partitionTable table,
					 newtComponent form, int left, int top, 
					 int height, int type) {
    newtComponent listbox, label;
    int i;
    char buf[80];

    listbox = newtListbox(left, top + 1, height, NEWT_LISTBOX_RETURNEXIT);

    label = newtLabel(left, top, "Device          Begin        End   "
			"Size (k)");

    for (i = 0; i < table.count; i++) {
	if (table.parts[i].type == type) {
	    sprintf(buf, "/dev/%-5s  %9d  %9d  %9d", table.parts[i].device, 
		    table.parts[i].begin, table.parts[i].end, 
		    table.parts[i].size);
	    newtListboxAddEntry(listbox, buf, &table.parts[i]);
	}
    }

    newtFormAddComponents(form, label, listbox, NULL);

    return listbox;
}

static int selectRootPartition(struct partitionTable table, int * rootPartNum) {
    newtComponent okay, cancel, form, text, listbox, answer;
    int i;
    struct partition * rootPart = NULL;

    for (i = 0; i < table.count; i++) 
	if (table.parts[i].type == PART_EXT2) break;
    if (i < table.count) {
	*rootPartNum = i;
    }

    newtOpenWindow(10, 2, 64, 19, "Select Root Partition");
    text = newtTextbox(1, 1, 62, 4, NEWT_TEXTBOX_WRAP);
    newtTextboxSetText(text, "The root partition forms the base of your Linux "
		       "filesystem. It must hold everything necessary for "
		       "booting and initializing your system. What partition "
		       "would you like to use for the root filesystem?");
    okay = newtButton(15, 15, "Ok");
    cancel = newtButton(38, 15, "Cancel");

    form = newtForm(NULL, NULL, 0);
    listbox = addPartitionListbox(table, form, 7, 6, 7, PART_EXT2);

    newtFormAddComponents(form, text, okay, cancel, NULL);
    answer = newtRunForm(form);
			
    if (answer != cancel) {
	rootPart = newtListboxGetCurrent(listbox);
    }

    newtFormDestroy(form);
    newtPopWindow();

    *rootPartNum = (rootPart - table.parts);
    if (!rootPart)
	return INST_CANCEL;
    else
	return 0;
}

static void editMountPoint(struct fstabEntry * item) {
    newtComponent form, entry, okay, cancel, clear, answer;
    char buf[50];
    char * entryValue, * chptr;
    int done = 0;
 
    newtOpenWindow(10, 7, 50, 10, "Edit Mount Point");

    form = newtForm(NULL, NULL, 0);

    strcpy(buf,"Device      : /dev/");
    strcat(buf, item->device);
    newtFormAddComponent(form, newtLabel(1, 1, buf));
    newtFormAddComponent(form, newtLabel(1, 3, "Mount point :"));

    entry = newtEntry(17, 3, item->mntpoint, 20, &entryValue, 
		      NEWT_ENTRY_SCROLL | NEWT_ENTRY_RETURNEXIT);

    okay = newtButton(5, 6, "Ok");
    clear = newtButton(20, 6, "Clear");
    cancel = newtButton(35, 6, "Cancel");

    newtFormAddComponents(form, entry, okay, clear, cancel, NULL);

    do {
	newtFormSetCurrent(form, entry);
	answer = newtRunForm(form);

	if (answer == clear)
	    newtEntrySet(entry, "", 1);
	else if (answer == cancel || !*entryValue) {
	    done = 1;
	} else if (*entryValue) {
	    chptr = entryValue;
	    if (*chptr != '/') {
		winMessage(33, 12, 30, 8, "Bad Mount Point",
			    "Mount points must begin with a leading /.");
		continue;
	    } 

	    if (*(chptr + 1) && *(chptr + strlen(chptr) - 1) == '/') {
		winMessage(33, 12, 30, 8, "Bad Mount Point",
			    "Mount points may not end with a /.");
		continue;
	    } 

	    while (*chptr && isprint(*chptr)) chptr++;

	    if (*chptr) {
		winMessage(30, 10, 30, 10, "Bad Mount Point",
			    "Mount points may only printable characters.");
		continue;
	    }

	    if (item->type != PART_EXT2 &&
		(!strncmp(entryValue, "/usr", 4) ||
		 !strncmp(entryValue, "/var", 4) ||
		 !strncmp(entryValue, "/tmp", 4) ||
		 !strncmp(entryValue, "/bin", 4) ||
		 !strncmp(entryValue, "/sbin", 4) ||
		 !strncmp(entryValue, "/etc", 4) ||
		 !strncmp(entryValue, "/boot", 4) ||
		 !strncmp(entryValue, "/dev", 4) ||
		 !strncmp(entryValue, "/root", 4) ||
		 !strncmp(entryValue, "/lib", 4))) {
		winMessage(30, 10, 30, 10, "Bad Mount Point",
			    "System partitions must be on Linux Native "
			    "partitions.");
		continue;
	    }

	    done = 1;
	}
    } while (!done);

    if (answer != cancel) {
	if (item->mntpoint) free(item->mntpoint);

	if (strlen(entryValue))
	    item->mntpoint = strdup(entryValue);
	else
	    item->mntpoint = NULL;
    }

    newtPopWindow();
}	    

void freeFstab(struct fstab fstab) {
    int i;

    for (i = 0; i < fstab.numEntries; i++) {
	if (fstab.entries[i].mntpoint) free(fstab.entries[i].mntpoint);
    }

    if (fstab.numEntries) free(fstab.entries);
}

struct fstab copyFstab(struct fstab * fstab) {
    struct fstab newfstab;
    int i, j;

    /* duplicate the current fstab */
    newfstab.numEntries = fstab->numEntries;
    newfstab.entries = malloc(fstab->numEntries * sizeof(struct fstabEntry));
    for (i = j = 0; i < newfstab.numEntries; i++) {
	if (fstab->entries[i].mntpoint) {
	    newfstab.entries[j] = fstab->entries[i];
	    newfstab.entries[j].mntpoint = strdup(fstab->entries[i].mntpoint);
	    j++;
	}
    }

    newfstab.numEntries = j;

    /* return the memory we don't actually need */
    newfstab.entries = realloc(newfstab.entries, j * sizeof(struct fstabEntry));

    return newfstab;
}

int setupMountTable(struct partitionTable table, struct fstab * finalFstab) {
    int rootPartNum = -1;
    int rc, hasdups;
    newtComponent f, okay, text, listbox, label, cancel, edit, answer;
    char buf[80];
    int i, j, numMountable, numLinux;
    int partNum;
    struct fstab fstab;
    int fstabNum;
    struct fstabEntry entry, * curr;
    int * fstabEntNum, * numptr;
    static int firstTime = 1;

    fstab = copyFstab(finalFstab);

    if (firstTime) {
	/* First, make them pick a root partition */
	if ((rc = selectRootPartition(table, &rootPartNum))) {
	    freeFstab(fstab);
	    return rc;
	}
	firstTime = 0;

	for (i = partNum = 0; i < table.count; i++) {
	    if (i == rootPartNum) {
		entry.device = table.parts[i].device;
		entry.size = table.parts[i].size;
		entry.type = table.parts[i].type;
		entry.tagName = table.parts[i].tagName;
		entry.isMounted = 0;
		entry.doFormat = 0;
		entry.mntpoint = strdup("/");
		addFstabEntry(&fstab, entry);
	    }
	}

	freeFstab(*finalFstab);
	*finalFstab = copyFstab(&fstab);
    }    

    /* If only one mountable partition exists, it would have been set up
       as root above so we can stop here */
    numLinux = numMountable = 0;
    for (i = partNum = 0; i < table.count; i++) {
	if (table.parts[i].type == PART_EXT2)
	    numMountable++, numLinux++;
	else if (table.parts[i].type == PART_DOS ||
	         table.parts[i].type == PART_HPFS)
	    numMountable++;
    }
    if (!numMountable) {
	errorWindow("You don't have any mountable partitions!");
	return INST_ERROR;
    } else if (!numLinux) {
	errorWindow("You don't have any Linux partitions available!");
	return INST_ERROR;
    } else if (numMountable == 1)
	return 0;

    newtOpenWindow(2, 2, 75, 19, "Partition Disk");

    f = newtForm(NULL, NULL, 0);
    text = newtTextbox(1, 1, 72, 4, NEWT_TEXTBOX_WRAP);
    newtTextboxSetText(text, "You may now mount other partitions within "
		       "your filesystem. Many users like to use separate "
		       "partitions for /usr and /home for example. You may "
		       "also mount your DOS or OS/2 partitions to make them "
		       "visible to Linux.");

    okay = newtButton(10, 15, "Ok");
    edit = newtButton(30, 15, "Edit");
    cancel = newtButton(50, 15, "Cancel");

    sprintf(buf, "%-10s  %9s  %-25s %-18s", "Device", "Size", 
		 "Partition type", "Mount point");
    label = newtLabel(1, 5, buf);

    listbox = newtListbox(1, 6, 8, NEWT_LISTBOX_RETURNEXIT);

    fstabEntNum = alloca(table.count * sizeof(*fstabEntNum));

    for (i = partNum = 0; i < table.count; i++) {
	if (table.parts[i].type == PART_EXT2 || 
	    table.parts[i].type == PART_DOS ||
	    table.parts[i].type == PART_HPFS) {

	    for (j = 0; j < fstab.numEntries; j++) 
		if (!strcmp(table.parts[i].device, fstab.entries[j].device))
		    break;

	    if (j < fstab.numEntries) {
		fstabNum = j;
	    } else {
		entry.device = table.parts[i].device;
		entry.size = table.parts[i].size;
		entry.type = table.parts[i].type;
		entry.tagName = table.parts[i].tagName;
		entry.isMounted = 0;
		entry.doFormat = 0;
		entry.mntpoint = NULL;
		fstabNum = addFstabEntry(&fstab, entry);
	    }

	    sprintf(buf, "/dev/%-5s  %9d  %-25s %-18s", table.parts[i].device, 
		    table.parts[i].size, table.parts[i].tagName, 
		    fstab.entries[fstabNum].mntpoint ? 
			fstab.entries[fstabNum].mntpoint : "");
	    fstabEntNum[partNum] = fstabNum;
	    newtListboxAddEntry(listbox, buf, fstabEntNum + partNum);
	    partNum++;
	} 
    }

    newtFormAddComponents(f, text, label, listbox, okay, edit, cancel, NULL);

    do { 
	answer = newtRunForm(f);
	if (answer == listbox || answer == edit) {
	    numptr = newtListboxGetCurrent(listbox);
	    curr = fstab.entries + *numptr;
	    editMountPoint(curr);
	    sprintf(buf, "/dev/%-5s  %9d  %-25s %-18s", curr->device, 
		    curr->size, curr->tagName, 
		    curr->mntpoint ? curr->mntpoint : "");
	    newtListboxSetEntry(listbox, numptr - fstabEntNum, buf);
	} else if (answer != cancel) {
	    answer = okay;
	    for (i = 0; i < fstab.numEntries; i++) 
		if (fstab.entries[i].mntpoint &&
		    !strcmp(fstab.entries[i].mntpoint, "/")) break;

	    if (i == fstab.numEntries) {
		winMessage(30, 7, 30, 10, "No Root Partition",
			    "You must assign a root (/) partition for the "
			    "install to proceed.");
		answer = NULL;
		continue;
	    }

	    hasdups = 0;
	    for (i = 0; i < fstab.numEntries; i++) 
		for (j = i + 1; j < fstab.numEntries; j++) 
		    if (fstab.entries[i].type != PART_SWAP &&
			fstab.entries[i].mntpoint &&
		        fstab.entries[j].mntpoint &&
			!strcmp(fstab.entries[i].mntpoint,
		                fstab.entries[j].mntpoint))
			hasdups = 1;

	    if (hasdups) {
		winMessage(30, 7, 30, 10, "Duplicate Mounts",
			    "You may not use the same mount point multiple "
			    "times.");
		answer = NULL;
	    }
	}
    } while (answer != okay && answer != cancel);

    newtFormDestroy(f);
    newtPopWindow();

    if (answer == cancel) { 
	freeFstab(fstab);
	return INST_CANCEL;
    }

    freeFstab(*finalFstab);
    *finalFstab = copyFstab(&fstab);
    freeFstab(fstab);

    /* Sort by mount point. This makes mounting everything in the proper
       order trivial */

    qsort(finalFstab->entries, finalFstab->numEntries, 
	  sizeof(*finalFstab->entries), fstabCmp);

    return 0;
}

static int mkExt2Filesystem(char * dev) {
    char * mke2fsargs[] = { "mke2fs", NULL, NULL, NULL};
    int rc;
    char message[80];

    mke2fsargs[1] = alloca(strlen(dev) + 6);
    strcpy(mke2fsargs[1], "/tmp/");
    strcat(mke2fsargs[1], dev);

    if (badBlocks)
	mke2fsargs[2] = "-c";

    sprintf(message, "Making ext2 filesystem on /dev/%s...", dev);
    winStatus(45, 3, "Running", message);

    devMakeInode(dev, mke2fsargs[1]);
    rc = runProgram(RUN_LOG, "/usr/bin/mke2fs", mke2fsargs);
    devRemoveInode(mke2fsargs[1]);

    newtPopWindow();

    if (rc)
	return INST_ERROR;
    else
	return 0;
}

int queryFormatFilesystems(struct fstab * fstab) {
    newtComponent form, checkList, okay, cancel, sb, text, answer;
    newtComponent checkbox;
    char * states;
    char doCheck = ' ';
    newtComponent * checks;
    char buf[80];
    int i, top;

    newtOpenWindow(11, 2, 57, 19, "Format Partitions");

    form = newtForm(NULL, NULL, 0);

    if (fstab->numEntries > 4) 
	sb = newtVerticalScrollbar(47, 7, 4, 9, 10);
    else
	sb = NULL;

    checkList = newtForm(sb, NULL, 0);
    newtFormSetHeight(checkList, 4);

    if (sb)
	newtFormAddComponent(checkList, sb);

    text = newtTextbox(1, 1, 55, 6, NEWT_TEXTBOX_WRAP);
    newtTextboxSetText(text, "What partitions would you like to "
		       "format? We strongly suggest formatting all of the "
		       "system partitions, including /, /usr, and /var. There "
		       "is no need to format /home or /usr/local if they "
		       "have already been configured during a previous "
		       "install.");

    checks = alloca(sizeof(newtComponent) * fstab->numEntries);
    states = alloca(sizeof(char) * fstab->numEntries);
    for (i = 0, top = 0; i < fstab->numEntries; i++) {
	if (fstab->entries[i].doFormat)
	    states[i] = '*';
	else
	    states[i] = ' ';

	if (fstab->entries[i].type == PART_EXT2) {
	    sprintf(buf, "/dev/%-5s  %-33s", fstab->entries[i].device, 
		   fstab->entries[i].mntpoint);
	    checks[i] = newtCheckbox(3, 7 + top++, buf, states[i], NULL, 
				     &states[i]);
	    newtFormAddComponent(checkList, checks[i]);
	} else {
	    checks[i] = NULL;
	}
    }

    okay = newtButton(12, 14, "Ok");
    cancel = newtButton(34, 14, "Cancel");
    checkbox = newtCheckbox(10, 12, "Check for bad blocks during format",
				badBlocks ? '*' : ' ', NULL, &doCheck);
    
    newtFormAddComponents(form, text, checkList, checkbox, okay, cancel, NULL);

    answer = newtRunForm(form);

    newtFormDestroy(form);
    newtPopWindow();

    if (answer == cancel) return INST_CANCEL;

    for (i = 0; i < fstab->numEntries; i++) {
        if (states[i] != ' ') 
	    fstab->entries[i].doFormat = 1; 
    }

    if (doCheck == ' ')
	badBlocks = 0;
    else
	badBlocks = 1;

    return 0;
}

int formatFilesystems(struct fstab * fstab) {
    int i;

    for (i = 0; i < fstab->numEntries; i++) {
        if (fstab->entries[i].doFormat)
	    mkExt2Filesystem(fstab->entries[i].device);
    }

    return 0;
}

int doMount(char * dev, char * where, char * fs, int rdonly) {
    char * buf = NULL;
    int isnfs = 0;
    char * mount_opt = NULL;
    long int flag;

    if (!strcmp(fs, "nfs")) isnfs = 1;

    logMessage("mounting %s on %s as type %s", dev, where, fs);

    if (testing) {
	messageWindow("Test mount", "I would mount /dev/%s on %s",
		"using a(n) %s filesystem.", dev, where, fs);
    } else {
	mkdirChain(where);

	if (!isnfs) {
	    buf = alloca(200);
	    strcpy(buf, "/tmp/");
	    strcat(buf, dev);

	    if (devMakeInode(dev, buf)) return 1;
	} else {
	    char * junk = NULL;
	    int morejunk = 0;

	    buf = dev;
	    logMessage("calling nfsmount(%s, %s, &morejunk, &junk, &mount_opt)",
			buf, where);

	    if (nfsmount(buf, where, &morejunk, &junk, &mount_opt))
		logMessage("\tnfsmount returned non-zero");
	}

	flag = MS_MGC_VAL;
	if (rdonly)
	    flag |= MS_RDONLY;
	
	logMessage("calling mount(%s, %s, %s, %ld, %p)", buf, where, fs, 
			flag, mount_opt);

	if (mount(buf, where, fs, flag, mount_opt)) {
	    messageWindow("Error", perrorstr("mount failed"));
	    return 1;
	}

	if (!isnfs) devRemoveInode(buf);
    }

    return 0;
}

int mountFilesystems(struct fstab * fstab) {
    int i;
    char buf[1000];

    /* don't bother mounting odd (non-ext2) filesystems - we don't need
       them for installs */
   
    chdir("/");

    for (i = 0; i < fstab->numEntries; i++) {
	if (fstab->entries[i].type == PART_EXT2) {
	    strcpy(buf, "/mnt");
	    strcat(buf, fstab->entries[i].mntpoint);

	    if (!doMount(fstab->entries[i].device, buf, "ext2", 0))
		fstab->entries[i].isMounted = 1;
	    else {
		logMessage("unmounting all filesystems due to mount error");
		umountFilesystems(fstab);
		return INST_ERROR;
	    }
	}    
    }

    return 0;
}

int umountFilesystems(struct fstab * fstab) {
    char buf[1000];
    int i;
    int olderrno;

    logMessage("unmounting all filesystems");

    chdir("/");

    if (testing) return 0;

    for (i = fstab->numEntries - 1; i >= 0; i--) {
	if (fstab->entries[i].isMounted) {
	    strcpy(buf, "/mnt");
	    strcat(buf, fstab->entries[i].mntpoint);

	    fstab->entries[i].isMounted = 0;
	    if (umount(buf)) {
		olderrno = errno;
		logMessage("error unmounting %s: %s\n", buf, strerror(errno));
		errno = olderrno;
		errorWindow("error unmounting filesystem: %s");
	    }
	}    
    }

    return 0;
}

static int mkdirChain(char * origChain) {
    char * chain;
    char * chptr;

    chain = alloca(strlen(origChain) + 1);
    strcpy(chain, origChain);
    chptr = chain;

    if (testing) return 0;

    while ((chptr = strchr(chptr, '/'))) {
	*chptr = '\0';
	if (mkdirIfNone(chain)) {
	    *chptr = '/';
	    return INST_ERROR;
	}

	*chptr = '/';
	chptr++;
    }

    if (mkdirIfNone(chain))
	return INST_ERROR;

    return 0;
}

static int mkdirIfNone(char * directory) {
    int rc, mkerr;
    char * chptr;

    /* if the path is '/' we get ENOFILE not found" from mkdir, rather
       then EEXIST which is weird */
    for (chptr = directory; *chptr; chptr++)
        if (*chptr != '/') break;
    if (!*chptr) return 0;

    rc = mkdir(directory, 0755);
    mkerr = errno;

    logMessage("creating directory %s rc = %d", directory, rc);

    if (!rc || mkerr == EEXIST) return 0;

    logMessage("    error: %s", strerror(mkerr));

    return INST_ERROR;
}

int writeFstab(struct fstab * fstab) {
    int i;
    FILE * f;
    char * fs = NULL;
    int freq = 0;
    int passno = 0;
    int bad;
    char * format = "/dev/%-18s %-23s %-7s %-15s %d %d\n";
    char * procFormat = "%-23s %-23s %-7s %-15s %d %d\n";

    if (testing)
	return 0;

    logMessage("creating /etc/fstab");

    f = fopen("/mnt/etc/fstab", "w");
    if (!f) {
	errorWindow("error creating /mnt/etc/fstab");
	return INST_ERROR;
    }

    for (i = 0; i < fstab->numEntries; i++) {
	if (!fstab->entries[i].mntpoint) continue;

	bad = 0;
	switch (fstab->entries[i].type) {
	  case PART_EXT2:
	    freq = 1;
	    fs = "ext2";
	    if (!strcmp(fstab->entries[i].mntpoint, "/"))
		passno = 1;
	    else
		passno = 2;
	  
	    break;

	  case PART_SWAP:
	    fs = "swap";
	    passno = 0;
	    freq = 0;
	    break;

	  case PART_DOS:
	    fs = "msdos";
	    passno = 0;
	    freq = 0;
	    break;

	  case PART_HPFS:
	    fs = "hpfs";
	    passno = 0;
	    freq = 0;
	    break;

	  default:
	    bad = 1;
	}

	if (!bad)
	    fprintf(f, format, fstab->entries[i].device, 
		    fstab->entries[i].mntpoint, fs, "defaults", freq, passno);
    }

    fprintf(f, format, "fd0", "/mnt/floppy", "ext2", "noauto", 0, 0);
    fprintf(f, procFormat, "none", "/proc", "proc", "defaults", 0, 0);

    fclose(f);

    return 0;
}

int addFstabEntry(struct fstab * fstab, struct fstabEntry entry) {
    int i;

    for (i = 0; i < fstab->numEntries; i++) 
	if (!strcmp(entry.device, fstab->entries[i].device))
	    break;

    if (i == fstab->numEntries) {
	fstab->numEntries++;
	if (fstab->numEntries > 1)
	    fstab->entries = realloc(fstab->entries, 
				sizeof(entry) * fstab->numEntries);
	else
	    fstab->entries = malloc(sizeof(entry));
    }

    fstab->entries[i] = entry;

    return i;
}

int activeSwapSpace(struct partitionTable * table, struct fstab * finalFstab) {
    newtComponent form, checkList, okay, cancel, sb, text, answer, label;
    newtComponent check, partition, cont;
    char * states;
    char buf[80];
    int i, top, j;
    struct fstabEntry entry;
    struct fstabEntry ** entries;
    struct fstab fstab;
    static int firstTime = 1;

    fstab = copyFstab(finalFstab);    

    form = newtForm(NULL, NULL, 0);

    for (i = j = 0; i < table->count; i++)
	if (table->parts[i].type == PART_SWAP)
	    j++;

    if (!j) {
	newtOpenWindow(18, 6, 44, 10, "Setup Swap");
	text = newtTextbox(1, 1, 42, 3, NEWT_TEXTBOX_WRAP);
	newtTextboxSetText(text, "You don't have any swap space defined. Would "
				 "you like to continue, or repartition your disk?");
	
	cont = newtButton(6, 6, "Continue");
	partition = newtButton(25, 6, "Cancel");

	form = newtForm(NULL, NULL, 0);
	newtFormAddComponents(form, text, cont, partition, NULL);
	
	answer = newtRunForm(form);
	newtFormDestroy(form);
	newtPopWindow();

	if (answer == partition)
	   return INST_CANCEL;
	
	return 0;
    }

    newtOpenWindow(15, 4, 54, 14, "Active Swap Space");
	
    if (j > 3)
	sb = newtVerticalScrollbar(47, 7, 3, 9, 10);
    else
	sb = NULL;

    checkList = newtForm(sb, NULL, 0);
    newtFormSetHeight(checkList, 3);

    if (sb)
	newtFormAddComponent(checkList, sb);

    text = newtTextbox(1, 1, 52, 4, NEWT_TEXTBOX_WRAP);
    newtTextboxSetText(text, "What partitions would you like to "
		       "use for swap space? This will destroy any "
		       "information already on the partition.");

    label = newtLabel(4, 5, "  Device          Begin        End   Size (k)");

    states = alloca(sizeof(char) * table->count);
    entries = alloca(sizeof(*entries) * table->count);

    for (i = 0, top = 0; i < table->count; i++) {
	if (table->parts[i].type != PART_SWAP) continue;

	for (j = 0; j < fstab.numEntries; j++) 
	    if (!strcmp(table->parts[i].device, fstab.entries[j].device))
		break;

	if ((j < fstab.numEntries && fstab.entries[j].mntpoint) || firstTime)
	    states[i] = '*';
	else
	    states[i] = ' ';

	if (j < fstab.numEntries) 
	    entries[i] = fstab.entries + j;
	else
	    entries[i] = NULL;

	sprintf(buf, "/dev/%-5s  %9d  %9d  %9d", table->parts[i].device, 
		table->parts[i].begin, table->parts[i].end, 
		table->parts[i].size);
	check = newtCheckbox(2, 6 + top++, buf, states[i], NULL, 
				 &states[i]);
	newtFormAddComponent(checkList, check);
    }

    firstTime = 0;

    okay = newtButton(9, 10, "Ok");
    cancel = newtButton(28, 10, "Cancel");
    
    newtFormAddComponents(form, text, label, checkList, okay, cancel, NULL);

    answer = newtRunForm(form);

    newtFormDestroy(form);
    newtPopWindow();

    if (answer == cancel) {
	freeFstab(fstab);
	return INST_CANCEL;
    }

    for (i = 0; i < table->count; i++) {
	if (table->parts[i].type != PART_SWAP) continue;

        if (states[i] != ' ') {
	    if (entries[i])
		entries[i]->mntpoint = strdup("swap");
	    else {
		entry.device = table->parts[i].device;
		entry.size = table->parts[i].size;
		entry.type = table->parts[i].type;
		entry.tagName = table->parts[i].tagName;
		entry.mntpoint = strdup("swap");
		entry.doFormat = 0;
		entry.isMounted = 0;

		addFstabEntry(&fstab, entry);
	    }
	} else if (entries[i]) {
	    free(entries[i]->mntpoint);
	    entries[i]->mntpoint = NULL;
	}
    }

    if (canEnableSwap) {
	for (i = 0; i < fstab.numEntries; i++) {
	    if (fstab.entries[i].type == PART_SWAP &&
		fstab.entries[i].mntpoint) {
		enableswap(fstab.entries[i].device, 0, 1);
		canEnableSwap = 0;
	    }
	}
    }

    freeFstab(*finalFstab);
    *finalFstab = copyFstab(&fstab);
    freeFstab(fstab);

    return 0;
}
