#!/bin/sh
# Automatically setup for murasaki rc-script
#                            shuu@dotaster.com
#
# $Id: auto_setup,v 1.4 2003/11/13 06:55:23 shuu Exp shuu $
#
# based on rcusbmgr by Olaf Hering
#

########################### You can modify settings
###################### generic setting
MODPROBE=/sbin/modprobe
CONFDIR=/etc/murasaki
###################### USB setting
MOUSEDEV="mousedev hid"
UHCI_1ST=usb-uhci			# compatible for 2.4
UHCI_2ND=uhci				# compatible for 2.4
OHCI=usb-ohci				# compatible for 2.4
EHCI=ehci-hcd				# USB 2.0
###################### PCMCIA setting (overwriting if the value exists)
PCIC=""
PCIC_OPTS=""
CORE_OPTS=""
########################### You can modify settings

########################### Functions
###################### generic functions
find_module()
{
	if [ $# -eq 0 ];then
		return 1;
	fi
	if $MODPROBE -l | grep -E "/$1\.(o|ko)$" > /dev/null;then
		return 0
	fi
	return 1
}

find_echo()
{
	for m in $*
	do
		if find_module $m ;then
			echo $m
		fi
	done
}

check_lspci()
{
	for dir in /sbin /usr/sbin /usr/local/sbin
	do
		if [ -x ${dir}/lspci ];then
			have_lspci=1
			LSPCI=${dir}/lspci
			break
		fi
	done
}

# $1: agent
check_agent()
{
	if grep "^$1:.*[oO][nN]" ${CONFDIR}/murasaki.conf >/dev/null;then
		return 0;
	fi
	return 1;
}

###################### USB functions
usb_check_host_uhci()
{
	if [ $uhci_flag -ne 0 ];then
		return
	fi
	$METHOD | grep -E '(USB.*(Intel|VIA)|UHCI)' >/dev/null
	if [ $? -eq 0 ];then
		for mod in $UHCI_MOD
		do
			if find_module ${mod} ;then
				HOST_1="$HOST_1 ${mod}"
				uhci_flag=1
				break
			fi
		done
	fi
}

usb_check_host_ehci()
{
	if [ $ehci_flag -ne 0 ];then
		return
	fi
	$METHOD | grep -E '(USB\ 2.0|EHCI)' >/dev/null
	if [ $? -eq 0 ];then
		if find_module ${EHCI} ;then
			HOST_1="$HOST_1 ${EHCI}"
			ehci_flag=1
		fi
	fi
}

usb_check_host_ohci()
{
	if [ $ohci_flag -ne 0 ];then
		return
	fi
	$METHOD | grep -E '(USB.*(OPTi|SiS|NEC)|OHCI)' >/dev/null
	if [ $? -eq 0 ];then
		for mod in $OHCI_MOD
		do
			if find_module ${mod} ;then
				HOST_1="$HOST_1 ${mod}"
				ohci_flag=1
			fi
		done
	fi
}

usb_check_host_pass1()
{
	usb_check_host_uhci
	usb_check_host_ohci
	usb_check_host_ehci
}

usb_check_host_pass2()
{
	if [ $have_lspci -eq 0 ];then
		return
	fi
	if [ $ehci_flag -eq 0 ];then
		${LSPCI} -n | grep -E '(1033:00e0|1039:7002|10b9:5239|1106:3104|8086:24cd|05ab:0060)' > /dev/null
		if [ $? -eq 0 ] ;then
			if find_module ${EHCI} ;then
				HOST_2=${EHCI}
				ehci_flag=1
			fi
		fi
	fi
}

###################### PCMCIA functions

pcmcia_getenv()
{
	for f in /etc/sysconfig/pcmcia /etc/pcmcia.conf
	do
		if [ -f $f ];then
			. $f

			if [ -z "$PCMCIA" -o "$PCMCIA" != "yes" ];then
				return 1;
			else
				return 0;
			fi
		fi
	done
}

########################### Exec
###################### generic exec

have_lspci=0

check_lspci

if [ $have_lspci -ne 0 ];then
	METHOD="$LSPCI -v"
else
	[ -f /proc/cpuinfo ] || mount -n -t proc proc /proc
	if [ -f /proc/pci ];then
		METHOD="cat /proc/pci"
	else
		echo "PCI devices are not found"
		exit 1
	fi
fi

###################### USB exec

UHCI_MOD="uhci-hcd $UHCI_1ST $UHCI_2ND"
OHCI_MOD="ohci-hcd $OHCI"
EHCI_MOD="$EHCI"

uhci_flag=0
ohci_flag=0
ehci_flag=0

if check_agent usb;then
	usb_check_host_pass1
	usb_check_host_pass2

	for mod in ${HOST_1} ${HOST_2}
	do
		echo ${mod}
	done

	if [ -f /usr/X11R6/bin/xterm ];then
		find_echo ${MOUSEDEV}
	fi

fi

###################### PCMCIA exec

if check_agent pcmcia_socket;then
	if ! grep pcmcia /proc/devices >/dev/null && [ -z "$PCIC" ];then
		pcmcia_getenv
	fi
	if find_module pcmcia_core;then
		echo pcmcia_core ${CORE_OPTS}
	fi
	if find_module ${PCIC};then
		echo ${PCIC} ${PCIC_OPTS}
	fi
	find_echo ds
fi

