/*
 *   Copyright (c) 1999 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.
 */
/*
 * start_pcmcia() attempts to start (a subset of) pcmcia card services
 */
#include <stdio.h>
#include <string.h>

#include "util.h"

/*
 * start_pcmcia() starts up pcmcia card services.
 *                it returns 0 if it doesn't detect pcmcia
 *                           1 if it detected pcmcia, and started the services
 *                          -1 if an error happened
 */
#define PRE "/lib/modules/pcmcia/"
int
start_pcmcia()
{
    FILE *probe;
    char bfr[20];
    char tcic[120];

    if (getpid() != 1)
	return 0;

    if ((probe = popen("/sbin/probe -m", "r")) == 0)
	return -1;

    memset(bfr, 0, sizeof bfr);
    fgets(bfr, sizeof bfr, probe);
    pclose(probe);

    strtok(bfr, "\n");
    if (strlen(bfr) > 0) {	/* we have pcmcia card services */
	run(0, "/sbin/insmod", "insmod", PRE "pcmcia_core.o", 0L);
	snprintf(tcic, sizeof tcic, PRE "%s.o", bfr);
	run(0, "/sbin/insmod", "insmod", tcic, 0L);
	run(0, "/sbin/insmod", "insmod", PRE "ds.o", 0L);
	run(0, "/sbin/cardmgr", "cardmgr", "-Em", "/lib/modules", 0L);
	dialog_msgbox(0, "Loading PCMCIA card services", -1, -1, 0);
	sleep(4);
    }
    /* and that should be it... */
    return 1;
} /* start_pcmcia */
