/*
 * Copyright (c)2004 The DragonFly Project.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *   Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 *   Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 * 
 *   Neither the name of the DragonFly Project nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

/*
 * fn_diagnostic.c
 * Diagnostic functions for installer.
 * $Id: fn_diagnostic.c,v 1.1 2004/07/09 23:25:40 cpressey Exp $
 */

#include <sys/types.h>

#include <dirent.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dfui/dfui.h>
#include <dfui/extbuf.h>
#include <dfui/mem.h>

#include "functions.h"
#include "diskutil.h"
#include "uiutil.h"
#include "commands.h"
#include "package.h"
#include "pathnames.h"
#include "confed.h"

/*** EXTERNS ***/

extern struct dfui_connection *c;
extern struct system_storage *ss;
extern char dfly_root[];

/*** DIAGNOSTIC FUNCTIONS ***/

void
fn_memtest(void)
{
        struct dfui_form *f;
        struct dfui_response *r;
        struct dfui_dataset *ds, *new_ds;
        struct commands *cmds;
        struct command *cmd;
	char *memtestsize;

        f = dfui_form_create(
                "memtest",
                "Memory test",
                "Memory test - Enter the size in values such as 400M, 1G.",
                "",

                "f",    "memtestsize",     "Memory test size",
                        "Enter the amount of memory you would like to check:", "",

                "a",    "ok", "OK", "", "",
                "a",    "cancel", "Cancel Memory Test", "", "",
                NULL
        );

	ds = dfui_dataset_new();
	dfui_dataset_celldata_add(ds, dfui_id_new("memtestsize"), "");
	dfui_form_dataset_add(f, ds);

        if (!dfui_be_present(c, f, &r)) abort_backend();

	if (dfui_id_is(dfui_response_get_action_id(r), "ok")) {
		new_ds = dfui_response_dataset_get_first(r);
		memtestsize = dfui_dataset_get_value(new_ds, "memtestsize");
		cmds = commands_new();
                cmd = command_add_logless(cmds,
		    "cd %s && /usr/local/bin/memtest %s 1 --log", INSTALLER_TEMP, memtestsize);
                cmd = command_add(cmds,
		    "egrep -v '^Unable to malloc' %smemtest.log > %smemtest.log.new",
		    INSTALLER_TEMP, INSTALLER_TEMP);
                cmd = command_add(cmds, "mv %smemtest.log.new %smemtest.log",
		    INSTALLER_TEMP, INSTALLER_TEMP);
                cmd = command_add(cmds,
		    "egrep -v '^Allocated.*failed' %smemtest.log > %smemtest.log.new",
		    INSTALLER_TEMP, INSTALLER_TEMP);
                cmd = command_add(cmds, "mv %smemtest.log.new %smemtest.log",
		    INSTALLER_TEMP, INSTALLER_TEMP);
 		if (commands_execute(c, cmds)) {
			commands_free(cmds);
			view_memtest_log(c);
			cmds = commands_new();
			cmd = command_add(cmds, "rm -f %smemtest.log", INSTALLER_TEMP);
			commands_execute(c, cmds);
		} else {
			inform("Memory test could not be run.");
		}
		commands_free(cmds);
	}

        dfui_form_free(f);
        dfui_response_free(r);
}

void
view_memtest_log(struct dfui_connection *c)
{
        struct dfui_form *f;
        struct dfui_response *r;
        struct extbuf *error_log;

        error_log = extbuf_new(1024);
        extbuf_cat_file(error_log, INSTALLER_TEMP "memtest.log");

        f = dfui_form_create(
                "error_log",
                "Error Log",
                extbuf_buf(error_log),
                "",

                "h",    "role", "informative",
                "h",    "minimum_width", "72",
                "h",    "monospaced", "true",

                "a",    "ok", "OK", "", "",
                NULL
        );

        if (!dfui_be_present(c, f, &r)) abort_backend();

        dfui_form_free(f);
        dfui_response_free(r);

        extbuf_free(error_log);
}

void 
fn_show_dmesg(void)
{
	struct dfui_form *f;
	struct dfui_response *r;
	struct extbuf *e;

	e = extbuf_new(1024);
	extbuf_cat_file(e, DMESG_BOOT);

	f = dfui_form_create(
		"dmesg",
		"System Startup Messages (dmesg)",
		extbuf_buf(e),
		"",

		"h",	"role", "informative",
		"h",	"minimum_width", "72",
		"h",	"monospaced", "true",

		"a",	"ok", "OK", "", "",
		NULL
	);

	if (!dfui_be_present(c, f, &r)) abort_backend();

	dfui_form_free(f);
	dfui_response_free(r);

	extbuf_free(e);
}

void
fn_show_pciconf(void)
{
	struct dfui_form *f;
	struct dfui_response *r;
	struct extbuf *e;

	e = extbuf_new(1024);
	extbuf_cat_pipe(e, "pciconf -l -v");

	f = dfui_form_create(
		"pciconf",
		"PCI Devices",
		extbuf_buf(e),
		"",

		"h",	"role", "informative",
		"h",	"minimum_width", "72",
		"h",	"monospaced", "true",

		"a",	"ok", "OK", "", "",
		NULL
	);

	if (!dfui_be_present(c, f, &r)) abort_backend();

	dfui_form_free(f);
	dfui_response_free(r);

	extbuf_free(e);
}

void
fn_show_pnpinfo(void)
{
	struct dfui_form *f;
	struct dfui_response *r;
	struct extbuf *e;

	e = extbuf_new(1024);
	extbuf_cat_pipe(e, "pnpinfo");

	f = dfui_form_create(
		"pnpinfo",
		"ISI PnP Devices",
		extbuf_buf(e),
		"",

		"h",	"role", "informative",
		"h",	"minimum_width", "72",
		"h",	"monospaced", "true",

		"a",	"ok", "OK", "", "",
		NULL
	);

	if (!dfui_be_present(c, f, &r)) abort_backend();

	dfui_form_free(f);
	dfui_response_free(r);

	extbuf_free(e);
}

void
fn_show_atacontrol(void)
{
	struct dfui_form *f;
	struct dfui_response *r;
	struct extbuf *e;

	e = extbuf_new(1024);
	extbuf_cat_pipe(e, "atacontrol list");

	f = dfui_form_create(
		"atacontrol",
		"ATA Devices",
		extbuf_buf(e),
		"",

		"h",	"role", "informative",
		"h",	"minimum_width", "72",
		"h",	"monospaced", "true",

		"a",	"ok", "OK", "", "",
		NULL
	);

	if (!dfui_be_present(c, f, &r)) abort_backend();

	dfui_form_free(f);
	dfui_response_free(r);

	extbuf_free(e);
}

void
show_ifconfig(char *ifname)
{
	struct extbuf *e;

	e = extbuf_new(1024);
	extbuf_cat_pipe(e, "/sbin/ifconfig %s", ifname);
	inform(extbuf_buf(e));
	extbuf_free(e);
}
