/*
 *   Copyright (c) 1996-2000 David Parsons. All rights reserved.
 *   
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. 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.
 *  3. All advertising materials mentioning features or use of this
 *     software must display the following acknowledgement:
 *     
 *   This product includes software developed by David Parsons
 *   (orc@pell.chi.il.us)
 *
 *  4. My name may not be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *     
 *  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``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 DAVID
 *  PARSONS 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.
 */

#include <dialog.h>
#include <ndialog.h>
#include "media.h"


extern int ethsetup(void*);
extern int filesetup(void*);


/*
 * read_toc_from_media() actually opens the media after any user configuration
 *                       (ftp site, filename, etc) is entered.
 */
static int
read_toc_from_media(void *m)
{
    char msg[300];
    int rc;

    while (mOpen(m) != 0) {
	if (mType(m) == M_ZIP || mType(m) == M_CDROM) {
	    snprintf(msg, sizeof msg,
			    "The %s doesn't contain a mastodon distribution.\n"
			    "Insert another disk and press [OK]%s.", mName(m),
			(NrMedia>1) ? ", or [CANCEL] to install\n"
				      "from a different medium"
				    : "");
	    rc = dialog_yesno_extended(INSTALLER, msg, -1, -1, "OK", "CANCEL");
	    if (rc != MENU_OK)
		return rc;
	}
	else {
	    snprintf(msg, sizeof msg,
		    "I can't find a Mastodon distribution here");
	    dialog_mesgbox(INSTALLER, msg, -1, -1);
	    return MENU_CANCEL;
	}
    }
    return MENU_OK;
} /* read_toc_from_media */


/*
 * check_install_media() checks the installation media to make sure that
 * it actually contains a Mastodon install image.
 */
int
check_install_media(void *m)
{
    char msg[300];
    int rc;

    do {
	switch (mType(m)) {
	case M_NET:
		if ( (rc = ethsetup(m)) == MENU_OK)
		    if (read_toc_from_media(m) == MENU_OK)
			return MENU_OK;
		break;
	case M_DOS:
	case M_FILE:
		if ( (rc = filesetup(m)) == MENU_OK)
		    if (read_toc_from_media(m) == MENU_OK)
			return MENU_OK;
		break;
	default:
		snprintf(msg, sizeof msg, "Checking %s for installation kit",
				    mName(mdesc));
		dialog_msgbox(INSTALLER, msg, -1, 50, 0);
		return read_toc_from_media(m);
	}
    } while (rc == MENU_OK);
} /* check_install_media */
