support running apps on device
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c6c7a6b..61053a6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -64,7 +64,6 @@
 )
 qt5_use_modules(cordovaubuntuplugin Widgets Quick)
 
-
 #TODO use subprojects
 file(GLOB_RECURSE PLUGIN_SOURCES src/plugins/*.cpp)
 file(GLOB_RECURSE PLUGIN_HEADERS src/plugins/*.h)
@@ -77,8 +76,7 @@
 )
 qt5_use_modules(coreplugins Widgets Location Sensors Feedback SystemInfo Contacts Multimedia Quick MultimediaWidgets)
 
-find_library(XCB_LIB xcb)
-target_link_libraries(cordova-ubuntu ${XCB_LIB} cordovaubuntuplugin)
+target_link_libraries(cordova-ubuntu cordovaubuntuplugin)
 target_link_libraries(coreplugins cordovaubuntuplugin)
 
 # Qt5's cmake does not export QT_IMPORTS_DIR, lets query qmake on our own for now
diff --git a/bin/build/lib/ubuntu.js b/bin/build/lib/ubuntu.js
index e03b931..a720dac 100644
--- a/bin/build/lib/ubuntu.js
+++ b/bin/build/lib/ubuntu.js
@@ -23,26 +23,165 @@
 var path = require('path');
 var fs = require('fs');
 var assert = require('assert');
+var colors = require('colors');
 
-module.exports.build = function(root_dir, www_dir) {
-    var ubuntu_dir = path.join(root_dir, 'platforms', 'ubuntu');
-    var campo_dir = path.join(ubuntu_dir, 'build');
-    assert.ok(fs.existsSync(ubuntu_dir));
-    assert.ok(fs.existsSync(campo_dir));
+function exec(cmd) {
+    console.log(cmd.green);
 
-    shell.pushd(campo_dir);
+    var res = shell.exec(cmd);
+    if (res.code !== 0) {
+        console.error(cmd.green + " " + "FAILED".underline.red);
+        process.exit(1);
+    }
 
-    shell.exec('cmake . -DCMAKE_INSTALL_PREFIX=".."');
-    shell.exec('make -j 6; make install');
+    return res;
+}
 
+function cp(source, dest) {
+    console.log(('cp -Rf ' + source + ' ' + dest).green);
+
+    if (shell.cp('-r', source, dest) === null) {
+        console.error("FAILED".underline.red);
+        process.exit(1);
+    }
+}
+
+function pushd(dir) {
+    console.log(('pushd ' + dir).green);
+    shell.pushd(dir);
+}
+
+function popd(dir) {
+    console.log(('popd').green);
     shell.popd();
 }
 
-module.exports.run = function(root_dir, www_dir) {
-    var ubuntu_dir = path.join(root_dir, 'platforms', 'ubuntu');
+function buildArmPackage(campoDir, ubuntuDir) {
+    var armhfDir = path.join(ubuntuDir, 'armhf');
 
-    shell.pushd(ubuntu_dir);
-    shell.exec('./cordova-ubuntu www/');
+    shell.rm('-rf', path.join(armhfDir, 'build'));
 
-    shell.popd();
+    var prefixDir = path.join(armhfDir, 'prefix');
+    shell.rm('-rf', prefixDir);
+    shell.mkdir(path.join(armhfDir, 'build'));
+    shell.mkdir(prefixDir);
+
+    pushd(path.join(armhfDir, 'build'));
+
+    exec('click chroot -aarmhf -s trusty run cmake ' + campoDir + ' -DCMAKE_TOOLCHAIN_FILE=/etc/dpkg-cross/cmake/CMakeCross.txt -DCMAKE_INSTALL_PREFIX="' + prefixDir + '"');
+    exec('find . -name AutomocInfo.cmake | xargs sed -i \'s;AM_QT_MOC_EXECUTABLE .*;AM_QT_MOC_EXECUTABLE "/usr/lib/\'$(dpkg-architecture -qDEB_BUILD_MULTIARCH)\'/qt5/bin/moc");\'');
+    exec('click chroot -aarmhf -s trusty run make -j 6');
+    exec('click chroot -aarmhf -s trusty run make install');
+    cp(path.join(ubuntuDir, 'www', '*'), path.join(prefixDir, 'www'));
+    cp(path.join(ubuntuDir, 'qml', '*'), path.join(prefixDir, 'qml'));
+    cp(path.join(ubuntuDir, 'apparmor.json'), prefixDir);
+    cp(path.join(ubuntuDir, 'cordova.desktop'), prefixDir);
+    cp(path.join(ubuntuDir, 'config.xml'), prefixDir);
+
+    var content = JSON.parse(fs.readFileSync(path.join(ubuntuDir, 'manifest.json'), {encoding: "utf8"}));
+    content.architecture = "armhf";
+    fs.writeFileSync(path.join(prefixDir, 'manifest.json'), JSON.stringify(content));
+
+    pushd(prefixDir);
+
+    exec('click build .');
+
+    popd();
+
+    popd();
+}
+
+function buildNative(campoDir, ubuntuDir) {
+    var nativeDir = path.join(ubuntuDir, 'native');
+    var prefixDir = path.join(nativeDir, 'prefix');
+
+    shell.rm('-rf', path.join(nativeDir, 'build'));
+    shell.rm('-rf', prefixDir);
+
+    shell.mkdir(path.join(nativeDir, 'build'));
+    shell.mkdir(prefixDir);
+
+    pushd(path.join(nativeDir, 'build'));
+
+    exec('cmake ' + campoDir + ' -DCMAKE_INSTALL_PREFIX="' + prefixDir + '"');
+    exec('make -j 6; make install');
+
+    cp(path.join(ubuntuDir, 'config.xml'), prefixDir);
+    cp(path.join(ubuntuDir, 'www', '*'), path.join(prefixDir, 'www'));
+    cp(path.join(ubuntuDir, 'qml', '*'), path.join(prefixDir, 'qml'));
+
+    popd();
+}
+
+module.exports.build = function(rootDir, wwwDir) {
+    var ubuntuDir = path.join(rootDir, 'platforms', 'ubuntu');
+    var campoDir = path.join(ubuntuDir, 'build');
+
+    assert.ok(fs.existsSync(ubuntuDir));
+    assert.ok(fs.existsSync(campoDir));
+
+    buildArmPackage(campoDir, ubuntuDir);
+    buildNative(campoDir, ubuntuDir);
+}
+
+function runNative(rootDir) {
+    var ubuntuDir = path.join(rootDir, 'platforms', 'ubuntu');
+    var nativeDir = path.join(ubuntuDir, 'native');
+
+    pushd(path.join(nativeDir, 'prefix'));
+    exec('./cordova-ubuntu www/');
+
+    popd();
+}
+
+function isDeviceAttached() {
+    var res = exec('adb get-state');
+
+    if (res.output.indexOf('device') == -1)
+        return false;
+
+    res = exec('adb shell uname -a');
+    if (res.output.indexOf('ubuntu-phablet') == -1)
+        return false;
+
+    return true;
+}
+
+function runOnDevice(rootDir) {
+    var ubuntuDir = path.join(rootDir, 'platforms', 'ubuntu');
+
+    if (!isDeviceAttached()) {
+        console.error('UbuntuTouch device is not attached'.red)
+        process.exit(1);
+    }
+
+    var armhfDir = path.join(ubuntuDir, 'armhf');
+    var prefixDir = path.join(armhfDir, 'prefix');
+
+    pushd(prefixDir);
+
+    var manifest = JSON.parse(fs.readFileSync(path.join(ubuntuDir, 'manifest.json'), {encoding: "utf8"}));
+    var appId = manifest.name;
+
+    var names = shell.ls().filter(function (name) {
+        return name.indexOf(appId) == 0 && name.indexOf('.click');
+    });
+
+    assert.ok(names.length == 1);
+
+    exec('adb push ' + names[0] + ' /home/phablet');
+    exec('adb shell "cd /home/phablet/; click install ' + names[0] + ' --user=phablet"');
+
+    exec('adb shell "su - phablet -c \'cd /opt/click.ubuntu.com/' + appId + '/current; ./cordova-ubuntu www/ --desktop_file_hint=/opt/click.ubuntu.com/' + appId + '/current/cordova.desktop\'"');
+
+    popd();
+
+    console.log('have fun!'.rainbow);
+}
+
+module.exports.run = function(rootDir, desktop) {
+    if (desktop)
+        runNative(rootDir);
+    else
+        runOnDevice(rootDir);
 }
diff --git a/bin/build/run b/bin/build/run
index 76b1a1a..8f5ba2f 100755
--- a/bin/build/run
+++ b/bin/build/run
@@ -25,5 +25,5 @@
 var www = path.join(root, 'www');
 
 platform.build(root, www);
-platform.run(root, www);
 
+platform.run(root, process.argv.indexOf('device') != -1);
diff --git a/bin/check_reqs b/bin/check_reqs
index dd84963..01e2f01 100755
--- a/bin/check_reqs
+++ b/bin/check_reqs
@@ -1,32 +1,53 @@
-#! /bin/sh
+#!/usr/bin/env node
 
-#
-# Copyright 2013 Canonical Ltd.
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-#  KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
+/*
+ *
+ * Copyright 2013 Canonical Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
 
+var exec = require('child_process').exec;
 
-#### CHECK FOR UBUNTU-SDK
-set PACKAGE_LIST="cmake debhelper libx11-dev libicu-dev pkg-config qtbase5-dev qtchooser qtdeclarative5-dev qtfeedback5-dev qtlocation5-dev qtmultimedia5-dev qtpim5-dev qtsensors5-dev qtsystems5-dev"
-dpkg-query -Wf'$${db:Status-abbrev}' ${PACKAGE_LIST} 2>/dev/null | grep -q '^i'
-if [ $? != 0 ]; then
-	echo "missing dependency ${PACKAGE_LIST}"
-	exit 1
-fi
+// FIXME: check for armhf
+var deps = "cmake libicu-dev pkg-config qtbase5-dev qtchooser qtdeclarative5-dev qtfeedback5-dev qtlocation5-dev qtmultimedia5-dev qtpim5-dev qtsensors5-dev qtsystems5-dev";
+var depsArm = "cmake libicu-dev:armhf pkg-config qtbase5-dev:armhf qtchooser qtdeclarative5-dev:armhf qtfeedback5-dev:armhf qtlocation5-dev:armhf qtmultimedia5-dev:armhf qtpim5-dev:armhf qtsensors5-dev:armhf qtsystems5-dev:armhf";
+var series = "trusty";
+exec("dpkg-query -Wf'$${db:Status-abbrev}' click " + deps, function(error, stdout, stderr) {
+    if (error) {
+        console.error("Error: missing dependency " + deps);
+        process.exit(error.code);
+    }
+
+    exec("dpkg-query -Wf='${Version}' click", function(error, stdout, stderr) {
+        //FIXME:
+        if (error || stdout != "0.4.13") {
+            console.error("Error: click package too old");
+            process.exit(error.code);
+        }
+
+        exec("click chroot -aarmhf -s " + series + " run dpkg-query -l " + deps, function(error, stdout, stderr) {
+            if (error) {
+                console.error("Error: missing armhf chroot");
+// to fix problem click chroot -aarmhf -s trusty maint
+// dpkg --force-overwrite --force-conflicts -i /var/cache/apt/archives/qtbase5-dev_5.0.2+dfsg1-7ubuntu13_amd64.deb
+// apt-get install -f
+                console.error("run:\nclick chroot -aarmhf -s " + series + " create\n click chroot -aarmhf -s " + series + " install " + depsArm);
+                process.exit(error.code);
+            }
+        }
+    })
+}
 
diff --git a/bin/create b/bin/create
index bb2637c..17397fe 100755
--- a/bin/create
+++ b/bin/create
@@ -41,6 +41,8 @@
         }
         shell.mkdir(project_path);
         shell.cp('-r', path.join(ROOT, '*'), path.join(project_path, 'build'));
+        shell.mkdir(path.join(project_path, 'armhf'));
+        shell.mkdir(path.join(project_path, 'native'));
         shell.cp('-r', path.join(ROOT, 'bin/build/*'), path.join(project_path, 'cordova'));
 
         shell.cp('-r', path.join(ROOT, 'bin/node_modules'), path.join(project_path, 'cordova'));
@@ -53,6 +55,7 @@
     try {
         require.resolve("shelljs");
         require.resolve("elementtree");
+        require.resolve("colors");
         create_project();
     } catch(e) {
         console.log('Shelljs module was not found, running \'npm install\'.....');
@@ -60,7 +63,7 @@
         var cwd = process.cwd();
 
         var block = true;
-        exec('npm install shelljs@0.2 elementtree', {cwd: __dirname}, function (error, stdout, stderr) {
+        exec('npm install', {cwd: __dirname}, function (error, stdout, stderr) {
             block = false;
             if (error !== null) {
                 console.error('ERROR : running \'npm install\' is npm installed? ' + error);
diff --git a/bin/package.json b/bin/package.json
index 28d2045..956d442 100644
--- a/bin/package.json
+++ b/bin/package.json
@@ -4,8 +4,8 @@
     "version": "0.0.0",
     "homepage": "https://launchpad.net/cordova-ubuntu/",
     "repository": {
-        "type": "bzr",
-        "url": "lp:cordova-ubuntu"
+        "type": "git",
+        "url": "https://git-wip-us.apache.org/repos/asf?p=cordova-ubuntu.git"
     },
     "keywords": [
         "cli",
@@ -18,7 +18,9 @@
         "node": ">=0.10.0"
     },
     "dependencies": {
-        "shelljs" : "0.1.4"
+        "shelljs": "0.2.6",
+        "elementtree": "*",
+        "colors": "0.6.2"
     },
     "devDependencies": {
     },