 From = require('./commands/from')
 functions = require('./dockerfile-functions')
 
+updateExistingLabelOrCreateNew = (label, command, labels) ->
+  matchAndUpdate = (c) ->
+    if c.label is label
+      c.command = command
+      return true
+    return false
+  unless labels.some(matchAndUpdate)
+    labels.push(label: label, command: command)
+
 class Dockerfile
   constructor: ->
     @commands = []
+    @labels = []
   count: -> @commands.length
-  add: (command) ->
+  add: (command, label) ->
     if command.overrides() and @commands.filter((c) -> functions.sameCommand(c, command)).length > 0
       throw new Error("Command already added. Call override() to override it")
+    updateExistingLabelOrCreateNew(label, command, @labels) if label?
     @commands.push(command)
     command.doBefore = (cmd) ->
       cmd.doAfter(@)

        throw new Error("Add/Override this command to Dockerfile first")
      @after = after
      return @
    command.next = (next) ->
      dockerfile.add(next).doAfter(@)
      # Updates the first element in the tight group
      next.doAfter = (after) => @doAfter(after)
      return next
    return command
  override: (command) ->
    unless command.overrides()
      throw new Error("This command does not override")
    for c, i in @commands

        command.after = c.after
        @commands[i] = command
        return command
    @add(command, label)
  build: (dockerfile = [], context = new FinalizingContext(new Pack()))->
    commands = clone(@commands)
    from = commands.filter((c) -> c.constructor == From)[0]
