#!/bb/ash
# RC Script for Tiny Core Linux
# (c) Robert Shingledecker 2004-2012
# Several modifications for speed by Curaga
. /etc/init.d/tc-functions

PATH="/bb:/bin:/sbin:/usr/bin:/usr/sbin"
export PATH
USER="tuser"
TCEDIR="/tmp/tce"
TCLOOP="/tmp/tcloop"
TCEINSTALLED="/usr/local/tce.installed"
INSTALLED=""
VERSION="$(cat /usr/share/doc/tc/release.txt)"
KERNEL="$(uname -r)"

addUser(){
	echo "${GREEN}Adding user ${YELLOW} $USER ${NORMAL}"
	/bb/adduser -s /bb/sh -G staff -D "$USER"
	echo "$USER":tcuser | /bb/chpasswd -m
	echo -e "$USER\tALL=NOPASSWD: ALL" >> /etc/sudoers
}

### END functions

# Main
clear
echo "${GREEN}Booting ${YELLOW}$VERSION ${NORMAL}"
echo "${GREEN}Running Linux Kernel ${YELLOW}$KERNEL${GREEN}.${NORMAL}"
export PATH=/usr/local/sbin:/usr/local/bin:"$PATH"

# Here we check all the boot parameters using the fastest way known to men, case & loop
echo -n "${BLUE}Checking boot options...${NORMAL}"
for i in `cat /proc/cmdline`; do
	case $i in
		*=*)
			case $i in
				waitusb*) WAITUSB=${i#*=} ;;
				lang*) LANGUAGE=${i#*=} ;;
				kmap*) KEYMAP=${i#*=} ;;
				tz*) TZ=${i#*=} ;;
				desktop*) DESKTOP=${i#*=} ;;
				icons*) ICONS=${i#*=} ;;
				noicons*) NOICONS=${i#*=} ;;
				user*) USER=${i#*=} ;;
				home*) MYHOME=${i#*=} ;;
				tcvd*) TCVD=${i#*=} ;;
				opt*) MYOPT=${i#*=} ;;
				swapfile*) SWAPFILE=${i#*=} ;;
				resume*) RESUME=${i#*=} ;;
				host*) HOST=1 ;;
				nfsmount* ) NFSMOUNT=${i#*=} ;;
				tftplist* ) TFTPLIST=${i#*=} ;;
				httplist* ) HTTPLIST=${i#*=} ;;
				aoe* ) AOE=${i#*=} ;;
				nbd* ) NBD=${i#*=} ;;
				mydata* ) MYDATA=${i#*=} ;;
				pretce* ) PRETCE=${i#*=} ;;
				xvesa* ) XVESA=${i#*=} ;;
				rsyslog=* ) RSYSLOG=${i#*=}; SYSLOG=1 ;;
				blacklist* ) BLACKLIST="$BLACKLIST ${i#*=}" ;;
				iso* ) ISOFILE=${i#*=} ;;
			esac
		;;
		*)
			case $i in
				checkfs) CHECKFS=1 ;;
				nozswap) NOZSWAP=1 ;;
				nofstab) NOFSTAB=1 ;;
				nortc) NORTC=1 ;;
				syslog) SYSLOG=1 ;;
				noutc) NOUTC=1 ;;
				nodhcp) NODHCP=1 ;;
				noicons) NOICONS=1 ;;
				text) TEXT=1 ;;
				xonly) XONLY=1 ;;
				superuser) SUPERUSER=1 ;;
				noswap) NOSWAP=1 ;;
				secure) SECURE=1 ;;
				protect) PROTECT=1 ;;
				cron) CRON=1 ;;
				xsetup) XSETUP=1 ;;
				laptop) LAPTOP=1 ;;
				base) ONLYBASE=1 ;;
				showapps) SHOWAPPS=1 ;;
				norestore) NORESTORE=1 ;;
				noautologin) NOAUTOLOGIN=1 ;;
				pause) PAUSE=1 ;;
			esac
		;;
	esac
done
echo "${GREEN}Done.${NORMAL}"

if [ -n "$BLACKLIST" ]; then
	mkdir -p /etc/modprobe.d
	for i in $BLACKLIST; do
		echo "$i" | tr ',' '\n' | while read j ; do
			echo "blacklist $j" >> /etc/modprobe.d/blacklist.conf
		done
	done
fi

# Start Udev to populate /dev and handle hotplug events
echo -n "${BLUE}Starting udev daemon for hotplug support...${NORMAL}"
/sbin/depmod -a
udevd --daemon 2>&1 >/dev/null
udevadm trigger --action=add 2>&1 >/dev/null &
[ -x /etc/init.d/tc-mkdev ] && /etc/init.d/tc-mkdev
/usr/bin/rotdash $!
echo "${GREEN}Done.${NORMAL}"

[ -n "$WAITUSB" ] && (
	case $WAITUSB in
		*LABEL*) 
			timeout=${WAITUSB%:*} 
			ID=${WAITUSB#*:}
			PART=${ID#*=}
			mkdir /tmp/testusbdevice
			while [ $timeout -gt 0 ]; do
				echo -en "${BLUE}\rWaiting as requested ... ${YELLOW}$timeout${NORMAL}"
				timeout=$(($timeout - 2))
				udevadm settle
				DEV1=$(blkid -L "$PART")
				sleep 2
				mount "$DEV1" /tmp/testusbdevice > /dev/null 2>&1 
				if /bb/df "$DEV1" > /dev/null 2>&1; then
					timeout=0
				else
					continue
				fi
	  				
			done
			umount "$DEV1" > /dev/null 2>&1
			rmdir /tmp/testusbdevice
			echo
		;;
		*UUID*) 
			timeout=${WAITUSB%:*}
			ID=${WAITUSB#*:}
			PART=${ID#*=}
			mkdir /tmp/testusbdevice

			while [ $timeout -gt 0 ]; do
				echo -en "${BLUE}\rWaiting as requested ... ${YELLOW}$timeout${NORMAL}"
				timeout=$(($timeout - 2))
				udevadm settle
				DEV1=$(blkid -U "$PART")
				sleep 2
				mount "$DEV1" /tmp/testusbdevice > /dev/null 2>&1 
				if /bb/df "$DEV1" > /dev/null 2>&1; then
					timeout=0
				else
					continue
				fi
	  				
			done
			umount "$DEV1" > /dev/null 2>&1
			rmdir /tmp/testusbdevice
			echo
		;;
		*) sleep "$WAITUSB" 
		;;	
	esac
)

modprobe loop 2>/dev/null
modprobe ohci_hcd 2>/dev/null

if [ -n "$NOZSWAP" ]; then
	echo "${BLUE}Skipping compressed swap in ram as requested from the boot command line.${NORMAL}"
else
	modprobe -q zram
	modprobe -q zcache

	while [ ! -e /dev/zram0 ]; do usleep 50000; done
	grep MemFree /proc/meminfo | awk '{print $2/4 "K"}' > /sys/block/zram0/disksize
	mkswap /dev/zram0 >/dev/null 2>&1
	swapon /dev/zram0
	echo "/dev/zram0      swap            swap     defaults,noauto      0 0" >> /etc/fstab # Lets swapoff -a work
fi

if [ -n "$NOFSTAB" ]; then
	echo "${BLUE}Skipping /etc/fstab creation as requested from the boot command line.${NORMAL}"
else
	echo "${BLUE}Scanning hard disk partitions to create ${YELLOW}/etc/fstab ${NORMAL}"
	modprobe scsi_wait_scan 2>/dev/null
	/usr/sbin/rebuildfstab & fstab_pid=$!
	mv /tmp/98-tc.rules /etc/udev/rules.d/. 2>/dev/null
	udevadm control --reload-rules &
fi

checkfs() {
	FS=/dev/"$1"
	if ! grep "^$FS$" /tmp/checkfs > /dev/null 2>&1; then
     		if grep "$FS" /etc/mtab > /dev/null 2>&1; then
     			:
     		else
			  if grep "$FS" /etc/fstab | grep ext[2,3,4] > /dev/null 2>&1; then
        		  	e2fsck -n "$FS" > /tmp/checkfs."$1" 2>&1
				if [ "$?" != "0" ]; then
					echo "$FS" "$2" >> /tmp/checkfs.errors
				
					echo -n "${YELLOW}"$FS" ${RED}being used for "$2" contains filesystem errors.  Advised to reboot \
with "base" option to fix them.  Proceed anyway?  (y/N:)"
					read ans
					if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
						:
					else
						echo "${YELLOW}Rebooting..${NORMAL}"
						/bb/reboot
					fi
				fi
				echo "$FS" >> /tmp/checkfs	
			  else
				echo "Filesystem is not ext[2,3,4] and cannot be checked." > /tmp/checkfs."$1"
			  fi 
    		 fi
 	fi
}

[ -n "$LANGUAGE" ] || LANGUAGE="C"
echo -n "${GREEN}Setting Language to ${MAGENTA}$LANGUAGE"
echo "LANG=$LANGUAGE" > /etc/sysconfig/language
export LANG=$LANGUAGE
echo "${GREEN} Done.${NORMAL}"

if [ -n "$TZ" ]; then
	echo -n "${GREEN}Setting Timezone to ${MAGENTA}$TZ"
	export TZ && echo "TZ=$TZ" > /etc/sysconfig/timezone
	echo "${GREEN} Done.${NORMAL}"
fi	

#if [ -n "$NORTC" ]; then
#	echo "${BLUE}Skipping rtc as requested from the boot command line.${NORMAL}"
#else
#	while [ ! -e /dev/rtc0 ]; do usleep 50000; done
#	if [ -n "$NOUTC" ]; then
#		hwclock -l -s &
#	else
#		hwclock -u -s &
#	fi
#fi

if [ -n "$HOST" ]; then
	sethostname
else
	/bb/hostname -F /etc/hostname
fi

if [ -n "$SYSLOG" ]; then
	 [ -z "$RSYSLOG" ] || SOPTS=" -R $RSYSLOG -L "
	 /bb/syslogd ${SOPTS} && echo "${GREEN}syslog started.${NORMAL}"
	 /bb/klogd && echo "${GREEN}klog started."
fi

/bb/ifconfig lo 127.0.0.1 up
/bb/route add 127.0.0.1 lo &

if [ -n "$AOE" ]; then
	IFACE=$( echo $AOE | awk -F: '{ print $1 }')
	DEV=$( echo $AOE | awk -F: '{ print $2 }')
	ifconfig $IFACE up
	modprobe aoe
	echo "${BLUE}Waiting for $DEV${NORMAL}"
	CNT=120
	until [ -e /dev/etherd/interfaces ]
	do
		[ $((--CNT)) -le 0 ] && break
		echo -ne "${RED}$CNT  \r"
		sleep 1
	done
	printf "%s\0" $IFACE > /dev/etherd/interfaces
	echo > /dev/etherd/discover
	until [ -e /dev/etherd/$DEV ]
	do
		[ $((--CNT)) -le 0 ] && break
		echo -ne "${RED}$CNT  \r"
		sleep 1
	done
	if [ $CNT -gt 0 ] ; then
		ln -s /dev/etherd/$DEV /dev/$DEV
		mkdir -p /mnt/$DEV
		mount /dev/$DEV /mnt/$DEV >/dev/null 2>&1
	else
		echo -ne "\n$DEV NOT mounted! Press <ENTER>" ; read junk
	fi
	echo "${NORMAL}"
fi

# dhcp.sh runs udhcpc async, so it exits before network is up
wait4Server() {
	/etc/init.d/dhcp.sh
	SERVER=$1 ; NOPING=$2 ; CNT=0
	if [ "$NOPING" == "1" ] ; then
		until ifconfig | grep -q Bcast
		do
			[ $((CNT++)) -gt 60 ] && break || sleep 1
		done
		sleep 1
	else
		until ping -c 1 $SERVER >/dev/null 2>&1     
		do
			[ $((CNT++)) -gt 60 ] && break || sleep 1
		done
	fi
	DHCP_RAN=1
}

if [ -n "$USER" ]; then
	if ! grep "$USER" /etc/passwd >/dev/null; then addUser; fi
else
	USER="tuser"
fi
chown root:root /etc/sudoers
chown -R root:staff /etc/sysconfig
chown -R root:staff /tmp
chown -R root:staff /opt
chmod 775 /opt/debextra
chmod 775 /opt
chmod 1777 /tmp
echo "$USER" > /etc/sysconfig/tcuser
mkdir -p /home/"$USER"

if [ -n "$SUPERUSER" ]; then
	touch /etc/sysconfig/superuser
fi

# Setup base directories for TCE Applicatons 
[ -d "$TCEINSTALLED" ] || mkdir "$TCEINSTALLED"
[ -d "$TCEDIR" ] || mkdir "$TCEDIR"
[ -d "$TCLOOP" ] || mkdir "$TCLOOP"
ln -sf "$TCEDIR" /etc/sysconfig/tcedir
chgrp staff "$TCEDIR" "$TCEINSTALLED" "$TCLOOP"
chmod g+w "$TCEDIR" "$TCEINSTALLED" "$TCLOOP"

chmod u+s /bin/busybox /usr/bin/sudo /bin/mount /bin/umount

get_app() {
	LOADER=sce-load ; PKG=sce
	PROTO=$1
	HOST=${2%%/*}
	REST=${2#*/}
	LENG=$((${#HOST}-1)) ; LAST=${HOST:$LENG:1}
	[ "$LAST" == ":" ] && HOST=${HOST:0:$LENG}
	FILE=${REST%:*}
	NAME=${FILE##*/}
	OPTS=${REST#*:}
	[ "$OPTS" == "no-ping" ] && NOPING=1 || NOPING=0
	[ -z "$DHCP_RAN" ] && wait4Server $HOST $NOPING
	(	TCE=/tmp/tce/$PKG
		mkdir -p $TCE
		cd $TCE
		[ "$PROTO" = "TFTP" ] && tftp -g -r $FILE $HOST
		[ "$PROTO" = "HTTP" ] && wget -q http://${HOST}/${FILE}
		[ $SHOWAPPS ] && echo -ne "${BLUE}TFTP Loading Extensions ${YELLOW}"
		dos2unix -u $NAME
		while read FILE FL
		do
			APP=${FILE##*/}
			[ "$PROTO" = "TFTP" ] && tftp -g -r $FILE $HOST
			[ "$PROTO" = "HTTP" ] && wget -q http://${HOST}/${FILE}
			[ $SHOWAPPS ] && echo -ne "$APP "
			if [ "$FL" = "-d" ] ; then
				( su "$USER" -c "$LOADER $FL $TCE/$APP" ) &
				sleep 5
			else
				su "$USER" -c "$LOADER $FL $TCE/$APP"
			fi
			[ "$FL" == "-c" ] && rm $APP
		done < $NAME
		[ $SHOWAPPS ] && echo "${GREEN} Done.${NORMAL}"
		rm $NAME
	)
}

# Preload drivers needed for tce/opt/home on lvm/RAID etc
if [ -n "$PRETCE" ]; then
	PKG=sce ; LOADER=sce-load ; LIST=sceboot.lst
	RAM=/tmp/tce
	CPTOFS=""
	if [ "$PRETCE" == "RAM" ] ; then
		SRC=$RAM
	else
		PPATH=${PRETCE%%/*}
		mkdir -p /mnt/$PPATH
		mount /dev/$PPATH /mnt/$PPATH
		SRC=/mnt/$PRETCE
		[ -x $SRC/onpre.sh ] && cp $SRC/onpre.sh /tmp
	fi
	while read NAME OPT
	do
		su "$USER" -c "/usr/bin/$LOADER $SRC/$PKG/$NAME"
	done < $SRC/$LIST
	[ -x /tmp/onpre.sh ] && /tmp/onpre.sh
fi

# First process tftp and http entries
modprobe -q squashfs 2>/dev/null
[ -n "$TFTPLIST" ] && get_app "TFTP" $TFTPLIST
[ -n "$HTTPLIST" ] && get_app "HTTP" $HTTPLIST

if [ -n "$NFSMOUNT" ]; then
	SERVER=$( echo $NFSMOUNT | awk -F: '{ print $1 }')
	DIR=$(    echo $NFSMOUNT | awk -F: '{ print $2 }')
	OPTS=$(   echo $NFSMOUNT | awk -F: '{ print $3 }')
	[ "$OPTS" == "no-ping" ] && NOPING=1 || NOPING=0
	[ -z "$DHCP_RAN" ] && wait4Server $SERVER $NOPING
	wait $fstab_pid
	[ -x /bin/mount ] && NOLOCK="" || NOLOCK=",nolock"
	echo "${SERVER}:${DIR} /mnt/nfs nfs defaults${NOLOCK} 0 0" >> /etc/fstab
	[ $SHOWAPPS ] && echo "Mounting ${SERVER}:${DIR}"
	mkdir /mnt/nfs
	mount /mnt/nfs >/dev/null 2>&1
fi

touch /var/tmp/k5_skip
if [ -n "$NBD" ]; then
	modprobe -q nbd
	SERVER=$( echo $NBD | awk -F: '{ print $1 }')
	PORT=$(    echo $NBD | awk -F: '{ print $2 }')
	OPTS=$(   echo $NBD | awk -F: '{ print $3 }')
	[ "$OPTS" == "no-ping" ] && NOPING=1 || NOPING=0
	[ -z "$DHCP_RAN" ] && wait4Server $SERVER $NOPING
	nbd-client $SERVER $PORT /dev/nbd0
	mkdir /mnt/nbd0
	mount /dev/nbd0 /mnt/nbd0 2>&1
	pidof nbd-client nbd0 >> /var/tmp/k5_skip
fi

if grep -q "^/dev/root" /proc/mounts; then
	INSTALLED=1
	echo "${RED}Detected Traditional Hard drive installation.${NORMAL}"
fi

/sbin/ldconfig 2>/dev/null

[ -n "$TEXT" ] && touch /etc/sysconfig/text
[ -n "$XONLY" ] && touch /etc/sysconfig/xonly

#Check for Virtual Hard Drive
if [ -n "$TCVD" ]; then
	wait $fstab_pid
	TCVD="${TCVD#/}"
	TCVD="${TCVD#dev/}"
	TCVD_DEVICE="${TCVD%%/*}"
	TCVD_LOOPFILE="${TCVD#*/}"
	if [ "$TCVD_DEVICE" == "$TCVD_LOOPFILE" ]; then
		TCVD_DEVICE="$(autoscan $TCVD_LOOPFILE 'f')"
	fi   
	PARTITION="${TCVD_DEVICE##/dev/}"
	find_mountpoint "$PARTITION"
	if [ -n "$MOUNTPOINT" ]; then
		[ "$MOUNTED" == "yes" ] || mount "$MOUNTPOINT"
		usleep_progress
		if [ -f "$MOUNTPOINT"/"$TCVD_LOOPFILE" ]; then
			[ -d /mnt/tcvd ] || /bb/mkdir /mnt/tcvd
			ln -sf "$MOUNTPOINT"/"$TCVD_LOOPFILE" /dev/tcvd
			printf "/dev/tcvd \t/mnt/tcvd \text2\tloop\t0 0 #Added by TC\n" >> /etc/fstab
			sync
		fi
	fi
fi  

unset HOME_SETUP
if [ -n "$MYHOME" ]; then
	wait $fstab_pid
	PARTITION=${MYHOME##/dev/}
	find_mountpoint "$PARTITION"
	if [ -n "$MOUNTPOINT" ]; then
		if [ "$CHECKFS" == "1" ]; then
			THISDEV=`echo "$MOUNTPOINT" | sed 's:/mnt/::'`
			checkfs "$THISDEV" HOME
		fi
		[ "$MOUNTED" == "yes" ] || mount "$MOUNTPOINT"

		MS_MNTS=`mount|awk '$5~/fat|vfat|msdos|ntfs/{printf "%s ",$3}'`
		case "$MS_MNTS" in 
			*"$MOUNTPOINT"* )
				echo "Persistent HOME not supported on Microsoft File Systems."
				echo "${YELLOW}Request for persistent HOME on $PARTITION ignored."
				echo -n "${RED}Press Enter key.${NORMAL}"; read ans
			;;
			* )
				if [ -d "$MOUNTPOINT/home/$USER" ]; then
					echo "${GREEN}Mounting ${YELLOW}/home ${GREEN} on ${YELLOW}"$MOUNTPOINT"/home.${NORMAL}"
					mount --bind "$MOUNTPOINT/home" /home
					HOME_SETUP=1
				else
					echo "${GREEN}Mounting ${YELLOW}/home ${GREEN} on ${YELLOW}"$MOUNTPOINT"/home.${NORMAL}"
					mkdir -p "$MOUNTPOINT/home/$USER"
					mount --bind "$MOUNTPOINT/home" /home
				fi
			;;
		esac

	else
		echo "Invalid device specified. Ignoring persistent HOME request!"
	fi
fi

setupHome
tar -xzvf /mydata.tgz -C / 2>/dev/null 1>&2
chown -R tuser:staff /home/tuser

unset OPT_SETUP
if [ -n "$MYOPT" ]; then
	wait $fstab_pid
	PARTITION=${MYOPT##/dev/}
	find_mountpoint "$PARTITION"
	if [ -n "$MOUNTPOINT" ]; then
		if [ "$CHECKFS" == "1" ]; then
			THISDEV=`echo "$MOUNTPOINT" | sed 's:/mnt/::'`
			checkfs "$THISDEV" OPT
		fi
		[ "$MOUNTED" == "yes" ] || mount "$MOUNTPOINT"

		MS_MNTS=`mount|awk '$5~/fat|vfat|msdos|ntfs/{printf "%s ",$3}'`
		case "$MS_MNTS" in 
			*"$MOUNTPOINT"* )
				echo "Persistent OPT not supported on Microsoft File Systems."
				echo "${YELLOW}Request for persistent OPT on $PARTITION ignored."
				echo -n "${RED}Press Enter key.${NORMAL}"; read ans
			;;
			* )
				echo "${GREEN}Mounting ${YELLOW}/opt ${GREEN} on ${YELLOW}"$MOUNTPOINT"/opt.${NORMAL}"
				[ -d "$MOUNTPOINT"/opt ] || mkdir "$MOUNTPOINT"/opt
				yes n | cp -ai /opt/. "$MOUNTPOINT"/opt/ 2>/dev/null
				mount --bind "$MOUNTPOINT"/opt/ /opt/
				chgrp -R staff /opt/ >/dev/null 2>&1
				chmod -R g+w /opt/ >/dev/null 2>&1
				chmod g+s /opt/ >/dev/null 2>&1
			;;
		esac

	else
		echo "Invalid device specified. Skipping persistent OPT request!"
	fi
fi

[ -n "$XSETUP" ] && touch /tmp/xsetup_requested

if [ -n "$LAPTOP" ]; then
	modprobe ac && modprobe battery 2>/dev/null
	modprobe yenta_socket >/dev/null 2>&1 || modprobe i82365 >/dev/null 2>&1
	udevadm trigger 2>/dev/null >/dev/null &
	echo " ${GREEN}Laptop options enabled (AC,Battery, & PCMCIA).${NORMAL}"
fi

if [ -n "$NOSWAP" ]; then
	echo "${BLUE}Ignoring swap partition(s) seek as requested.${NORMAL}"
else
	wait $fstab_pid
	/bb/swapon -a 2>/dev/null
	if [ "$?" == 0 ]; then echo "${GREEN}Possible swap partition(s) enabled.${NORMAL}"; fi
fi

if [ -n "$SWAPFILE" ]; then
	wait $fstab_pid
	SWAPFILE=${SWAPFILE##/dev/}
	find_mountpoint "$SWAPFILE"
	if [ -n "$MOUNTPOINT" ]; then
		if [ "$MOUNTED" == "yes" ]; then
			swapon "$MOUNTPOINT"/tc.swp
		else
			mount "$MOUNTPOINT" && swapon "$MOUNTPOINT"/tc.swp
		fi
	fi
fi

if [ -n "$RESUME" ]; then
	rm -f /etc/sysconfig/tc.resume 2>/dev/null
	RESUME=`basename $RESUME`
	SWAP_SIZE=$(cat /proc/swaps | grep "$RESUME" | awk '{print $3}')
	[ -z "$SWAP_SIZE" ] && /bb/swapon /dev/"$RESUME" &&  SWAP_SIZE=$(cat /proc/swaps | grep "$RESUME" | awk '{print $3}')
	[ -z "$SWAP_SIZE" ] && exit 1
	MEM_SIZE=$(cat /proc/meminfo | awk '/MemTotal/{print $2}')
	if [ $SWAP_SIZE -gt $MEM_SIZE ]; then
		grep -q disk /sys/power/state &&  echo "disk" > /etc/sysconfig/tc.resume
	fi
fi

if [ "$NOICONS" == 1 ]; then
	> /etc/sysconfig/icons
else  
# If icon manager is specified use that
	if [ -n "$ICONS" ]; then
		echo "$ICONS" > /etc/sysconfig/icons
	else
		[ -s /etc/sysconfig/icons ] && ICONS=`cat /etc/sysconfig/icons`
	fi
fi  
[ "$NOICONS" == "ondemand" ] && touch /etc/sysconfig/noondemandicons
sync

wait $fstab_pid
if [ "$CHECKFS" == "1" ]; then

	for i in `cat /proc/cmdline`; do
		case $i in
			tce=*) TCE=${i#*=} ;;
		esac
	done
	[ -z "$TCE" ] && TCE="$(autoscan 'tce' 'd')"/tce
	if [ "$TCE" != "/tce" ]; then
		TCE_DEVICE="${TCE%%/*}"
		find_mountpoint "$TCE_DEVICE"
		THISDEV=`echo "$MOUNTPOINT" | sed 's:/mnt/::'`
		checkfs "$THISDEV" TCE
	fi

fi

## If udev does not create and mount below directories in /dev.
if [ ! -d /dev/pts ]; then
	mkdir /dev/pts
	mount -t devpts -o gid=5,mode=620 devpts /dev/pts
fi

if [ ! -d /dev/shm ]; then
	mkdir /dev/shm
	mount -t tmpfs tmpfs /dev/shm
fi
##

[ -f /etc/udev/rules.d/60-persistent-storage.rules ] && rm /etc/udev/rules.d/60-persistent-storage.rules
udevadm trigger  2>&1 >/dev/null &&

MSSG="${BLUE}Loading extensions...${NORMAL}"
if [ -n "$SHOWAPPS" ]; then
	touch /etc/sysconfig/showapps
	echo "$MSSG"
	/usr/bin/tce-setup "booting"
	[ -n "$ISOFILE" ] && /usr/bin/fromISOfile "$ISOFILE"
	rm /etc/sysconfig/showapps
	echo "${GREEN}Done.${NORMAL}"
else
	echo -n "$MSSG"
	/usr/bin/tce-setup "booting" > /dev/null 2>&1
	/usr/bin/rotdash $!
	[ -n "$ISOFILE" ] && /usr/bin/fromISOfile "$ISOFILE" > /dev/null 2>&1 &
	/usr/bin/rotdash $!
	echo "${GREEN}Done.${NORMAL}"
	if [ -s /tmp/aberr ]; then
		echo "${RED}Warning! Errors occured during the loading of the extensions.${YELLOW}"
		cat /tmp/aberr
		echo -n "${RED}Press Enter key.${NORMAL}"; read ans
	fi
fi

[ -n "$KEYMAP" ] || KEYMAP="us"
if [ -f "/usr/share/kmap/$KEYMAP.kmap" ]; then
	echo -n "${GREEN}Setting keymap to ${MAGENTA}$KEYMAP"
	/bb/loadkmap < /usr/share/kmap/$KEYMAP.kmap
	echo "KEYMAP=$KEYMAP" > /etc/sysconfig/keymap
	echo "${GREEN} Done.${NORMAL}"
fi

if [ -n "$SECURE" ]; then
	getpasswd "root"
	echo "root:$PASSWD" | chpasswd -m
	getpasswd "$USER"
	echo "$USER:$PASSWD" | chpasswd -m
fi
if [ -n "$PROTECT" ]; then
	getpasswd "encryption"
	echo "$PASSWD" > /etc/sysconfig/bfe
	chown ${USER}:root /etc/sysconfig/bfe
	chmod 600 /etc/sysconfig/bfe
fi

[ -n "$XVESA" ]  && sed -i 's/1024x768x32/'"$XVESA"'/' /home/"$USER"/.xsession 2>/dev/null

# If desktop is specified use that
if [ -n "$DESKTOP" ]; then
	echo "$DESKTOP" > /etc/sysconfig/desktop
else
	[ -s /etc/sysconfig/desktop ] && DESKTOP=`cat /etc/sysconfig/desktop`
fi

# If restore then also pass TCE directory for quick look of mydata.tgz backupfile
if [ -n "$MYDATA" ]; then
	echo "$MYDATA" > /etc/sysconfig/mydata
else
	echo "mydata" > /etc/sysconfig/mydata
fi   
if [ -n "$NORESTORE" ]; then
	echo "${GREEN}Skipping restore as requested from the command line.${NORMAL}"
else
	TCEDIR=`readlink /etc/sysconfig/tcedir`
	/etc/init.d/tc-restore.sh "$TCEDIR"
fi

# After restore items
if [ -n "$NODHCP" ]; then
	echo "${GREEN}Skipping DHCP broadcast/network detection as requested on boot commandline.${NORMAL}"
else
	[ -z "$DHCP_RAN" ] && /etc/init.d/dhcp.sh &
	[ -z "$NORTC" ] || /etc/init.d/settime.sh &
fi

[ -n "$CRON" ] && /etc/init.d/services/crond start

/sbin/loadcpufreq 2>/dev/null &

/opt/bootsync.sh

/bb/addgroup "$USER" cdrom
/bb/addgroup "$USER" tty
/bb/addgroup "$USER" dialout
/bb/addgroup "$USER" audio
/bb/addgroup "$USER" video
/bb/addgroup "$USER" plugdev

[ -n "$NOAUTOLOGIN" ] && echo "booting" > /etc/sysconfig/noautologin
	
/sbin/depmod -a
/sbin/udevadm control --reload-rules
/sbin/udevadm trigger 2>&1 >/dev/null 

if [ -n "$PAUSE" ]; then
	echo -n "${BLUE}Boot time configuration completed. Press <Enter> to continue.${NORMAL}"
	read junk
fi
