#!/bin/ash

# Copyright 2001-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License, v2 or later


#// Path, Kernel Version, Mounts, Hostname
#//--------------------------------------------------------------------------------

PATH=/usr/sbin:/usr/bin:/sbin:/bin

#// Copyright year, Build date in YYYYMMDD format, and in MMDDYYYY to make busybox 'date' happy
CPYYEAR="2005"
MYDATE="20050530"
BBDATE="$(echo ${MYDATE} | cut -c 5-8)$(echo ${MYDATE} | cut -c 1-4)"
DISDATE="$(echo ${MYDATE} | cut -c 7-8) $(echo ${MYDATE} | cut -c 5-6) $(echo ${MYDATE} | cut -c 1-4)"

#// Netboot or CD?
TYPE="netboot"
if [ "${TYPE}" = "cd" ]; then
	RC="4"
	TAG="LiveCD RC${RC}"
else
	TAG="Netboot"
fi

#// Determine kernel minor version
KV_MINOR="$(uname -r | cut -d- -f1 | cut -d. -f2)"
[ "${KV_MINOR}" = "6" ] && MYKV="2.6" || MYKV="2.4"


#// Filesystem tasks
if [ ! -f "/tmp/.startup" ]; then
	#// Mounting
	mount none /proc -t proc				# /proc
	mount none /tmp -t tmpfs -o rw				# /tmp
	mount none /etc -t tmpfs -o rw				# /etc
	mount tmpfs /dev/shm -t tmpfs				# /dev/shm
	mount devpts /dev/pts -t devpts				# /dev/pts
	[ "${MYKV}" = "2.6" ] && mount none /sys -t sysfs	# /sys

	#// Copy /etc data over
	cp	/root/etc/fstab 	/etc			# Filesystem Data
	cp	/root/etc/inittab	/etc			# inittab
	cp	/root/etc/mdadm.conf 	/etc			# mdadm config (RAID)
	cp -d	/root/etc/mtab 		/etc			# mtab (symlink -> /proc/mounts)
	cp	/root/etc/resolv.conf 	/etc			# DNS info
	cp -d	/root/etc/terminfo	/etc			# terminfo (symlink -> /usr/share/terminfo)
fi


#// Hostname
hostname gentoo-mips-${MYDATE}


#// Modified Functions copied from Gentoo's /sbin/functions.sh
#//--------------------------------------------------------------------------------

#// show an informative message (with a newline)
einfo() {
	echo -e " * ${*}"
	return 0
}

#//--------------------------------------------------------------------------------



#// Determine Machine Type
#//--------------------------------------------------------------------------------

MACHINFO="$(cat /proc/cpuinfo | grep "system type" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
CPUINFO="$(cat /proc/cpuinfo | grep "cpu model" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
case "${MACHINFO}" in
	"SGI Indy")			MACHTYPE="SGI Indy" ;;			# Indy R4x00/R5000
	"SGI Indigo2")
		case "${CPUINFO}" in
			R4*)		MACHTYPE="SGI Indigo2" ;;		# I2 R4x00
			R8*)		MACHTYPE="SGI Indigo2 Power" ;;		# I2 R8000
			R10*)		MACHTYPE="SGI Indigo2 Impact" ;;	# I2 R10000
		esac
		;;
	"SGI IP32"|"SGI O2")		MACHTYPE="SGI O2" ;;			# O2 R5K/RM5K2/RM7K/R10K/R12K
	"SGI Octane"|"SGI IP30")	MACHTYPE="SGI Octane" ;;		# Octane R10K/R12K/R14K
	"SGI Origin"|"SGI IP27")	MACHTYPE="SGI Origin" ;;		# Origin R10K/R12K
	"MIPS Cobalt"|*RaQ*|*Qube*)	MACHTYPE="Cobalt Microserver" ;;	# Cobalt Qube/RaQ (1/2)
	*)				MACHTYPE="Unknown MIPS" ;;		# ???
esac

#//--------------------------------------------------------------------------------



#// Discover if the network is already running for us or not, and run telnetd
#//--------------------------------------------------------------------------------

#// This part only gets invoked on initial startup
if [ ! -f "/tmp/.startup" ]; then
	#// If this image is loaded via NFS Root, chances are the network is autoconfigured for us
	if [ ! -z "$(ifconfig | grep "eth0")" ]; then
		MYIP="$(ifconfig | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1 | head -n 1)"

		#// Should we launch telnetd on the specified address?
		for CMDLINE in $(cat /proc/cmdline); do
			case "${CMDLINE}" in
				loadtelnet=*) CMDVALUE="$(echo "${CMDLINE}" | cut -d\= -f2)" ;;
				telnet=*) CMDVALUE="$(echo "${CMDLINE}" | cut -d\= -f2)" ;;
			esac
		done

		if [ "${CMDVALUE}" != "no" -o "${CMDVALUE}" != "false" ]; then
			telnetd -l /linuxrc

			#// Was it launched?
			[ ! -z "$(ps | grep "telnetd")" ] && TELNETDUP="true" || TELNETDUP="false"
		else
			TELNETDUP="false"
		fi		
	fi
fi

#//--------------------------------------------------------------------------------




#//--------------------------------------------------------------------------------

#// For those in the Church of the SubGenius...
BUILDDATE="Build Date: $(date -d ${BBDATE} +"%B %d, %Y")"
for CMDLINE in $(cat /proc/cmdline); do
	if [ "${CMDLINE}" = "discord" ]; then
		BUILDDATE="$(ddate +'Built on %{%A, the %e day of %B%} in the YOLD %Y. %NCelebrate %H!' ${DISDATE})"
	fi
done

#//--------------------------------------------------------------------------------




#// Basic Startup Stuff
#//--------------------------------------------------------------------------------

echo
echo -e "Gentoo Linux; http://www.gentoo.org/"
echo -e " Copyright 2001-${CPYYEAR} Gentoo Foundation; Distributed under the GPL"
echo -e ""
echo -e " Gentoo/MIPS ${TAG} for ${MACHTYPE} Systems"
echo -e " ${BUILDDATE}"
echo -e ""

#// If this is the initial startup, then display some messages, otherwise just execute a shell for the user
if [ ! -f "/tmp/.startup" ]; then
	if [ -z "${MYIP}" ]; then
		einfo "To configure networking, do the following:"
		echo -e ""
		einfo "For Static IP:"
		einfo "/bin/net-setup <IP Address> <Gateway Address> [telnet]"
		echo -e ""
		einfo "For Dynamic IP:"
		einfo "/bin/net-setup dhcp [telnet]"
		echo -e ""
		einfo "If you would like a telnetd daemon loaded as well, pass \"telnet\""
		einfo "As the final argument to /bin/net-setup."
		echo -e ""
	else
		einfo "IP Address: ${MYIP}"
		echo -e ""

		if [ "${TELNETDUP}" = "true" ]; then
			einfo "Telnetd has been launched on IP ${MYIP} on Port 23"
			echo -e ""
		fi
	fi

	#// All Done
	touch /tmp/.startup
else
	/bin/ash
fi
echo -e ""

#//--------------------------------------------------------------------------------

