#!/usr/bin/env python

############################################################
# This script is responsible for creating all autogenerated
# .js files from coffee scripts.
############################################################
import os, sys

# Change to the directory containing this file.
path = os.path.split(os.path.realpath(__file__))[0]
os.chdir(path)

version = open("node_modules/salvus_version.js").read().split('=')[1].strip()

def cmd(s):
    #print s
    if os.system(s):
         sys.exit(1)

###########
# Backend server code:
###########
cmd("./scripts/make_coffee " + ' '.join(sys.argv[1:]))

###########
# Code shared between backend servers and web browser clients
###########
#       rm -f static/salvus-*.min.js static/index-*.min.js && \
cmd("""\
      browserify --exports=require,connect node_modules/client_browser.js -o static/salvus.js && \
      uglifyjs2 static/salvus.js -o static/salvus-{version}.min.js --source-map static/salvus-{version}.map.js && \
      cd page &&
      ../scripts/make_coffee $@ temp &&
      cd .. &&
      gen_page $@ && \
      uglifyjs2 static/index.js -o static/index-{version}.min.js --source-map static/index-{version}.map.js
""".format(version = version))

cmd("ls -lh static/salvus.js")

cmd("cp node_modules/message.js  static/salvus/")
