                 return
 
             process = (access, callback) ->
-                weboob.retrieveAccountsByBankAccess access, callback
+                weboob.retrieveOperationsByBankAccess access, callback
 
             async.each accesses, process, (err) =>
                 if err?
-                    console.log "Error when fetching accounts: #{err}"
+                    console.log "Error when fetching operations: #{err}"
                     return
+                console.log "All accounts have been polled."
+                @timeout = null
+                @prepareNextCheck()
 
-                process = (access, callback) ->
-                    weboob.retrieveOperationsByBankAccess access, callback
+module.exports = accountPoller = new AccountsPoller
 
-                async.each accesses, process, (err) =>
-                    if err?
-                        console.log "Error when fetching operations: #{err}"
-                        return
-                    console.log "All accounts have been polled."
-                    @prepareNextCheck()
+Config.byName 'weboob-installed', (err, found) ->
+    if err? or not found? or found.value isnt 'true'
+        return
+    accountPoller.checkAllAccesses()
 
-
-
-module.exports = new AccountsPoller

        @prepareNextCheck()

    prepareNextCheck: ->
        # day after between 00:00am and 02:00am
        delta = Math.random() * 120 | 0 # opa asm.js style
        now = moment()
        nextUpdate = now.clone().add(1, 'days')
                            .hours(0)
                            .minutes(delta)
                            .seconds(0)


async = require 'async'

BankAccess  = require "../models/bankaccess"
Config      = require "../models/kresusconfig"

weboob = require './weboob-manager'

