#!/bin/sh
#
# Copyright (c) 1994-1996, 1998 University of Utah and the Flux Group.
# All rights reserved.
#
# This file is part of the OSKit, the Flux Operating System Toolkit.  The
# OSKit is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License (GPL), version 2, as published by
# the Free Software Foundation (FSF).  To explore alternate licensing terms,
# contact the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
#
# The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
# received a copy of the GPL along with the OSKit; see the file COPYING.  If
# not, write to the FSF, 59 Temple Place #330, Boston, MA 02111, USA.
#

# This little script uses GNU ld to build a BSD/Mach 32-bit boot image
# from a kernel and a set of boot modules.

bootdir=${BOOTDIR-/usr/flux/lib/boot}
bb=$bootdir/bsdboot.o
as=${AS-i386-mach-as}
ld=${LD-i386-mach-ld}

modules=
outfile=Image
savetemps=
ldopts="-Ttext 100000 -n"

# Parse the command-line options.
until [ $# -eq 0 ]
do
	case "$1" in
		-x ) bb="$2"; shift; shift;;
		-o ) outfile="$2"; shift; shift;;
		-save-temps) savetemps="$1"; shift;;
		* ) modules="$modules $1"; shift;;
	esac
done

# Wrap each of the input files in a .o file.
# At the same time, build an assembly language module
# containing a table describing the boot modules.
echo >$outfile.mods.S ".data; .globl boot_modules,_boot_modules; boot_modules:; _boot_modules:"
files=
for module in $modules; do
	# Split out the associated string, if any.
	file=`echo $module | sed -e 's,:.*$,,'`
	string=`echo $module | sed -e 's,^[^:]*:,,'`
	if test -z "$string"; then string=$file; fi

	# Add this file to the list of files to be included,
	# but only if it hasn't already been seen before
	# (e.g., with a different attached string).
	alreadythere=
	for afile in $files; do
		if [ $file = $afile ]; then alreadythere=yes; fi
	done
	if [ -z $alreadythere ]; then files="$files $file"; fi

	# Convert all non-alphanum chars to underscores for the symbol name.
	# The BFD binary input format will do the same thing
	# to produce the symbol names that it "wraps around" the input files.
	sym_name=`echo $file | sed -e 's,[^a-zA-Z0-9],_,g'`

	# Produce an entry in the module description file.
	echo >>$outfile.mods.S ".long _binary_$sym_name""_start"
	echo >>$outfile.mods.S ".long _binary_$sym_name""_end"
	echo >>$outfile.mods.S ".long 1f"
	echo >>$outfile.mods.S ".data 2"
	echo >>$outfile.mods.S "1: .ascii \"$string\\0\""
	echo >>$outfile.mods.S ".data"
done
echo >>$outfile.mods.S ".long 0; .data; .align 4"

# Assemble the module vector file.
$as -o $outfile.mods.o $outfile.mods.S || exit 1

# Link the BSD boot file and the module vector file with the boot module files.
# Use the binary bfd backend for the input bmod files.
$ld $ldopts -o $outfile $bb $outfile.mods.o \
	-format binary $files -format default \
	|| exit 1

if test -z "$savetemps"; then
	rm -f $outfile.mods.S $outfile.mods.o
fi

exit 0
