#include <newt.h>
#include <sys/wait.h>
#include <unistd.h>

#include "config.h"
#include "install.h"
#include "log.h"

static int runConfigTool(char * tool) {
    int childpid;
    int status;

    logMessage("running %s", tool);
    newtSuspend();

    if (!(childpid = fork())) {
	chroot("/mnt");
	chdir("/");
	execl(tool, tool, NULL);
	exit(2);
    } 

    waitpid(childpid, &status, 0);
    newtResume();
    if (WIFEXITED(status))
	return 0;
    
    if (WEXITSTATUS(status) == 1) {
	logMessage("    tool canceled");
	return INST_CANCEL;
    }

    logMessage("    tool failed");

    return INST_ERROR;
}

int timeConfig(void) {
    return runConfigTool("/usr/sbin/timeconfig");
}

int kbdConfig(void) {
    return runConfigTool("/usr/sbin/kbdconfig");
}

int mouseConfig(void) {
    return runConfigTool("/usr/sbin/mouseconfig");
}

void configPCMCIA(char * pcic) {
    FILE * f;

    if (testing) return;

    f = fopen("/mnt/etc/sysconfig/pcmcia", "w");
    if (pcic) {
	fprintf(f, "PCMCIA=yes\n");
	fprintf(f, "PCIC=%s\n", pcic);
    } else {
	fprintf(f, "PCMCIA=no\n");
	fprintf(f, "PCIC=\n");
    }

    fprintf(f, "PCIC_OPTS=\nCORE_OPTS=\n");

    fclose(f);
}