   # ddp.on 'message', (msg) ->
   #   console.error("ddp message: " + msg)
 
+login.loginWithToken = loginWithToken
+login.loginWithUsername = loginWithUsername
+login.loginWithEmail = loginWithEmail
+login.loginWithAccount = loginWithAccount
+
 module?.exports = login

tryOneEmail = (ddp, options, cb) ->
  userPrompt "Email: ", options, (err, res) ->
    return cb err if err
    attemptLogin ddp, {email: res.account}, res.pass, options, cb

tryOneUser = (ddp, options, cb) ->
  userPrompt "Username: ", options, (err, res) ->
    return cb err if err
    attemptLogin ddp, {user: res.account}, res.pass, options, cb

tryOneEither = (ddp, options, cb) ->
  userPrompt "Account: ", options, (err, res) ->
    return cb err if err
    if isEmail res.account
      attemptLogin ddp, {email: res.account}, res.pass, options, (err, tok) ->
        return cb err, tok unless err and err.error is 400
        attemptLogin ddp, {user: res.account}, res.pass, options, cb
    else
      attemptLogin ddp, {user: res.account}, res.pass, options, cb

#
# When run standalone, the code below will execute

    ddp.call 'login', [{user: user, password: pass}], (err, res) ->
      console.error 'Login failed: ', err.message if err
      return cb err, res?.token

loginWithUsername = (ddp, username, password, options..., cb) ->
   attemptLogin ddp, {user: username}, password, options[0], cb

loginWithEmail = (ddp, email, password, options..., cb) ->
   attemptLogin ddp, {email: email}, password, options[0], cb

loginWithAccount = (ddp, account, password, options..., cb) ->
  if isEmail account
    loginWithEmail ddp, account, password, options[0], (err, tok) ->
      return cb err, tok unless err and err.error is 400
      loginWithUsername ddp, {user: account}, password, options[0], cb
  else
    loginWithUsername ddp, {user: account}, password, options[0], cb

loginWithToken = (ddp, token, cb) ->
  ddp.call 'login', [{ resume: token }], cb

tryOneToken = (ddp, options, cb) ->
  loginWithToken ddp, process.env[options.env], (err, res) ->
    return cb err, res?.token

userPrompt = (prompt, options, cb) ->

