#!/bin/sh
#
# etc/install-etc 1.5 2000/06/07 22:11:52 (David Hinds)
#
# This handles updating the PCMCIA startup and device configuration
# scripts.  I used to do this in the Makefile, but it is really much
# more of a shell scripty thing.

# Get configuration settings
. ../config.out

PROBE=../cardmgr/probe
ETC=$PREFIX/etc/pcmcia

install_clients ()
{
    echo "-> Updating client scripts in $ETC"
    mkdir -p $ETC
    CONF=*.conf
    if [ "$CONF" != "*.conf" ] ; then
	cp *.conf $ETC
    fi
    for f in *.opts ; do
	b=`basename $f .opts`
	cmp -s $b $ETC/$b && continue
	[ -r $ETC/$b ] && mv $ETC/$b $ETC/$b.O
	cp $b $ETC/$b
	[ -r $ETC/$f ] || cp $f $ETC/$f
    done
    for f in shared ; do
	cmp -s $f $ETC/$f && continue
	[ -r $ETC/$f ] && mv $ETC/$f $ETC/$f.O
	cp $f $ETC/$f
    done
}

install_sysv ()
{
    SYSV=$PREFIX$RC_DIR/init.d/pcmcia

     if [ -d /etc/sysconfig ] ; then
	mkdir -p $PREFIX/etc/sysconfig
	CFG=$PREFIX/etc/sysconfig/pcmcia
	if [ -f $CFG ] ; then . $CFG ; fi
	if [ "$PCMCIA" != "yes" ] ; then
	    echo "-> Creating PCMCIA options file $CFG"
	    echo PCMCIA=yes > $CFG
	    PCIC=i82365
	    if [ -x $PROBE ] ; then PCIC=`$PROBE -m` ; fi
	    echo "PCIC=$PCIC" >> $CFG
	    echo "PCIC_OPTS=" >> $CFG
	    echo "CORE_OPTS=" >> $CFG
	    echo "CARDMGR_OPTS=" >> $CFG
	fi
    fi

    if ! cmp -s rc.pcmcia $SYSV ; then
	if [ -e $SYSV ] ; then SYSV=$SYSV.N ; fi
	echo "-> Installing PCMCIA startup script as $SYSV"
	mkdir -p $PREFIX$RC_DIR/init.d
	cp rc.pcmcia $SYSV
	chmod +x $SYSV
    fi

    for RUN in 0 6 ; do
	RC=$PREFIX$RC_DIR/rc$RUN.d
	mkdir -p $RC
	if [ ! -r $RC/*pcmcia ] ; then
	    ln -vsf ../init.d/pcmcia $RC/K52pcmcia
	fi
    done
    for RUN in 2 3 5 ; do
	RC=$PREFIX$RC_DIR/rc$RUN.d
	mkdir -p $RC
	if [ ! -r $RC/*pcmcia ] ; then
	    ln -vsf ../init.d/pcmcia $RC/S45pcmcia
	fi
    done
}

install_bsd ()
{
    BSD=$PREFIX/etc/rc.d/rc.pcmcia
    if [ -e $BSD ] ; then BSD=$BSD.N ; fi

    echo "-> Installing PCMCIA startup script as $BSD"
    mkdir -p $PREFIX/etc/rc.d
    if [ -x $PROBE ] ; then
	PCIC=`$PROBE -m`
	sed -e "s/=i82365/=$PCIC/" rc.pcmcia > $BSD
    else
	cp rc.pcmcia $BSD
    fi
    chmod +x $BSD
}

install_depmod ()
{
    CONF=$PREFIX/etc/modules.conf
    if [ ! -r $CONF -a -r $PREFIX/etc/conf.modules ] ; then
	CONF=$PREFIX/etc/conf.modules
    fi
    if ! modprobe -c | grep -q 'path\[pcmcia\]' ; then
	echo "-> Updating $CONF"
	grep -qs ^path $CONF || grep -qs ^keep $CONF ||
	    echo keep >> $CONF
	modprobe -c | sed -ne '/path/s/\[net\]/\[pcmcia\]/p' >> $CONF
    fi
    if [ -x /sbin/depmod -a "$PREFIX" = "" -a "$CONF_SRC" = 1 ] ; then
	echo "-> Running depmod..."
	depmod -a
    else
	echo "-> *NOT* running depmod.  Run depmod by hand if necessary."
    fi
}

if [ "$SYSV_INIT" = "y" ] ; then
    install_sysv
else
    install_bsd
fi

install_clients
install_depmod

# Directory to receive cardmgr's 'stab' socket status file
mkdir -p $PREFIX/var/lib/pcmcia
