~saviq/+junk/open-transition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/sh
CODE_DIR=open_transition
USER=phablet
USER_ID=32011
PASSWORD=phablet
PACKAGE=
BINARY=qmlscene
RUN_OPTIONS="open_transition.qml --desktop_file_hint=/usr/share/applications/qmlscene.desktop"
TARGET_IP=`adb shell ip addr | grep -o inet.*/24 | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'`
SETUP=false
SUDO="echo $PASSWORD | sudo -S"

usage() {
    echo "usage: run_on_device [OPTIONS]\n"
    echo "Script to setup a build environment for the shell and sync build and run it on the phone.\n"
    echo "OPTIONS:"
    echo "  -s, --setup   Setup the build environment"
    echo ""
    echo "IMPORTANT:"
    echo " * Make sure to have the networking and PPAs setup on the device beforehand (phablet-deploy-networking && phablet-ppa-fetch)."
    echo " * Execute that script from a directory containing a branch the shell code."
    exit 1
}

exec_with_ssh() {
    ssh -t $USER@$TARGET_IP "bash -ic \"$@\""
}

exec_with_adb() {
    adb shell chroot /data/ubuntu /usr/bin/env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin "$@"
}
adb_root() {
    adb root
    adb wait-for-device
}

install_ssh_key() {
    ssh-keygen -R $TARGET_IP
    HOME_DIR=/data/ubuntu/home/phablet
    adb push ~/.ssh/id_rsa.pub $HOME_DIR/.ssh/authorized_keys
    adb shell chown $USER_ID:$USER_ID $HOME_DIR/.ssh
    adb shell chown $USER_ID:$USER_ID $HOME_DIR/.ssh/authorized_keys
    adb shell chmod 700 $HOME_DIR/.ssh
    adb shell chmod 600 $HOME_DIR/.ssh/authorized_keys
}

install_dependencies() {
    exec_with_adb apt-get -y install openssh-server
    exec_with_ssh $SUDO apt-get -y install build-essential rsync bzr ccache
    exec_with_ssh $SUDO apt-get -y build-dep $PACKAGE
}

push_code() {
    bzr push bzr+ssh://$USER@$TARGET_IP/~/$CODE_DIR/
    exec_with_ssh bzr checkout $CODE_DIR/
}

sync_code() {
    bzr export --uncommitted --format=dir /tmp/$CODE_DIR
    #rsync --size-only -rlOgoDzv -e ssh /tmp/$CODE_DIR/ $USER@$TARGET_IP:$CODE_DIR/
    rsync -rlOgoDzv -e ssh /tmp/$CODE_DIR/ $USER@$TARGET_IP:$CODE_DIR/
    rm -rf /tmp/$CODE_DIR
}

build() {
    exec_with_ssh "cd $CODE_DIR/ && PATH=/usr/lib/ccache:/opt/qt5/bin:$PATH cmake -DCMAKE_BUILD_TYPE=Debug ."
    exec_with_ssh PATH=/usr/lib/ccache:$PATH make --directory=$CODE_DIR/
}

run() {
    adb shell pkill $BINARY
    exec_with_ssh "cd $CODE_DIR/ && QML_RENDER_TIMING=1 QML_FORCE_THREADED_RENDERER=1 $BINARY $RUN_OPTIONS"
}

set -- `getopt -n$0 -u -a --longoptions="setup,help" "sh" "$@"`

# FIXME: giving incorrect arguments does not call usage and exit
while [ $# -gt 0 ]
do
    case "$1" in
       -s|--setup)   SETUP=true;;
       -h|--help)    usage;;
       --)           shift;break;;
    esac
    shift
done

if $SETUP; then
    echo "Setting up environment for building shell.."
    adb_root
    install_ssh_key
    install_dependencies
    push_code
else
    echo "Transferring code.."
    sync_code
    echo "Building.."
    #build
    echo "Running.."
    adb_root
    run
fi