#!/bin/sh -e
#
# arcload install script, based on the arcboot install script
#
# Copyright (C) 2007 Julien BLACHE <jblache@debian.org>
#
# Released under the GPL.
#

function dev_to_scsi()
{
	maj=$(echo $1 | cut -b1-2 | sed -e s/^0//)
	min=$(echo $1 | cut -b3-4 | sed -e s/^0//)

	dev=$maj:$(($min / 16))

	for disk in /sys/block/sd*; do
		bdev=$(cat $disk/dev)
		if [ "$bdev" = "$dev" ]; then
			scsi=$(readlink $disk/device)
			break
		fi
	done

	# Add the partition number
	scsi=$scsi:$((($min % 16) - 1))

	echo $(basename "$scsi")
}


DVHTOOL=/usr/sbin/dvhtool
STAT=/usr/bin/stat

ARCLOAD_LIB=/usr/lib/arcload
ARCLOAD32=$ARCLOAD_LIB/arcload.ecoff
ARCLOAD64=$ARCLOAD_LIB/arcload64


if [ -z "$1" ]; then
	echo "Usage: arcload <name_of_disk>"
	exit 1
fi

if [ ! -x $DVHTOOL ]; then
	echo "Can't find dvhtool - giving up!"
	exit 1
fi

DEVICE="$1"

# Detect system type
SYSTYPE=$(grep -m 1 "system type" /proc/cpuinfo | sed -e "s/: /:/g" | cut -d":" -f2)
CPUTYPE=$(grep -m 1 "cpu model" /proc/cpuinfo | sed -e "s/: /:/g" | cut -d":" -f2 | cut -d' ' -f1)

case "$SYSTYPE" in
	"SGI Indy")
		SWIP=IP22
		;;
	"SGI Indigo2")
		# The R10k I2 is an IP28
		if [ "$CPUTYPE" = "R10000" ]; then
			SWIP=IP28
		else
			SWIP=IP22
		fi
		;;
	"SGI Origin"|"SGI IP27")
		SWIP=IP27
		;;
	"SGI Octane"|"SGI IP30")
		SWIP=IP30
		;;
	"SGI O2"|"SGI IP32")
		SWIP=IP32
		;;
	*)
		echo "Unknown system type: $SYSTYPE ($CPUTYPE)"
		exit 1
		;;
esac

# Pick the appropriate arcload variant
case "$SWIP" in
	"IP22"|"IP32")
		ARCLOAD=$ARCLOAD32
		;;
	"IP27"|"IP28"|"IP30")
		ARCLOAD=$ARCLOAD64
		;;
	*)
		echo "Unknown platform: $SWIP"
		exit 1
		;;
esac

echo "Selected $ARCLOAD for an $SYSTYPE ($CPUTYPE) $SWIP"

# Install the bootloader into the volume header
echo
echo -n "Putting `basename $ARCLOAD` into the volume header of $1..."
$DVHTOOL -d "$DEVICE" --unix-to-vh $ARCLOAD arcload
if [ $? = 0 ]; then
	echo " done."
else
	echo "FAILED!"
	echo "dvhtool failed, please investigate."
	echo "*** YOUR MACHINE MAY NOT REBOOT UNLESS YOU FIX THE PROBLEM ***"
	exit 1
fi


# Check for an arcload config file
if [ ! -f /etc/arc.cf ] && [ ! -f /arc.cf ]; then
	cat << EOF
/etc/arc.cf not found; you need to write a config file for arcload.
See /usr/share/doc/arcload/README.Debian, the examples and the
arcload documentation.
EOF
fi


# Use the version string embedded into the binary
ARCLOAD_VER=$(grep -a -m 1 Debian $ARCLOAD | cut -d')' -f1)")"

cat << EOF

$ARCLOAD_VER installed into the volume header of $DEVICE

You need to set the following PROM variables to the given values at the
next reboot; unless they're already set to the proper values, your
machine won't boot. To do so, at the next boot, press Esc at the prompt
to enter the System Maintenance Menu, then select option 5 to enter Command
Monitor. Enter the following commands at the PROM prompt (>>):
EOF

DVH=$(dev_to_scsi $($STAT -c "%02t%02T" $DEVICE))
DVHCTL=$(echo $DVH | cut -d':' -f1)
#DVHBUS=$(echo $DVH | cut -d':' -f2)
DVHID=$(echo $DVH | cut -d':' -f3)
#DVHLUN=$(echo $DVH | cut -d':' -f4)
DVHPART=8

ROOT=$(dev_to_scsi $(printf %04d $($STAT -c "%D" /)))
ROOTCTL=$(echo $ROOT | cut -d':' -f1)
#ROOTBUS=$(echo $ROOT | cut -d':' -f2)
ROOTID=$(echo $ROOT | cut -d':' -f3)
#ROOTLUN=$(echo $ROOT | cut -d':' -f4)
ROOTPART=$(echo $ROOT | cut -d':' -f5)
# ext2 comes out as ext2/ext3
ROOTFS=$($STAT -f -c "%T" / | cut -d'/' -f1)


# Build the ARCS paths for the volume header and root partition
#
# I chose to use the old-style dksc() path specification because it's supported
# everywhere, it's easier to build from Linux and it's shorter.
#
# The scsi()disk()rdisk()partition() form should be supported everywhere too,
# but it's longer to write and hence is error-prone when it needs to be put
# on paper. Some systems may also require prepending something like xio()pci()
# which, again, can't be easily determined from Linux (though those systems
# should be happy with just scsi()... too).
#
# Origin systems support an additional path specification, though it differs
# slightly between the Origin systems (200, 2000, 300, 3000), that depends on
# the hardware graph and can't be built easily from Linux.
#
# The dksc() notation can only be used to address devices connected to the
# master BASEIO board on multi-module systems. This is the only limitation, but
# it really shouldn't matter much in our case.
#
# See: <http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0620&db=man&fname=/usr/share/catman/a_man/cat1/prom.z>
case "$SWIP" in
#	"IP22")
#		SystemPartition="scsi($DVHCTL)disk($DVHID)rdisk($DVHLUN)partition($DVHPART)"
#		OSLoadPartition="scsi($ROOTCTL)disk($ROOTID)rdisk($ROOTLUN)partition($ROOTPART)[$ROOTFS]"
#		;;
#	"IP27")
#		SystemPartition="dksc($DVHCTL,$DVHID,$DVHPART)"
#		OSLoadPartition="dksc($ROOTCTL,$ROOTID,$ROOTPART)[$ROOTFS]"
#		;;
#	"IP28")
#		;;
#	"IP30")
#		;;
#	"IP32")
#		;;
	*)
		SystemPartition="dksc($DVHCTL,$DVHID,$DVHPART)"
		OSLoadPartition="dksc($ROOTCTL,$ROOTID,$ROOTPART)[$ROOTFS]"
		;;
esac

cat << EOF

  >> setenv SystemPartition $SystemPartition
  >> setenv OSLoader arcload
  >> setenv OSLoadPartition $OSLoadPartition
  >> setenv OSLoadFilename <depends on your arc.cf>

The value for OSLoadFilename depends on your arcload configuration.

For the machine to boot automatically, set the AutoLoad variable to yes

You can check the PROM variables settings with the printenv command.
When done, type exit and choose option 1 Start System to boot the machine.
EOF

