#!/bin/bash
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
#
# To do something instead of anaconda

# Reread boot command line; echo last parameter's argument or return false.

# same for strings
stringinstring(){
case "$2" in *$1*) return 0;; esac
return 1
}

getbootparam(){
stringinstring " $1=" "$cmdline" || return 1
result="${cmdline##*$1=}"
result="${result%%[     ]*}"
echo "$result"
return 0
}

# Check boot commandline for specified option
checkbootparam(){
stringinstring " $1" "$cmdline"
return "$?"
}

# Mount /proc and /sys (done here so volume labels can work with fsck)
mount -n -t proc /proc /proc
[ -d /proc/bus/usb ] && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
mount -n -t sysfs /sys /sys >/dev/null 2>&1

. /etc/init.d/functions

# Only read this once.
cmdline=$(cat /proc/cmdline)

LANGUAGE="$(getbootparam lang 2>/dev/null)"
[ -n "$LANGUAGE" ] || LANGUAGE="ko"

case "$LANGUAGE" in
ko)
# Korean version
LANG="ko_KR.UTF-8"
SUPPORTED="ko_KR.UTF-8:ko_KR:ko"
SYSFONT="latarcyrheb-sun16"
KEYBOARDTYPE="pc"
KEYTABLE="us"
CHARSET="iso8859-1"
ZONE="Asia/Seoul"
UTC=false
ARC=false
;;
*)
LANG="C"
SUPPORTED="en_US.UTF-8:en_US:en"
SYSFONT="latarcyrheb-sun16"
KEYBOARDTYPE="pc"
KEYTABLE="us"
CHARSET="iso8859-1"
ZONE="America/New_York"
UTC=false
ARC=false
;;
esac

# need to check
mount -n -o remount,rw /

KTZ="$(getbootparam tz 2>/dev/null)"
[ -f "/usr/share/zoneinfo/$KTZ" ] && ZONE="$KTZ"
rm -f /etc/localtime
cp "/usr/share/zoneinfo/$ZONE" /etc/localtime

rm -f /etc/sysconfig/i18n /etc/sysconfig/keyboard /etc/sysconfig/clock
rm -f /etc/X11/xorg.conf /etc/sysconfig/hwconf /etc/sysconfig/mouse
rm -f /etc/sysconfig/BOOYO /etc/sysconfig/netcard

# Standard variables/files
echo "LANG=\"$LANG\""                  > /etc/sysconfig/i18n
echo "SUPPORTED=\"$SUPPORTED\""       >> /etc/sysconfig/i18n
echo "SYSFONT=\"$SYSFONT\""           >> /etc/sysconfig/i18n

# Default Keyboard layout for console and X
echo "KEYBOARDTYPE=\"$KEYBOARDTYPE\""  > /etc/sysconfig/keyboard
echo "KEYTABLE=\"$KEYTABLE\""         >> /etc/sysconfig/keyboard

# Default clock
echo "ZONE=\"$ZONE\""                  > /etc/sysconfig/clock
echo "UTC=\"$UTC\""                   >> /etc/sysconfig/clock
echo "ARC=\"$ARC\""                   >> /etc/sysconfig/clock


if [ "$HOSTTYPE" != "s390" -a "$HOSTTYPE" != "s390x" ]; then
  last=0
  for i in `LC_ALL=C grep '^[0-9].*respawn:/sbin/mingetty' /etc/inittab | sed 's/^.* tty\([0-9][0-9]*\).*/\1/g'`; do
        > /dev/tty$i
        last=$i
  done
  if [ $last -gt 0 ]; then
       > /dev/tty$((last+1))
       > /dev/tty$((last+2))
  fi
fi

if [ "$CONSOLETYPE" = "vt" -a -x /sbin/setsysfont ]; then
   echo -n "Setting default font ($SYSFONT): "
   /sbin/setsysfont
   if [ $? -eq 0 ]; then
      success
   else
      failure
   fi
   echo ; echo
fi

# Print a text banner.
echo -en $"\t\tWelcome to "
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;31m"
echo -en "Fedora Core 4"
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m"
echo " Live CD 0.1"

if [ "$PROMPT" != "no" ]; then
 echo -en $"\t\tPress 'I' to enter interactive startup."
 echo
fi

# Fix console loglevel
if [ -n "$LOGLEVEL" ]; then
        /bin/dmesg -n $LOGLEVEL
fi

if [ -f /etc/udev/udev.conf ];then
	. /etc/udev/udev.conf
fi

[ -x /sbin/start_udev ] && /sbin/start_udev

# First: PCMCIA Check/Setup
# This needs to be done before other modules are being loaded by hwsetup

if checkbootparam "nopcmcia"; then
echo " ${BLUE}Skipping PCMCIA detection as requested on boot commandline.${NORMAL}"
else
modprobe pcmcia_core >/dev/null 2>&1
# Try Cardbus or normal PCMCIA socket drivers
modprobe yenta_socket >/dev/null 2>&1 || modprobe i82365 >/dev/null 2>&1 || modprobe tcic >/dev/null 2>&1
if [ "$?" = "0" ]; then
echo " ${GREEN}PCMCIA found, starting cardmgr.${NORMAL}"
modprobe ds >/dev/null 2>&1
cardmgr >/dev/null 2>&1 && sleep 4
fi
fi

# Initialize hardware
if [ -f /proc/sys/kernel/modprobe ]; then
   if ! strstr cmdline nomodules && [ -f /proc/modules ] ; then
       sysctl -w kernel.modprobe="/sbin/modprobe" >/dev/null 2>&1
       sysctl -w kernel.hotplug="/sbin/udevsend" >/dev/null 2>&1
   else
       # We used to set this to NULL, but that causes 'failed to exec' messages"
       sysctl -w kernel.modprobe="/bin/true" >/dev/null 2>&1
       sysctl -w kernel.hotplug="/bin/true" >/dev/null 2>&1
   fi
fi

/opt/booyo/hwsetup-booyo

echo -n $"Initializing hardware... "
ide=""
scsi=""
network=""
audio=""
pcmcia=""
other=""
eval `/opt/booyo/kmodule-booyo | while read devtype mod ; do
	case "$devtype" in
		"IDE")	ide="$ide $mod"
		   echo "ide=\"$ide"\";;
		"SCSI") scsi="$scsi $mod"
		   echo "scsi=\"$scsi"\";;
		"NETWORK") network="$network $mod"
		   echo "network=\"$network"\";;
		"AUDIO") audio="$audio $mod"
		   echo "audio=\"$audio"\";;
		"SOCKET") pcmcia="$pcmcia $mod"
		   echo "pcmcia=\"$pcmcia"\";;
		*) other="$other $mod"
		   echo "other=\"$other"\";;
	esac
done`

load_module () {
	LC_ALL=C fgrep -q "$1" /etc/hotplug/blacklist || modprobe $1 >/dev/null 2>&1
}

# IDE
for module in $ide ; do
	load_module $module >/dev/null 2>&1
done

# SCSI
for module in `/sbin/modprobe -c | awk '/^alias[[:space:]]+scsi_hostadapter[[:space:]]/ { print $3 }'` $scsi; do
	load_module $module >/dev/null 2>&1
done
load_module floppy >/dev/null 2>&1

echo -n $" storage"

# Fat-Client-Version: DHCP Broadcast for IP address
if checkbootparam "nodhcp"; then
rm -f /etc/sysconfig/network
cat << EOM > /etc/sysconfig/network
NETWORKING=no
HOSTNAME=localhost.localdomain
EOM
else
NETDEVICES="$(awk -F: '/eth.:|tr.:/{print $1}' /proc/net/dev 2>/dev/null)"
for DEVICE in $NETDEVICES
do
cat << EOM > /etc/sysconfig/network-scripts/ifcfg-$DEVICE
DEVICE=$DEVICE
ONBOOT=yes
BOOTPROTO=dhcp
EOM
done
fi

pushd /etc/sysconfig/network-scripts >/dev/null 2>&1
interfaces=`ls ifcfg* | LC_ALL=C egrep -v '(ifcfg-lo|:|rpmsave|rpmorig|rpmnew)' | \
            LC_ALL=C egrep -v '(~|\.bak)$' | \
            LC_ALL=C egrep 'ifcfg-[A-Za-z0-9\._-]+$' | \
	    sed 's/^ifcfg-//g' |
	    sed 's/[0-9]/ &/' | LC_ALL=C sort -k 1,1 -k 2n | sed 's/ //'`

for i in $interfaces ; do
	eval $(LC_ALL=C fgrep "DEVICE=" ifcfg-$i)
	load_module $DEVICE >/dev/null 2>&1
done
popd >/dev/null 2>&1

for module in $network ; do
	load_module $module >/dev/null 2>&1
done

echo -n $" network"

# Sound
for module in `/sbin/modprobe -c | awk '/^alias[[:space:]]+snd-card-[[:digit:]]+[[:space:]]/ { print $3 }'` $audio; do
	load_module $module >/dev/null 2>&1
done

echo -n $" audio"

# pcmcia
if [ -z "$pcmcia" ] || strstr "$cmdline" nopcmcia ; then
        cat << EOM > /etc/sysconfig/pcmcia
PCMCIA=no
PCIC=
PCIC_OPTS=
CORE_OPTS=
EOM
else
for module in $pcmcia ; do
	case "$module" in
		"yenta_socket")	FOUND="yes"
		   MODULE=$module;;
		"i82365") FOUND="yes"
		   MODULE=$module;;
		"tcic") FOUND="yes"
		   MODULE=$module;;
		*) FOUND="no"
		   MODULE="";;
	esac
done
        cat << EOM > /etc/sysconfig/pcmcia
PCMCIA=$FOUND
PCIC=$MODULE
PCIC_OPTS=
CORE_OPTS=
EOM
fi

# Everything else (duck and cover)
for module in $other ; do
	load_module $module >/dev/null 2>&1
done

echo -n $" done"
success
echo

/opt/booyo/mkxf86config

#echo "raidautorun /dev/md0" | nash --quiet

# Start the graphical boot, if necessary; /usr may not be mounted yet, so we
# may have to do this again after mounting
# RHGB_STARTED=0
mount -n /dev/pts

#if LC_ALL=C fgrep rhgb /proc/cmdline > /dev/null 2>&1 && [ "$BOOTUP" = "color" -a "$GRAPHICAL" = "yes" -a -x /usr/bin/rhgb ]; then
#   LC_MESSAGES= /usr/bin/rhgb
#   RHGB_STARTED=1
#fi

# Configure kernel parameters
# update_boot_stage RCkernelparam
action $"Configuring kernel parameters: " sysctl -e -p /etc/sysctl.conf >/dev/null 2>&1

# Set the system clock.
# update_boot_stage RCclock
ARC=0
SRM=0
UTC=0

if [ -f /etc/sysconfig/clock ]; then
   . /etc/sysconfig/clock

   # convert old style clock config to new values
   if [ "${CLOCKMODE}" = "GMT" ]; then
      UTC=true
   elif [ "${CLOCKMODE}" = "ARC" ]; then
      ARC=true
   fi
fi

CLOCKDEF=""
CLOCKFLAGS="$CLOCKFLAGS --hctosys"

case "$UTC" in
    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --utc"
		CLOCKDEF="$CLOCKDEF (utc)" ;;
    no|false)	CLOCKFLAGS="$CLOCKFLAGS --localtime"
		CLOCKDEF="$CLOCKDEF (localtime)" ;;
esac
case "$ARC" in
    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --arc"
		CLOCKDEF="$CLOCKDEF (arc)" ;;
esac
case "$SRM" in
    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --srm"
		CLOCKDEF="$CLOCKDEF (srm)" ;;
esac

/sbin/hwclock $CLOCKFLAGS >/dev/null 2>&1

action $"Setting clock $CLOCKDEF: `date`" date >/dev/null 2>&1

if [ "$CONSOLETYPE" = "vt" -a -x /bin/loadkeys ]; then
 KEYTABLE=
 KEYMAP=
 if [ -f /etc/sysconfig/console/default.kmap ]; then
  KEYMAP=/etc/sysconfig/console/default.kmap
 else
  if [ -f /etc/sysconfig/keyboard ]; then
    . /etc/sysconfig/keyboard
  fi
  if [ -n "$KEYTABLE" -a -d "/lib/kbd/keymaps" ]; then
     KEYMAP="$KEYTABLE.map"
  fi
 fi
 if [ -n "$KEYMAP" ]; then
  # Since this takes in/output from stdin/out, we can't use initlog
  if [ -n "$KEYTABLE" ]; then
    echo -n $"Loading default keymap ($KEYTABLE): "
  else
    echo -n $"Loading default keymap: "
  fi
  loadkeys $KEYMAP < /dev/tty0 > /dev/tty0 2>/dev/null && \
     success $"Loading default keymap" || failure $"Loading default keymap"
  echo
 fi
fi

# Set the hostname.
# update_boot_stage RChostname
action $"Setting hostname ${HOSTNAME}: " hostname ${HOSTNAME} >/dev/null 2>&1

# Initialiaze ACPI bits
if [ -d /proc/acpi ]; then
   for module in /lib/modules/$unamer/kernel/drivers/acpi/* ; do
      insmod $module >/dev/null 2>&1
   done
fi

# Unmount the initrd, if necessary
#if LC_ALL=C fgrep -q /initrd /proc/mounts && ! LC_ALL=C fgrep -q /initrd/loopfs /proc/mounts ; then
#   if [ -e /initrd/dev/.devfsd ]; then
#      umount /initrd/dev
#   fi
#   umount /initrd
#   /sbin/blockdev --flushbufs /dev/ram0 >/dev/null 2>&1
#fi
                                                                                
if [ -x /sbin/isapnp -a -f /etc/isapnp.conf -a ! -f /proc/isapnp ]; then
    # check for arguments passed from kernel
    if ! strstr "$cmdline" nopnp ; then
	PNP=yes
    fi
    if [ -n "$PNP" ]; then
	action $"Setting up ISA PNP devices: " /sbin/isapnp /etc/isapnp.conf
    else
	action $"Skipping ISA PNP configuration at users request: " /bin/true
    fi
fi

# Remount the root filesystem read-write.
# update_boot_stage RCmountfs
state=`awk '/ \/ / && ($3 !~ /rootfs/) { print $4 }' /proc/mounts`
[ "$state" != "rw" -a "$READONLY" != "yes" ] && \
  action $"Remounting root filesystem in read-write mode: " mount -n -o remount,rw /

# Clear mtab
(> /etc/mtab) &> /dev/null

# Remove stale backups
rm -f /etc/mtab~ /etc/mtab~~

# Enter root, /proc and (potentially) /proc/bus/usb and devfs into mtab.
mount -f /
#mount -f /proc
#mount -f /sys >/dev/null 2>&1
#mount -f /dev/pts
[ -f /proc/bus/usb/devices ] && mount -f -t usbfs usbfs /proc/bus/usb
[ -e /dev/.devfsd ] && mount -f -t devfs devfs /dev 
egrep " /BOOYO | /cdrom | /ramdisk " /proc/mounts >> /etc/mtab

if ! strstr "$cmdline" nomodules && [ -f /proc/modules ] ; then
    USEMODULES=y
fi

# Load modules (for backward compatibility with VARs)
if [ -f /etc/rc.modules ]; then
	/etc/rc.modules
fi

# Mount all other filesystems (except for NFS and /proc, which is already
# mounted). Contrary to standard usage,
# filesystems are NOT unmounted in single user mode.
#action $"Mounting local filesystems: " mount -a -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs -O no_netdev
action $"Mounting local filesystems: " /opt/booyo/rebuildfstab >/dev/null 2>&1

# Start the graphical boot, if necessary and not done yet.
#if LC_ALL=C fgrep rhgb /proc/cmdline > /dev/null 2>&1 && [ "$RHGB_STARTED" -eq 0 -a "$BOOTUP" = "color" -a "$GRAPHICAL" = "yes" -a -x /usr/bin/rhgb ]; then
#   LC_MESSAGES= /usr/bin/rhgb
#   RHGB_STARTED=1
#fi

# Initialize pseudo-random number generator
if [ -f "/var/lib/random-seed" ]; then
	cat /var/lib/random-seed > /dev/urandom
else
	touch /var/lib/random-seed
fi
chmod 600 /var/lib/random-seed
dd if=/dev/urandom of=/var/lib/random-seed count=1 bs=512 2>/dev/null

# Use the hardware RNG to seed the entropy pool, if available
[ -x /sbin/rngd -a -f /dev/hw_random ] && rngd

# Do we need (w|u)tmpx files? We don't set them up, but the sysadmin might...
#_NEED_XFILES=
#[ -f /var/run/utmpx -o -f /var/log/wtmpx ] && _NEED_XFILES=1

# Clean up /var.  I'd use find, but /usr may not be mounted.
for afile in /var/lock/* /var/run/* ; do
	if [ -d "$afile" ]; then
	   case "$afile" in
		*/news|*/mon)	;;
		*/sudo)		rm -rf $afile/*/* ;;
		*/vmware)	rm -rf $afile/*/* ;;
		*/samba)	rm -rf $afile/*/* ;;
                */screen)       rm -rf $afile/* ;;
                */iiim)         rm -rf $afile/.i* ;;
		*)		rm -f $afile/* ;;
	   esac
	else
	   rm -f $afile
	fi
done
rm -f /var/lib/rpm/__db* &> /dev/null
rm -f /var/gdm/.gdmfifo &> /dev/null

# Reset pam_console permissions
[ -x /sbin/pam_console_apply ] && /sbin/pam_console_apply -r

{
# Clean up utmp/wtmp
> /var/run/utmp
touch /var/log/wtmp
chgrp utmp /var/run/utmp /var/log/wtmp
chmod 0664 /var/run/utmp /var/log/wtmp
#if [ -n "$_NEED_XFILES" ]; then
#  > /var/run/utmpx
#  touch /var/log/wtmpx
#  chgrp utmp /var/run/utmpx /var/log/wtmpx
#  chmod 0664 /var/run/utmpx /var/log/wtmpx
#fi

# Clean up various /tmp bits
rm -f /tmp/.X*-lock /tmp/.lock.* /tmp/.gdm_socket /tmp/.s.PGSQL.*
rm -rf /tmp/.X*-unix /tmp/.ICE-unix /tmp/.font-unix /tmp/hsperfdata_* \
       /tmp/kde-* /tmp/ksocket-* /tmp/mc-* /tmp/mcop-* /tmp/orbit-*  \
       /tmp/scrollkeeper-*  /tmp/ssh-*
# Make ICE directory
mkdir -m 1777 -p /tmp/.ICE-unix >/dev/null 2>&1
chown root:root /tmp/.ICE-unix
#[ -n "$SELINUX" ] && restorecon /tmp/.ICE-unix >/dev/null 2>&1

# Start up swapping.
# update_boot_stage RCswap
action $"Enabling swap space: " swapon -a -e

# Set up binfmt_misc
/bin/mount -t binfmt_misc none /proc/sys/fs/binfmt_misc > /dev/null 2>&1

# Initialize the serial ports.
if [ -f /etc/rc.serial ]; then
	. /etc/rc.serial
fi

# Boot time profiles. Yes, this should be somewhere else.
if [ -x /usr/sbin/system-config-network-cmd ]; then
  if strstr "$cmdline" netprofile= ; then
    for arg in $cmdline ; do
        if [ "${arg##netprofile=}" != "${arg}" ]; then
	    /usr/sbin/system-config-network-cmd --profile ${arg##netprofile=}
        fi
    done
  fi
fi

# Now that we have all of our basic modules loaded and the kernel going,
# let's dump the syslog ring somewhere so we can find it later
#dmesg -s 131072 > /var/log/dmesg

# create the crash indicator flag to warn on crashes, offer fsck with timeout
#touch /.autofsck &> /dev/null

kill -TERM `/sbin/pidof getkey` >/dev/null 2>&1
} &
if strstr "$cmdline" confirm ; then
	touch /var/run/confirm
fi
if [ "$PROMPT" != "no" ]; then
	/sbin/getkey i && touch /var/run/confirm
fi
wait

# Let rhgb know that we're leaving rc.sysinit
#if [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --ping ; then
#    /usr/bin/rhgb-client --sysinit
#fi
