 # * database
 # Send an error if an application already has same slug.
 action "install", ->
+
     body.slug = slugify body.name
     body.state = "installing"
 
-    setupApp = (appli) ->
-        manager = new AppManager
-
-        console.info 'attempt to install app ' + JSON.stringify(appli)
-        manager.installApp appli, (err, result) ->
-            if err
-                appli.state = "broken"
-                appli.save (saveErr) ->
-                    if saveErr
-                        send_error saveErr.message
-                    else
-                        send
-                            error: true
-                            success: false
-                            message: err.message
-                            app:app
-                            , 201
-            else
+    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 (saveErr) ->
-                    if saveErr
-                        send_error saveErr.message
-                    else
-                        manager.resetProxy (proxyErr) ->
-                            if proxyErr
-                                railway.logger.write "Proxy reset failed."
-                                send_error proxyErr.message
-                            else
-                                send { success: true, app: appli }, 201
-
-    Application.all key: body.slug, (err, apps) ->
-        if err
-            send_error err.message
-        else if apps.length
-            # TODO take care of request retry
-            # (happens when install is taking too long)
-            # may be send a 100 - Continue
-            send_error "There is already an app with similar name", 400
-        else
-            Application.create body, (err, app) ->
-                if err
-                    send_error err.message
-                else
-                    setupApp app
-
+                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


# Helpers

send_error = (msg, code=500) ->
    if msg
        send error: true, msg: msg, code
    else
        send error: true, msg: "Server error occured", code


# Load application corresponding to slug given in params
before 'load application', ->
    Application.all key: params.slug, (err, apps) =>
        if err
            console.log err
            send_error()
        else if apps is null or apps.length == 0
            send error: 'Application not found', 404
        else
            @app = apps[0]
            next()

# * database
action "uninstall", ->

    manager = new AppManager
    manager.uninstallApp @app, (err, result) =>

        return mark_broken @app, err if err

        @app.destroy (err) ->

            return send_error err if err

            manager.resetProxy (err) ->

                return send_error proxyErr if err

                send
                    success: true
                    msg: 'Application succesfuly uninstalled'

# Update an app :
# * haibu, application manager
