#
# By Chih-Wei Huang <cwhuang@linux.org.tw>
# Last updated 2012/07/10
#
# License: GNU Public License
# We explicitely grant the right to use the scripts
# with Android-x86 project.
#

# An auto detect function provided by kinneko
auto_detect()
{
	tmp=/tmp/dev2mod
	echo 'dev2mod() { while read dev; do case $dev in' > $tmp
	sort -r /lib/modules/`uname -r`/modules.alias | \
		sed -n 's/^alias  *\([^ ]*\)  *\(.*\)/\1)modprobe \2;;/p' >> $tmp
	echo 'esac; done; }' >> $tmp
	source $tmp
	cat /sys/bus/*/devices/*/modalias | dev2mod
	cat /sys/devices/virtual/wmi/*/modalias | dev2mod
}

load_uvesafb()
{
	[ -x /sbin/v86d ] || ln -s ../android/sbin/v86d /sbin
	[ -c /dev/fb0 ] || modprobe uvesafb mode_option=${UVESA_MODE:-800x600}-16 ${UVESA_OPTION:-mtrr=3 scroll=redraw}
}

find_network_dev_name()
{
	INITRC=init.x86.rc
	DHCPCD="/system/bin/dhcpcd -o domain_name_servers"
	rmline=`grep -n "#REMOVE FROM HERE" $INITRC | cut -d':' -f1`
	rmline=`expr $rmline + 1`
	sed -i -e "$rmline,\$d" $INITRC
	for netdev in `ls /sys/class/net`; do
		case "$netdev" in
			lo|ip6tnl*|sit*|tun*|eth0|wlan0|wmaster*)
				;;
			*)
				echo -e "\nservice dhcpcd_$netdev $DHCPCD -ABDKL\n    class main\n    disabled\n    oneshot\n" >> $INITRC
				echo -e "service iprenew_$netdev $DHCPCD -n\n    class main\n    disabled\n    oneshot" >> $INITRC
				;;
		esac
	done
}

load_modules()
{
	if [ -n "$FOUND" ]; then
		[ -n "$PREDEV" ] && modprobe $PREDEV
		[ -n "$FB0DEV" -a -z "$UVESA_MODE" ] && modprobe $FB0DEV
		[ -n "$LANDEV" ] && modprobe $LANDEV
		[ -n "$WIFDEV" ] && modprobe $WIFDEV
		[ -n "$SNDDEV" ] && modprobe $SNDDEV
		[ -n "$CAMDEV" ] && modprobe $CAMDEV
	else
		auto_detect
	fi

	# 3G modules
	EXTMOD="$EXTMOD pppoe ppp-async ppp-deflate bsd-comp"
	for m in $EXTMOD; do
		modprobe $m
	done

	load_uvesafb
	lsusb -v | awk ' /Class:.E0/ { printf("%s      0660   bluetooth bluetooth\n", $9) } ' >> ueventd.android_x86.rc
}
