#!/usr/bin/env python

import os
import sys
import time
import subprocess

# Use /dev/null to ignore output
dev_null = open('/dev/null', 'w')

# Get the package directory (OS X doesn't have readlink or realpath)
dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) + '/..'

# The command for the server that should show up in the process list
server = [
  'java',
  '-server',
  '-Djava.library.path=' + dir + '/bin',
  '-cp',
  dir + '/nailgun/nailgun.jar:' +
  dir + '/closure-compiler/compiler.jar:' +
  dir + '/bin',
  'com.martiansoftware.nailgun.NGServer'
]

# Start the server if it's not already running
def check_output():
  return subprocess.Popen(['ps','x'], stdout=subprocess.PIPE).communicate()[0]

if check_output().find(' '.join(server)) < 0:
  subprocess.Popen(['nohup'] + server, stdout=dev_null, stderr=dev_null)

# Wait for the server to start up, then run the compiler
for i in range(10):
  # Try to set the current directory
  if subprocess.call([dir + '/nailgun/ng', 'NailgunTest', os.getcwd()], stdout=dev_null, stderr=dev_null):
    # Not ready yet
    time.sleep(1)
  else:
    # Forward the exit code from the compiler
    sys.exit(subprocess.call([dir + '/nailgun/ng', 'com.google.javascript.jscomp.CommandLineRunner'] + sys.argv[1:]))

sys.stderr.write('error: could not connect to the nailgun server, giving up\n')
sys.exit(1)
