#! /bin/sh
# Copyright (C) 2005 Sergey Poznyakoff
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2, or (at
# your option) any later version.
#
# This program 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 GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

PROGNAME=$0
LISTING=
DUMP='2>/dev/null'

usage() {
	cat <<-EOF
	$PROGNAME: assemble and execute MIXAL file.
	usage: $PROGNAME [OPTIONS] FILE

	OPTIONS are:

	-l       Create listing file
	-d       Dump machine state after executing the program to stderr
	-D FILE  Dump machine state after executing the program to FILE
	-h       Display this usage summary
EOF
}

while getopts "dD:hl" OPTION
do
	case $OPTION in
	d)  DUMP='';;
	D)  DUMP="2>$OPTARG";;
	h)  usage
	    exit 0;;
	l)  LISTING=1;;
	*)  echo "Try $PROGNAME -h for more information." >&2
	    exit 1;;
	esac
done

shift $((OPTIND-1))

if [ -n "$LISTING" ]; then
	eval "cat $1 | mixal -l 2>mixal.sym | mixsim $DUMP"
	cat mixal.sym mixal.lst > $1.lst
	rm mixal.sym
else
	eval "mixal $1 | mixsim $DUMP"
fi
