/*
 * 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. 
 */

/*
 * main.c
 * Main program for installer backend.
 * $Id: main.c,v 1.3 2004/07/11 07:22:50 cpressey Exp $
 */

#include <sys/param.h>
#include <sys/sysctl.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

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

#include "flow.h"
#include "pathnames.h"

void usage(char **);

char dfly_root[256];
int do_reboot = 0;

int
main(int argc, char **argv)
{
	int opt;
	char *rendezvous = NULL;
	int transport = DFUI_TRANSPORT_TCP;

	/*
	 * XXX set transport from env var
	 */

	strlcpy(dfly_root, DEFAULT_DFLY_ROOT, sizeof(dfly_root));

#ifdef DEBUG
	dfui_debug_file = fopen("/tmp/dfuibe_installer_debug.log", "w");
#endif

	/*
	 * Get command-line arguments.
	 */
	while ((opt = getopt(argc, argv, "o:r:t:")) != -1) {
		switch(opt) {
		case 'o':
			strlcpy(dfly_root, optarg, sizeof(dfly_root));
			break;
		case 'r':
			rendezvous = dfui_strdup(optarg);
			break;
		case 't':
			if (strcmp(optarg, "npipe") == 0) {
				transport = DFUI_TRANSPORT_NPIPE;
			} else if (strcmp(optarg, "caps") == 0) {
				transport = DFUI_TRANSPORT_CAPS;
			} else if (strcmp(optarg, "tcp") == 0) {
				transport = DFUI_TRANSPORT_TCP;
			} else {
				usage(argv);
			}
			break;
		case '?':
		default:
			usage(argv);
		}
	}
	argc -= optind;
	argv += optind;

	if (rendezvous == NULL) {
		if (transport == DFUI_TRANSPORT_TCP) {
			rendezvous = dfui_strdup("9999");
		} else {
			rendezvous = strdup("test");
		}
	}

	flow(transport, rendezvous);
	free(rendezvous);

	if (do_reboot)
		exit(5);
	else
		exit(0);
}

void
usage(char **argv)
{
	fprintf(stderr, "Usage: %s [-r rendezvous] [-t caps|npipe|tcp]\n", argv[0]);
	exit(1);
}
