/*
 *   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 <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <signal.h>

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>

#include <linux/fs.h>

#include <dialog.h>
#include <ndialog.h>
#include <basis/rpopen.h>

#include "partition.h"
#include "pathnames.h"
#include "util.h"
#include "builddisk.h"
#include "part.h"
#include "media.h"
#include "packages.h"


/*
 * mount_disk() attempts to mount the cd rom, zip disk, or dos partition
 * that the installation software is on.
 */
int
mount_disk()
{
    char msg[300];
    int rc;
    struct stat st;

    snprintf(msg, sizeof msg, "Checking %s for installation kit",
				MEDIANAME(mdesc));
    dialog_msgbox(INSTALLER, msg, -1, 50, 0);
#if ACTUAL
    /* mount the install media so we can access the TOOLS directory there
     */
    do {
	if ((rc = checkfor(MEDIA(mdesc), MEDIANAME(mdesc), 0)) != 0) {
	    snprintf(msg, sizeof msg,
		    "Please insert installation disk into the %s drive",
		    MEDIANAME(mdesc));
	    rc = dialog_yesno_extended(INSTALLER,
			   msg, -1, -1, "OK", "Abort");
	    if (rc != 0)
		return 0;
	    rc = 1;
	}
	else if ((rc = MOUNT(mdesc)(MEDIA(mdesc), SOURCE,
				    MEDIAFS(mdesc),
				    MS_MGC_VAL|MS_RDONLY,
				    MEDIAOPTS(mdesc))) != 0) {
	    snprintf(msg, sizeof msg, 
		    "Please insert installation disk into the %s drive\n"
		    "(%s: %s)", MEDIANAME(mdesc), MEDIA(mdesc), strerror(errno));
	    rc = dialog_yesno_extended(INSTALLER, msg, -1, -1, "OK", "Abort");
	    if (rc != 0)
		return 0;
	    rc = 1;
	}
	else if ((rc = stat(PATH_PKGS PACKAGES, &st)) != 0) {
	    UMOUNT(mdesc)(SOURCE);

	    eject_media(mdesc);
	    snprintf(msg, sizeof msg, "Cannot find distribution tarball; "
			 "is this the right %s?", MEDIANAME(mdesc));
	    dialog_notify(msg);
	}
	else
	    rc = readpackagelist(PATH_PKGS PACKAGES);
    } while (rc != 0);
    return 1;
#else
    return (readpackagelist(PACKAGES) == 0) ? 1 : 0;
#endif
} /* mount_disk */


