 
   ## Interacting with the Levels package ---------------------------------------
 
-  onDidActivateLevels: (callback) ->
-    @emitter.on('did-activate-levels',callback)
-
-  onDidDeactivateLevels: (callback) ->
-    @emitter.on('did-deactivate-levels',callback)
-
-  consumeLevels: ({@languageRegistry}) ->
-    @levelsIsActive = true
-    @emitter.emit('did-activate-levels')
-    new Disposable =>
-      @levelsIsActive = false
-      @emitter.emit('did-deactivate-levels')
+  consumeLevels: ({@languageRegistry,@notificationUtils}) ->
+    if @activated
+      @startUsingLevels()
+    @consumedLevels = true
+    @levelsBinding = new Disposable =>
+      @stopUsingLevels()
+      @consumedLevels = false
 
   startUsingLevels: ->
-    unless @language?
+    unless @usingLevels
       try
-        @language = @languageRegistry.readLanguageSync\
-          (@configFilePath,@executablePath)
-        @dummyGrammar.name = @language.getGrammarName()
-        @dummyGrammar.scopeName = @language.getScopeName()
-        @dummyGrammar.fileTypes = @language.getLevelCodeFileTypes()
-        @language.setDummyGrammar(@dummyGrammar)
-        @setUpLanguageConfigurationManagement()
+        unless @language?
+          @language = @languageRegistry.readLanguageSync\
+            (@configFilePath,@executablePath)
+          @dummyGrammar.name = @language.getGrammarName()
+          @dummyGrammar.scopeName = @language.getScopeName()
+          @dummyGrammar.fileTypes = @language.getLevelCodeFileTypes()
+          @language.setDummyGrammar(@dummyGrammar)
+        atom.grammars.addGrammar(@dummyGrammar)
+        @languageRegistry.addLanguage(@language)
+        @usingLevels = true
+        @onDidStartUsingLevels()
       catch error
-        console.log error
+        atom.notifications.addError \
+          "Failed to load the language from the #{@pkgMetadata.name} package",
+          detail: error.toString(),
+          dismissable: true
+
+  stopUsingLevels: ->
+    if @usingLevels
+      atom.grammars.removeGrammar(@dummyGrammar)
+      @languageRegistry.removeLanguage(@language)
+      @usingLevels = false
+      @onDidStopUsingLevels()
+
+  onDidStartUsingLevels: ->
+    @setUpConfigManagement()
     whitelistPath = path.join(@pkgDirPath,'language','whitelist')
     process.env['LRB_WHITELIST_PATH'] = whitelistPath
-    atom.grammars.addGrammar(@dummyGrammar)
-    @languageRegistry.addLanguage(@language)
 
-  stopUsingLevels: ->
-    atom.grammars.removeGrammar(@dummyGrammar)
+  onDidStopUsingLevels: ->
+    @configSubscrs.dispose()
     delete process.env['LRB_WHITELIST_PATH']
 
   ## Language configuration management -----------------------------------------
 
-  setUpLanguageConfigurationManagement: ->
-    pkgName = @pkgMetadata.name
+  config:
+    rubyInterpreterDirectoryPath:
+      title: 'Ruby Interpreter Directory Path'
+      description:
+        'The path to the directory that contains the Ruby interpreter (a file
+        named `ruby` on Mac OS X/Linux and `ruby.exe` on Windows, respectively).
+        If no path is given, the `PATH` environment variable will be checked
+        automatically.'
+      type: 'string'
+      default: ''
 
-    configKeyPath = "#{pkgName}.generalSettings.levelCodeFileTypes"
-    if (levelCodeFileTypes = atom.config.get(configKeyPath)).length > 0
-      @language.setLevelCodeFileTypes(levelCodeFileTypes)
-    else
-      atom.config.set(configKeyPath,@language.getLevelCodeFileTypes())
-    atom.config.onDidChange configKeyPath, ({newValue}) =>
-      @language.setLevelCodeFileTypes(newValue)
+  setUpConfigManagement: ->
+    @configSubscrs = new CompositeDisposable
 
-    configKeyPath = "#{pkgName}.executionSettings.executionCommandPatterns"
-    if (executionCommandPatterns = atom.config.get(configKeyPath)).length > 0
+    configKeyPath = "#{@pkgMetadata.name}.rubyInterpreterDirectoryPath"
+    if (rubyInterpreterDirectoryPath = atom.config.get(configKeyPath).trim())
+      rubyInterpreterPath = path.join(rubyInterpreterDirectoryPath,'ruby')
+      executionCommandPatterns = ["#{rubyInterpreterPath} <filePath>"]
       @language.setExecutionCommandPatterns(executionCommandPatterns)
     else
-      atom.config.set(configKeyPath,@language.getExecutionCommandPatterns())
-    atom.config.onDidChange configKeyPath, ({newValue}) =>
-      @language.setExecutionCommandPatterns(newValue)
+      @language.setExecutionCommandPatterns(["ruby <filePath>"])
+    @configSubscrs = atom.config.onDidChange configKeyPath, ({newValue}) =>
+      if (rubyInterpreterDirectoryPath = newValue.trim())
+        rubyInterpreterPath = path.join(rubyInterpreterDirectoryPath,'ruby')
+        executionCommandPatterns = ["#{rubyInterpreterPath} <filePath>"]
+        @language.setExecutionCommandPatterns(executionCommandPatterns)
+      else
+        @language.setExecutionCommandPatterns(["ruby <filePath>"])
 
 # ------------------------------------------------------------------------------

      if pkg.name is @pkgMetadata.name
        @dummyGrammar = pkg.grammars[0]
        atom.grammars.removeGrammar(@dummyGrammar)
        @startUsingLevels() if @levelsIsActive
        @onDidActivateLevels => @startUsingLevels()
        @onDidDeactivateLevels => @stopUsingLevels()
        pkgSubscr.dispose()

  deactivate: ->
    @languageRegistry.removeLanguage(@language) if @levelsIsActive

  ## Activation helpers --------------------------------------------------------


{CompositeDisposable,Disposable} = require('atom')
path                             = require('path')
CSON                             = require('season')

# ------------------------------------------------------------------------------

module.exports =

  ## Language package activation and deactivation ------------------------------

  activate: ->
    @pkgDirPath = path.dirname(__dirname)
    pkgMetadataFilePath = CSON.resolve(path.join(@pkgDirPath,'package'))
    @pkgMetadata = CSON.readFileSync(pkgMetadataFilePath)
