 
 ## Actions
 
-
-# Home page of the application, render browser UI.
-action 'index', ->
-    layout false
-    render title: "Cozy Home"
-
 # Return list of applications available on this cozy instance.
 action 'applications', ->
     Application.all (errors, apps) ->
         if errors
-            send_error "Retrieve applications failed."
+            send_error new Error "Retrieve applications failed."
         else
             send rows: apps
 

# * database
action "uninstall", ->

    markAppAsBroken = =>
        @app.state = "broken"
        @app.save (err) ->
            if err
                send_error()
            else
                send_error "uninstallation failed"

    removeAppFromDb = =>
        @app.destroy (err) ->
            if err
                console.log err
                send_error 'Cannot destroy app'
            else
                manager.resetProxy (err) ->
                    if err
                        send_error 'Reset Proxy failed.'
                    else
                        send
                            success: true
                            msg: 'Application succesfuly uninstalled'

    manager = new AppManager
    manager.uninstallApp @app, (err, result) =>
        if err
            markAppAsBroken()
        else
            removeAppFromDb()

# Update an app :
# * haibu, application manager

# * database
# Send an error if an application already has same slug.
action "install", ->

    body.slug = slugify body.name
    body.state = "installing"

    Application.all key: body.slug, (err, apps) ->

        return send_error err, 500 if err

        if apps.length > 0
            err = new Error "There is already an app with similar name"
            return send_error err, 400


        Application.create body, (err, appli) ->

            return send_error err if err

            console.info 'attempt to install app ' + JSON.stringify(appli)
            manager = new AppManager()
            manager.installApp appli, (err, result) ->

                return mark_broken appli, err if err

                appli.state = "installed"
                appli.port  = result.drone.port
                appli.save (err) ->

                    return send_error err if err

                    manager.resetProxy (err) ->

                        return send_error err if err

                        send success: true, app: appli, 201

# Remove app from 3 places :
# * haibu, application manager
