#!/bin/ash

# Copyright 2001-2004 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
MYDATE="20040627"


#// 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" ;;
	"MIPS Cobalt"|*RaQ*|*Qube*)	MACHTYPE="Cobalt Microserver" ;;
	*)				MACHTYPE="Unknown MIPS" ;;
esac

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

# Do we set up networking?
if [ ! -f "/tmp/.startup" ]; then
	if ( grep -q telnet /proc/cmdline ); then
		telnet=telnet
	fi
	
	if ( ( grep -q ip= /proc/cmdline ) && ( grep -q gw= /proc/cmdline ) ); then
		ip=$( sed -e 's/^.*\(ip=[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*[^0-9]\).*$/\1/' )
		gw=$( sed -e 's/^.*\(gw=[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*[^0-9]\).*$/\1/' )
		/bin/net-setup $ip $gw $telnet
	elif ( grep -q dhcp /proc/cmdline ); then
		/bin/net-setup dhcp $telnet
	fi
fi

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



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

echo
echo -e "Gentoo Linux; http://www.gentoo.org/"
echo -e " Copyright 2001-2005 Gentoo Foundation; Distributed under the GPL"
echo -e ""
echo -e " Gentoo/MIPS Netboot for ${MACHTYPE} Machines"
echo -e " Build Date: May 9th, 2005"
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"
			putlcd "Telnet: Port 23" "${MYIP}"
			echo -e ""
		fi
	fi

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

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

