#!/bin/ash

# Copyright 2001-2004 Gentoo Technologies, Inc.
# 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
MYDATE="20040426"


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


#// Filesystem mounts
if [ ! -f "/tmp/.startup" ]; then
	mount none /proc -t proc
	mount none /tmp -t tmpfs -o rw
	mount tmpfs /dev/shm -t tmpfs
	mount devpts /dev/pts -t devpts
	[ "${MYKV}" = "2.6" ] && mount none /sys -t sysfs
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
#//--------------------------------------------------------------------------------

CPUINFO="$(cat /proc/cpuinfo | grep "system type" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
case "${CPUINFO}" in
	"SGI IP32"|"SGI O2")		MACHTYPE="SGI O2" ;;
	"SGI Indy"|"SGI Indigo2")	MACHTYPE="SGI Indy/Indigo2" ;;
	"SGI Octane"|"SGI IP30")	MACHTYPE="SGI Octane" ;;
	"SGI Origin"|"SGI IP27")	MACHTYPE="SGI Origin" ;;
	"MIPS Cobalt"|*RaQ*|*Qube*)	MACHTYPE="Cobalt Microserver" ;;
	*)				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)" ;;
			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

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



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

echo
echo -e "Gentoo Linux; http://www.gentoo.org/"
echo -e " Copyright 2001-2004 Gentoo Technologies, Inc.; Distributed under the GPL"
echo -e ""
echo -e " Gentoo/MIPS Netboot for ${MACHTYPE} Machines"
echo -e " Build Date: April 26th, 2004"
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 ""

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

