# -*-perl-*-

# stuff to handle pcmcia devices

$pcic = "";
$pcmcia_opts = "";
$pcmcia_3c589 = "";

sub pcmcia_init {

    &rhs_infobox("PCMCIA",
<<EOM
>
Probing for PCMCIA devices.
>
EOM
		 , 70);

    # This is just to look good...
    sleep(1);

    $pcic = "";
    open(SAVEERR, ">&STDERR");
    open(STDERR, ">/dev/null");
    open(PROC, "probe|") || &newdie("PCMCIA probe failed!");
    while (<PROC>) {
	/Intel PCIC probe/ && (/not found/ || ($pcic = "i82365"));
	/Databook TCIC-2 probe/ && (/not found/ || ($pcic = "tcic"));
    }
    close PROC;
    open(STDERR, ">&SAVEERR");

    if ($pcic eq "") {
	return 0;
    }

    if (!rhs_yesno("PCMCIA",
<<EOM
>
You appear to have $pcic PCMCIA support.
>
Is this true?
>
EOM
		   ,  70)) {
	$pcic = "";
	return 0;
    }

    &mount_bootdisk();

    if (&rhs_yesno("3COM 3c589",
<<EOM
>
If you have a 3COM 3c589 PCMCIA Ethernet card,
are you using 10base2 (also known as BNC, or thin
net, or coax)?
>
 (If you are not using a 3c589, just press enter)
>
EOM
		   , 70)) {
	$pcmcia_3c589 = "  module \"3c589_cs\" opts \"if_port=3\"";
    } else {
	$pcmcia_3c589 = "  module \"3c589_cs\" # opts \"if_port=3\"";
    }

    open(FD, "/etc/pcmcia/config.in");
    open(OF, ">/etc/pcmcia/config");
    while (<FD>) {
#	if (/^PCIC=/) {
#	    $_ = "PCIC=$pcic\n";
#	}
	if (/module \"3c589_cs/) {
	    $_ = "$pcmcia_3c589\n";
	}
	print OF;
    }
    close FD;
    close OF;

    $pcmcia_opts = "";

    &rhs_infobox("PCMCIA",
<<EOM
>
Loading PCMCIA core module.
>
EOM
		 , 70);
    &invoke("insmod /lib/modules/current/pcmcia/pcmcia_core.o");
    &rhs_infobox("PCMCIA",
<<EOM
>
Loading PCMCIA $pcic module.
>
EOM
		 , 70);
    &invoke("insmod /lib/modules/current/pcmcia/$pcic.o $pcmcia_opts");
    &rhs_infobox("PCMCIA",
<<EOM
>
Loading PCMCIA ds module.
>
EOM
		 , 70);
    &invoke("insmod /lib/modules/current/pcmcia/ds.o");
    &rhs_infobox("PCMCIA",
<<EOM
>
Running PCMCIA card manager.
>
EOM
		 , 70);
    &invoke("cardmgr");

    return 0;
}

sub finish_pcmcia {

    local ( $ans );
    
    if (! -f "$fsmount/etc/sysconfig/pcmcia") {
	return 1;
    }

    if ($pcic) {
	$ans = "yes";
    } else {
	$ans = "no";
    }

    open(FD, ">$fsmount/etc/sysconfig/pcmcia");
    print FD <<EOM
PCMCIA=$ans
PCIC=$pcic
OPTS=$pcmcia_opts
EOM
    ;
    close FD;

    open(IFD, "$fsmount/etc/sysconfig/pcmcia-scripts/config");
    open(FD, ">$fsmount/etc/sysconfig/pcmcia-scripts/config.new");
    while (<IFD>) {
	if (/module \"3c589_cs/) {
	    $_ = $pcmcia_3c589;
	}
	print FD;
    }
    close IFD;
    close FD;

    unlink("$fsmount/etc/sysconfig/pcmcia-scripts/config");
    link("$fsmount/etc/sysconfig/pcmcia-scripts/config.new",
	 "$fsmount/etc/sysconfig/pcmcia-scripts/config");
    unlink("$fsmount/etc/sysconfig/pcmcia-scripts/config.new");

    return 1;
}

#######
1;
