#!/bin/sh
#
# AUTOSETUP-MOL -- automatically setup MOL system
# Copyright (C) 2007 Keicho Kondo <dgel@users.sourceforge.jp>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# ===========================================================
# Set PATH, Load libmol, Mount procfs, and Load boot options
# ===========================================================

# get variables and functions
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/exec:."
. /exec/libmol

# mount procfs
mkdir -p /proc
mount -t proc proc /proc

# setup DEBUGCMD variable. If debug boot option is present, call debug()
# function several times during script's execution
checkbootparam "debug" && DEBUGCMD="debug" || DEBUGCMD=""

$DEBUGCMD

# device on/off parameter
checkbootparam "noscsi" && { NOSCSI="yes"; }
checkbootparam "noraid" && { NORAID="yes"; }
# for all devices setup
checkbootparam "noaudio" && { NOAUDIO="yes"; }
checkbootparam "nonet" && { NONETWORK="yes"; }
checkbootparam "novideo" && { NOVIDEO="yes"; }
# network parameter
checkbootparam "nodhcp" && { NODHCP="yes"; }
checkbootparam "ipaddr" && { IPADDR="$(getbootparam ipaddr 2>/dev/null)"; }
checkbootparam "netmask" && { NETMASK="$(getbootparam netmask 2>/dev/null)"; }
checkbootparam "gateway" && { GATEWAY="$(getbootparam gateway 2>/dev/null)"; }
checkbootparam "netdev" && { NETDEV="$(getbootparam netdev 2>/dev/null)"; }

$DEBUGCMD

# ===========================================================
# Setup all devices
# ===========================================================

## Make device files
echo "${CRE}${BLUE}Creating Device Files...${NORMAL}"
mount -t sysfs none /sys
mount -o mode=0755 -t tmpfs none /dev
mknod /dev/console c 5 1
mknod /dev/null c 1 3
mknod /dev/zero c 1 5
mknod /dev/mem c 1 1
mkdir /dev/pts
mkdir /dev/shm
#/sbin/udevstart
/sbin/start_udev >/dev/null 2>&1
echo -n "/sbin/hotplug" > /proc/sys/kernel/hotplug
umount /sys

## Run hwsetup-mol
# check /etc/sysconfig/mol directory
mount -t usbfs none /proc/bus/usb
if [ -d /etc/sysconfig/mol ]; then
	for f in $(echo /etc/sysconfig/mol/*); do
		for conf in floppy mol mouse netcard sound xserver; do
			[ "${f#/etc/sysconfig/mol/}" == "$conf" ] && mv -f /etc/sysconfig/mol/${f} /etc/sysconfig/mol/${f}.backup
		done
	done
else
	mkdir -p /etc/sysconfig/mol
fi

# check boot parameter
[ -n "${NOAUDIO}" ] && { HWSETUP_OPT="-a ${HWSETUP_OPT}"; }
[ -n "${NOSCSI}" ] && { HWSETUP_OPT="-s ${HWSETUP_OPT}"; }
[ -n "${NONETWORK}" ] && { HWSETUP_OPT="-net ${HWSETUP_OPT}"; }
[ -n "${NOVIDEO}" ] && { HWSETUP_OPT="-video ${HWSETUP_OPT}"; }
[ -n "${NORAID}" ] && { HWSETUP_OPT="-raid ${HWSETUP_OPT}"; }

/exec/hwsetup-mol -p ${HWSETUP_OPT}
[ -f /etc/sysconfig/mol/mol ] && . /etc/sysconfig/mol/mol
umount /proc/bus/usb

$DEBUGCMD

## XkbLayout and Videocard
if [ -z "${NOVIDEO}" -a -f /etc/X11/xorg.conf ]; then
	case $(grep -v "^#" /etc/X11/xorg.conf | awk '/XkbLayout/{print $3}') in
		*jp*) echo "XKEYBOARD=\"jp\"" >>/etc/sysconfig/mol/keyboard ;;
	esac
	mv /etc/X11/xorg.conf /etc/X11/xorg.conf.backup
	/exec/xsetup-mol
else
	echo " ${BLUE}Skipping X Configuration.${NORMAL}"
fi

$DEBUGCMD

## Soundcard
if [ -z "${NOAUDIO}" ] && [ -n "${SOUND_FULLNAME}" -a -n "${SOUND_DRIVER}" ]; then
	echo " ${GREEN}Soundcard is ${YELLOW}${SOUND_FULLNAME}${GREEN} driver=${MAGENTA}${SOUND_DRIVER}${NORMAL}"
else
	echo " ${BLUE}Skipping Soundcard detection.${NORMAL}"
fi

$DEBUGCMD

## NIC
if [ -z "${NONETWORK}" ] && [ -n "${NETCARD_FULLNAME}" -a -n "${NETCARD_DRIVER}" ]; then
	NETCARD_DEVICE="$(awk -F: '/eth.:|tr.:/{print $1}' /proc/net/dev | awk -F' ' '{print $1}' 2>/dev/null)"
	echo " ${GREEN}Netcard is ${YELLOW}${NETCARD_FULLNAME}${GREEN} at ${MAGENTA}${NETCARD_DEVICE} ${GREEN}driver=${MAGENTA}${NETCARD_DRIVER}${NORMAL}"

	# for Static IP address
	if [ -n "${IPADDR}" ]; then
		[ -n "${NETMASK}" ] || NETMASK="255.255.255.0"
		[ -n "${GATEWAY}" ] || GATEWAY="${IPADDR%.*}.1"
		[ -n "${NETDEV}" ]  || NETDEV="eth0"
		STATIC_NETDEV="${NETDEV}"
		echo " ${GREEN}Network device ${MAGENTA}${STATIC_NETDEV} ${GREEN}detected, IP is ${YELLOW}${IPADDR}${GREEN}.${NORMAL}"
#-----------------------------------------------------------------
cat >/etc/sysconfig/mol/ifcfg-${STATIC_NETDEV} <<EOF
DEVICE=$STATIC_NETDEV
ONBOOT=yes
BOOTPROTO=static
IPADDR=$IPADDR
NETMASK=$NETMASK
GATEWAY=$GATEWAY
EOF
#-----------------------------------------------------------------
	fi

	# for DHCP
	if [ -n "${NODHCP}" ]; then
		echo " ${BLUE}Skipping DHCP broadcast/network detection as requested on boot commandline.${NORMAL}"
	else
		for DHCP_NETDEV in ${NETCARD_DEVICE}; do
			[ "${STATIC_NETDEV}" = "${DHCP_NETDEV}" ] && continue
			echo " ${GREEN}Network device ${MAGENTA}${DHCP_NETDEV} ${GREEN}detected, DHCP broadcasting for IP.${NORMAL}"
#---------------------------------------------------------------
cat >/etc/sysconfig/mol/ifcfg-${DHCP_NETDEV} <<EOF
DEVICE=$DHCP_NETDEV
ONBOOT=yes
BOOTPROTO=dhcp
EOF
#---------------------------------------------------------------
		done
	fi
else
	echo " ${BLUE}Skipping Netcard detection.${NORMAL}"
fi

$DEBUGCMD

## Mouse
if [ -n "${MOUSE_FULLNAME}" -o -n "${MOUSE_DEVICE}" ]; then
	echo " ${GREEN}Mouse is ${YELLOW}${MOUSE_FULLNAME}${GREEN} at ${MAGENTA}${MOUSE_DEVICE}${NORMAL}"
fi

$DEBUGCMD

# ===========================================================
# Pre-init
# ===========================================================

## Enable kernel messages
echo "6" >/proc/sys/kernel/printk

## Make utmp and wtmp
[ -e /var/run/utmp ] && rm -f /var/run/utmp
:> /var/run/utmp
[ -e /var/log/wtmp ] && rm -f /var/log/wtmp
:> /var/log/wtmp

## Execute final script
if [ -x "/exec/final.sh" ]; then
	echo "${BLUE}Executing Final Script${NORMAL}"
	/exec/final.sh
fi

## Tell kernel where the real modprobe lives
echo "/sbin/modprobe" >/proc/sys/kernel/modprobe

## Change root device from /dev/fd0 to /dev/ram0
#echo "0x100" > /proc/sys/kernel/real-root-dev

## Unmount procfs
umount /proc

$DEBUGCMD

echo "${CRE}${BLUE}Starting init process.${NORMAL}"
## give control to the init process
exec /exec/init "$@"


