#!/bin/sh
#
# build: build script for CM bootstrapping
#
#   Copyright (c) 1995 by AT&T Bell Laboratories
#
# author: Matthias Blume (blume@cs.princeton.edu)
#
# options:
#   -arch arch		-- specify the architecture, "heap" is the default.
#   -o image		-- specify the name of the heap image, "sml-cm.ARCH"
#			   is the default.
#   -sml path		-- specify the path to the sml executable, "../bin/sml"
#			   is the default.
#   -yacc path		-- defines the name of the ml-yacc command, "ml-yacc"
#			   is the default.
#   -lex path		-- defines the name of the ml-lex command, "ml-lex"
#			   is the default.
#   -burg path		-- defines the name of the ml-burg command, "ml-burg"
#			   is the default.
#   -L path-list	-- specify a colon separated search path list for
#			   libraries.
#

trap 'rm -f config.sml load-all-tmp; exit 1' 1 2 3 15

CMD="buildcm"


ROOT="sml-cm"
HEAP_IMAGE=""
SML="../../bin/sml"

YACC="ml-yacc"
LEX="ml-lex"
BURG="ml-burg"
RCSCO="co -q"
CTH="12"
NLL="false"
BIN="../../bin"
CMPATH=""

ARCH="UNKNOWN"

#
# process command-line options
#
ARGS=""
while [ "$#" != "0" ]
do
    arg=$1
    shift
    case $arg in
	-arch)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply architecture for -arch option"
		exit 1
	    fi
	    ARCH=$1; shift
	;;
	-yacc)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply name of ML-Yacc for -yacc option"
		exit 1
	    fi
	    YACC=$1; shift
	;;
	-lex)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply name of ML-Lex for -lex option"
		exit 1
	    fi
	    LEX=$1; shift
	;;
	-burg)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply name of ML-Burg for -burg option"
		exit 1
	    fi
	    BURG=$1; shift
	;;
	-rcs)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply RCS check-out command for -rcs option"
		exit 1
	    fi
	    RCSCO=$1; shift
	;;
	-L)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply path spec for -L option"
		exit 1
	    fi
	    CMPATH=$1; shift
	;;
	-cth)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply number for -cth option"
		exit 1
	    fi
	    CTH=$1; shift
	;;
	-nll)
	    NLL="true"
	;;
	-bin)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply bin directory path for -bin option"
		exit 1
	    fi
	    BIN=$1; shift
	;;
	-o)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply image name for -o option"
		exit 1
	    fi
	    HEAP_IMAGE=$1; shift
	;;
	-sml)
	    if [ "$#" = "0" ]; then
		echo "$CMD: must supply path for -sml option"
		exit 1
	    fi
	    SML=$1; shift
	;;
	*)
	    ARGS="$ARGS $arg"
	;;
    esac
done

#
# determine the host architecture (if not supplied already)
#
if [ "$ARCH" = "UNKNOWN" ]; then
  ARCH_N_OPSYS=`$BIN/.arch-n-opsys`
  if [ $? != "0" ]; then
    echo "$CMD: unable to determine architecture/operating system"
    exit 1
  fi
  eval $ARCH_N_OPSYS
fi

#
# split the pathname and build SML string list
#

SPACEPATH=`echo $CMPATH | sed -e 's/:/ /g'`
PATHLIST=""
for i in $SPACEPATH
do
	PATHLIST="$PATHLIST \"$i\" :: "
done
PATHLIST="$PATHLIST []"

#
# generate the config.sml file, with the proper yacc and lex paths, etc.
#

cat - > config.sml <<XXXX
(* config.sml  --- generated by _build_ script *)

structure CmConfig = struct
    val lex = "$LEX"
    val yacc = "$YACC"
    val burg = "$BURG"
    val consolidate_threshold = $CTH
    val namelength_limited = $NLL
    val path = $PATHLIST
    val rcsco = "$RCSCO"
end
XXXX

sed -e 's|config/config.sml|config.sml|' <load-all >load-all-tmp

if [ "$HEAP_IMAGE" = "" ]; then
    HEAP_IMAGE="$ROOT.$ARCH"
fi

$SML $ARGS <<ZZZ
	val env0 = #get Compiler.Environment.topLevelEnvRef ();
	use "load-all-tmp";
	CM.export ("$HEAP_IMAGE", SOME env0);
ZZZ

rm -f config.sml load-all-tmp
exit 0
