 # Set the environment variable below, and the server will refuse to serve clients who don't know the secret.
 secret = process.env.TEXT_AID_TOO_SECRET
 
-# These must be installed via "npm".
-watchr = require "watchr"
-optimist = require "optimist"
-
-fs = require "fs"
-child_process = require "child_process"
+# The first two of these must be installed via "npm".
+for module in [ "watchr", "optimist", "fs", "child_process" ]
+  try
+    global[module] = require module
+  catch
+    console.log "ERROR\n#{module} is not available: sudo npm install -g #{module}"
+    process.exit 1
 
 config =
   port: "9293"
   host: "localhost"
   editor: "urxvt -T textaid -geometry 100x30+80+20 -e vim"
 
-args = optimist.usage("Usage: $0 [--port PORT] [--editor EDITOR-COMMAND]")
-  .alias("h", "help")
-  .default("port", config.port)
-  .default("editor", config.editor)
+defaultEditor =
+  if process.env.TEXT_AID_TOO_EDITOR
+    process.env.TEXT_AID_TOO_EDITOR
+  else
+    config.editor
+
+helpText =
+  """
+  Usage:
+    text-aid-too [--port PORT] [--editor EDITOR-COMMAND]
+
+  Example:
+    export TEXT_AID_TOO_EDITOR="gvim"
+    TEXT_AID_TOO_SECRET=hul8quahJ4eeL1Ib text-aid-too --port 9293
+
+  Environment variables:
+    TEXT_AID_TOO_EDITOR: the editor command to use.
+    TEXT_AID_TOO_SECRET: the shared secret; set this in the extension too.
+
+  """
+
+args = optimist.usage helpText
+  .alias "h", "help"
+  .default "port", config.port
+  .default "editor", defaultEditor
   .argv
-
-console.log args.editor
 
 if args.help
   optimist.showHelp()
   process.exit(0)
 
-console.log "Listening on: ws://#{config.host}:#{args.port}."
+console.log "editor: #{args.editor}"
+console.log "server: ws://#{config.host}:#{args.port}"
+
 WSS  = require("ws").Server
 wss  = new WSS port: args.port, host: config.host
 wss.on "connection", (ws) -> ws.on "message", handler ws

    callback() for callback in onExit.reverse()
    onExit = []

  if secret?
    unless request.secret? and request.secret == secret
      console.log "Mismatched or invalid secret; exiting:", secret, request.secret
      return exit()

    monitor = watchr.watch
      path: filename
      listener: sendText
      catchupDelay: 400
    onExit.push -> monitor.close()

    child = child_process.exec getEditCommand filename
