# -*- coding: utf-8 -*-

# Copyright (c) 2008-2010 Kent State University
#
# This file is distributed under the MIT License. See the accompanying file
# LICENSE.txt or http://www.opensource.org/licenses/mit-license.php for terms
# and conditions.

import sys
import os
from testing import *

parse_args()

# Find the output name of the program.
# FIXME: This assumes that output is given as -o, when it might also be
# given as --output (I think).
output = None
for n, i in enumerate(cmd_args):
  if i[:2] == "-o":
    if len(i) == 2:
      output = cmd_args[n + 1]
    else:
      output = i[2:]

# Compile the program
ret = run_program("compile", cmd_args)
if ret != 0:
  # The build failed, so we should exit. Note that failing with an error
  # would cause the overall build to fail. We want the build to continue
  # after failure.
  # However, for convenience, we'll dump the error to stderr.
  sys.stderr.write(stage_cmd("compile") + "\n")
  sys.stderr.write(stage_err("compile"))
  exit_failure()

# Now, try to run the program
ret = run_program("run", [output])
if ret != 0:
  # As before, dump the error to the terminal and finish (successfully!)
  sys.stderr.write(stage_err("run"))
  exit_failure()

exit_success()