#! /usr/bin/env python

import subprocess
import sys
import os

if len(sys.argv)!=2:
  print('usage: sub_test COMPILER')
  sys.exit(0)

home = ""
if "CHPL_HOME" in os.environ:
  home = os.environ["CHPL_HOME"]

platform = "unknown"
if "CHPL_TARGET_PLATFORM" in os.environ:
  platform = os.environ["CHPL_TARGET_PLATFORM"]
else:
  p = subprocess.Popen([home + "/util/chplenv/chpl_platform.py", "--target"],
                        stdout=subprocess.PIPE)
  myoutput = p.communicate()[0].decode()
  if p.returncode != 0:
    sys.stdout.write("could not run chpl_platform.py")
    sys.exit(1)
  else:
    platform = myoutput


# Skip this test on cygwin -- see JIRA 164
if platform.startswith("cygwin"):
  sys.exit(0)

# Find the base installation
compiler=sys.argv[1]
if not os.access(compiler,os.R_OK|os.X_OK):
  Fatal('Cannot execute compiler \''+compiler+'\'')

# find current directory

def run(source, exe_name, compopts, expect): 
  p = subprocess.Popen([compiler, source] + compopts,
                       stdout=subprocess.PIPE)
  myoutput = p.communicate()[0].decode()
  sys.stdout.write(myoutput)
  if p.returncode != 0:
    sys.stdout.write("[Error matching compiler output for %s (%s)]\n"%(source, exe_name))
    sys.exit(1)
  else:
    sys.stdout.write("[Success compiling %s (%s)]\n"%(source, exe_name))

  returncode = -1
  myoutput = ""

  if os.access(exe_name,os.R_OK|os.X_OK):

    numlocales = "0"
    if "NUMLOCALES" in os.environ:
      numlocales = os.environ['NUMLOCALES']

    if numlocales == "0":
      p = subprocess.Popen(["./%s" % exe_name], stdout=subprocess.PIPE)
    else:
      p = subprocess.Popen(["./%s" % exe_name, "-nl%s" % numlocales],
          stdout=subprocess.PIPE)

    myoutput = p.communicate()[0].decode()
    returncode = p.returncode

  myoutput = myoutput.strip()
  if returncode == 0 and myoutput == expect:
    sys.stdout.write("[Success matching program output for %s (%s)]\n"%(source, exe_name))
  else:
    sys.stdout.write("[Error matching program output for %s (%s)]\n"%(source, exe_name))

  if os.access(exe_name, os.F_OK):
    os.remove(exe_name)
  if os.access(exe_name + "_real", os.F_OK):
    os.remove(exe_name + "_real")


run("test-default-binary-name.chpl", "test-default-binary-name", [], "Hello")
run("test-default-binary-name.chpl", "test_exe", ["-o", "test_exe"], "Hello")
run("test-default-binary-name-main-module.chpl", "test-default-binary-name-main-module", ["--main-module", "M1"], "M1 main")
run("test-default-binary-name-main-module.chpl", "test-default-binary-name-main-module", ["--main-module", "M2"], "M2 main")
run("test-default-binary-name2.chpl", "test-default-binary-name2", [], "Hi\nHello")
run("test-default-binary-name3.chpl", "test-default-binary-name3", [], "Hi")
run("test-default-binary-name3.chpl", "M3", ["M3.chpl"], "Hi\nHello")
run("subdir/testSubDirFile.chpl", "testSubDirFile", [], "Hello")

# finally, test with setting the binary name with the enivronment variable.
# this is last so we don't have to worry about restoring the environment
# variable.
#
os.environ["CHPL_EXE_NAME"] = "test_exe_env"
run("test-default-binary-name.chpl", "test_exe_env", [], "Hello")
