#!/bin/sh
# Automatically setup for murasaki rc-script
#                            shuu@dotaster.com
#
# $Id: auto_setup,v 1.3 2003/09/28 07:59:17 shuu Exp shuu $
#
# based on rcusbmgr by Olaf Hering
#

########################### You can modify settings
MOUSEDEV="mousedev hid"
MODPROBE=/sbin/modprobe
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
########################### You can modify settings

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
have_lspci=0

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

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
}

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
}

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
}

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
}

check_host_pass1()
{
	check_host_uhci
	check_host_ohci
	check_host_ehci
}

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
}

########################### Exec

if [ -f /usr/X11R6/bin/xterm ];then
	MODULE=$MOUSEDEV
fi

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

check_host_pass1
check_host_pass2

for mod in $MODULE
do
	if find_module ${mod} ;then
		echo ${mod}
	fi
done

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