#!/bin/bash
#to run ensure you have phablet-tools installed along bzr
#sudo apt-get install bzr phablet-tools
#run by passing the branch name you want to install and run on your phablet device
#ie, lp:ubuntu-rssreader-app
#or, lp:droppping-letters
#or, lp:~nskaggs/ubuntu-filemanager-app/my-fix

#set temp paths
tool_path=/tmp/clickbranch/clicktools
branch_path=/tmp/clickbranch
manifest_file="$branch_path/manifest.json"

#arguments
# e.g.; branch=lp:dropping-letters
branch=$1

#grab branch
bzr branch $branch $branch_path

# e.g.; click_package=com.ubuntu.dropping-letters
if [[ 'grep 'name' $manifest_file' ]];then
   click_package=`grep 'name' $manifest_file | awk '{print $2}' | tr -d '"' | tr -d , `
fi

#figure it out from folder structure
test=`ls /tmp/clickbranch/tests/autopilot`

#grab clickbuild tool
bzr branch lp:~sergiusens/+junk/click_ready $tool_path
cd $branch_path

$tool_path/click-build.py --bzr-source $branch

#push to device
adb push $branch_path/*.click /tmp
clickpkgname=`ls /tmp/clickbranch | grep .click`
installcmd="sudo -u phablet pkcon install-local /tmp/$clickpkgname"
adb shell $installcmd

#remove old directories
rm -rf $branch_path

#run it
phablet-config autopilot --dbus-probe enable
phablet-click-test-setup --click $click_package
phablet-test-run -v $test
