 
 # Initialize the persisted managed templates.
 initTemplates = ->
-  store.init 'templates', []
   initTemplates_update()
   store.modify 'templates', (templates) ->
     # Initialize all default templates to ensure their properties are as

# Handle the conversion/removal of older version of settings that may have been
# stored previously by `initUrlShorteners`.
initUrlShorteners_update = ->
  store.modify 'updates', (updates) ->
    updates.shorteners ?= []
    # Check if the URL shorteners need updated for 0.1.0.0.
    if '0.1.0.0' not in updates.shorteners
      # Update the URL shorteners for 0.1.0.0.
      store.rename 'bitlyEnabled',       'bitly',         off
      store.rename 'bitlyXApiKey',       'bitlyApiKey',   ''
      store.rename 'bitlyXLogin',        'bitlyUsername', ''
      store.rename 'googleEnabled',      'googl',         on
      store.rename 'googleOAuthEnabled', 'googlOAuth',    on
      # Ensure that URL shorteners are not updated for 0.1.0.0 again.
      updates.shorteners.push '0.1.0.0'
    # Check if the URL shorteners need updated for 1.0.0.
    if '1.0.0' not in updates.shorteners
      # Update the URL shorteners for 1.0.0.
      store.set 'bitly',
        apiKey:    store.get('bitlyApiKey') ? ''
        enabled:   store.get('bitly') ? no
        username:  store.get('bitlyUsername') ? ''
      store.remove 'bitlyApiKey', 'bitlyUsername'
      store.set 'googl',
        enabled: store.get('googl') ? yes
        oauth:   store.get('googlOAuth') ? yes
      store.remove 'googlOAuth'
      yourls = store.get 'yourls'
      store.set 'yourls',
        enabled:   if typeof yourls is 'boolean' then yourls else no
        password:  store.get('yourlsPassword') ? ''
        signature: store.get('yourlsSignature') ? ''
        url:       store.get('yourlsUrl') ? ''
        username:  store.get('yourlsUsername') ? ''
      store.remove 'yourlsPassword', 'yourlsSignature', 'yourlsUrl',
                   'yourlsUsername'
      # Ensure that URL shorteners are not updated for 1.0.0 again.
      updates.shorteners.push '1.0.0'

# URL shortener functions
# -----------------------

# Handle the conversion/removal of older version of settings that may have been
# stored previously by `initTemplates`.
initTemplates_update = ->
  # Create updater for the `features` namespace and then rename it to
  # `templates`.
  updater = new utils.Updater 'features'
  updater.rename 'templates'
  # Define the processes for all required updates to the `templates` namespace.
  updater.update '0.1.0.0', ->
    store.rename 'copyAnchorEnabled',  'feat__anchor_enabled',  yes
    store.rename 'copyAnchorOrder',    'feat__anchor_index',    2
    store.rename 'copyBBCodeEnabled',  'feat__bbcode_enabled',  no
    store.rename 'copyBBCodeOrder',    'feat__bbcode_index',    4
    store.rename 'copyEncodedEnabled', 'feat__encoded_enabled', yes
    store.rename 'copyEncodedOrder',   'feat__encoded_index',   3
    store.rename 'copyShortEnabled',   'feat__short_enabled',   yes
    store.rename 'copyShortOrder',     'feat__short_index',     1
    store.rename 'copyUrlEnabled',     'feat__url_enabled',     yes
    store.rename 'copyUrlOrder',       'feat__url_index',       0
  updater.update '0.2.0.0', ->
    names = store.get('features') ? []
    for name in names when typeof name is 'string'
      store.rename "feat_#{name}_template", "feat_#{name}_content"
      image = store.get "feat_#{name}_image"
      switch typeof image
        when 'string'
          if image in ['', 'spacer.gif', 'spacer.png']
            store.set "feat_#{name}_image", 0
          else
            for img, i in ext.IMAGES
              oldImg = img.replace /^tmpl/, 'feat'
              if "#{oldImg}.png" is image
                store.set "feat_#{name}_image", i + 1
                break
        else store.set "feat_#{name}_image", 0
  updater.update '1.0.0', ->
    names              = store.remove('features') ? []
    templates          = []
    toolbarFeatureName = store.get 'toolbarFeatureName'
    for name in names when typeof name is 'string'
      image = store.remove("feat_#{name}_image") ? 0
      image--
      if image < 0
        image = ''
      else
        for img, i in ext.IMAGES when i is image
          image = img
          break
        image = '' if typeof image isnt 'string'
      key = ext.getKeyForName name
      if toolbarFeatureName is name
        if store.exists 'toolbar'
          store.modify 'toolbar', (toolbar) ->
            toolbar.key = key
        else
          store.set 'toolbar', key: key
      templates.push
        content:  store.remove("feat_#{name}_content")  ? ''
        enabled:  store.remove("feat_#{name}_enabled")  ? yes
        image:    image
        index:    store.remove("feat_#{name}_index")    ? 0
        key:      key
        readOnly: store.remove("feat_#{name}_readonly") ? no
        shortcut: store.remove("feat_#{name}_shortcut") ? ''
        title:    store.remove("feat_#{name}_title")    ? i18n.get 'untitled'
        usage:    0
    store.set 'templates', templates
    store.remove store.search(
      /^feat_.*_(content|enabled|image|index|readonly|shortcut|title)$/
    )...

# Initialize the settings related to the toolbar/browser action.
initToolbar = ->
  initToolbar_update()
  store.modify 'toolbar', (toolbar) ->
    toolbar.close   ?= yes
