#!/usr/bin/env python

import subprocess
from sys import argv, exit
import os

def bash(cmdstr):
  print cmdstr
  p = subprocess.Popen(["/bin/bash", "-c", cmdstr])
  p.communicate() # wait for the process to end

ENTRYPOINT = "src/main.c"
OFILE_C = "./out/a"
OFILE_EM = "out/index.html"
SHELL_FILE = "src/shell.html"
HEADERS = "src/headers.h"

MAKEHEADERS = "makeheaders -h src/*.c > {HEADERS}".format(**locals())
COMPILE_C = "clang {ENTRYPOINT} -std=c99 -o {OFILE_C} -Wall -I ~/Library/Frameworks/SDL2.framework/Headers -I ~/Library/Frameworks/SDL2_image.framework/Headers -I ~/Library/Frameworks/SDL2_mixer.framework/Headers -I ~/Library/Frameworks/SDL2_ttf.framework/Headers -F ~/Library/Frameworks -framework SDL2 -framework SDL2_Image -framework SDL2_Mixer -framework SDL2_TTF".format(**locals())
COMPILE_EM = "emcc {ENTRYPOINT} -o {OFILE_EM} -Wall --preload-file data --use-preload-plugins --shell-file {SHELL_FILE} -s WASM=1 -s EXIT_RUNTIME=1 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 -s TOTAL_MEMORY=512MB".format(**locals())
RUN_C = "DEBUG=1 {OFILE_C}".format(**locals())
CLEAN = "trash out/* && trash {HEADERS} && ls -l out/".format(**locals())

options = {
  "all": "{MAKEHEADERS} && {COMPILE_C} && {RUN_C}".format(**locals()),

  "clean": "{CLEAN}".format(**locals()),
  "compile": "{MAKEHEADERS} && {COMPILE_C}".format(**locals()),
  "emscripten": "{COMPILE_EM}".format(**locals()),
  "headers": "{MAKEHEADERS}".format(**locals()),
  "lint": "{COMPILE_C} --analyze".format(**locals()),
  "run": "{RUN_C}".format(**locals()),

  "dryrun": 0,  # handled specially
}


def matchPrefix(prefix, options):
  prefix = prefix.lower().strip()
  if prefix in options:
    return prefix
  foundOpt = None
  for opt in options:
    if opt.startswith(prefix):
      if foundOpt is None:
        foundOpt = opt
      else:
        raise Exception("Multiple options match prefix '%s' ('%s', '%s', maybe others)"%(prefix, foundOpt, opt))
  return foundOpt


if len(argv) > 3:
  print "Usage: ./build [{}]".format('|'.join(options.keys()))
  exit(1)
if len(argv) == 1:
  prefix = "all"
else:
  prefix = argv[1]

match = matchPrefix(prefix, options.keys())
if match is None:
  raise Exception("No matching option found")
else:
  # print "Running '%s'"%match
  if match == "emscripten" and not os.getenv("EMSDK"):
    print "need to load emcc:"
    print "\tsource ~/projects/emsdk/emsdk_env.sh"
    exit(1)
  if match == "dryrun":
    if len(argv) == 2:
      match = "all"
    else:
      match = matchPrefix(argv[2], options.keys())
    cmdstr = options[match]
    print cmdstr
  else:
    cmdstr = options[match]
    bash(cmdstr)
