1#! /bin/bash
2#This script does a clean build of PMC and install it to your device through adb
3#Requires envsetup.sh in the branch
4
5#function to change jdk version
6function setup_jdk() {
7  # Remove the current JDK from PATH
8  if [ -n "$JAVA_HOME" ] ; then
9    PATH=${PATH/$JAVA_HOME\/bin:/}
10  fi
11  export JAVA_HOME=$1
12  export PATH=$JAVA_HOME/bin:$PATH
13}
14
15#Color code for echo
16r='\e[0;31m'
17brn='\e[0;33m'
18y='\e[1;33m'
19g='\e[0;32m'
20cy='\e[0;36m'
21lb='\e[1;34m'
22p='\e[0;35m'
23lg='\e[0;37m'
24NC='\e[0m' # No Color
25
26echo -e "Welcome to ${r}U${brn}N${y}I${g}C${lb}O${cy}R${p}N${NC} build system for ${g}PMC${NC}"
27
28IFS='_' read -a array <<< "$TARGET_PRODUCT"
29export TP=${array[0]}
30if [[ ${#array[@]} -eq 2 ]]; then
31  export TP=${array[1]}
32fi
33
34APP_NAME=PMC
35APP_PACKAGE_NAME=com.android.pmc
36
37BRANCH_ROOT=$PWD/../../../../..
38PMC_PROJ=$BRANCH_ROOT/vendor/google_testing/comms/Tools/PMC
39SHARED_LIB_JAR_ROOT=$BRANCH_ROOT/out/target/common/obj/JAVA_LIBRARIES
40APP_JAR_ROOT=$BRANCH_ROOT/out/target/common/obj/APPS
41APK_ROOT=$BRANCH_ROOT/out/target/product/$TP/system/priv-app/PMC
42
43function pmc_build {
44
45echo -e "${y}Removing intermeidates of the app${NC}"
46rm -r $APP_JAR_ROOT/"$APP_NAME"_intermediates
47#Remove the apk file
48rm $APK_ROOT/"$APP_NAME".apk
49
50#Build all the dependency libs
51. $BRANCH_ROOT/build/envsetup.sh
52
53exec () {
54  ${@:1:($#-1)}
55  if [ $? -ne 0 ]; then
56    echo -e "${r}Encountered error when ${@:$#}${NC}"
57    echo -e "${lg}UNICORN ${r}DIED${NC}!"
58    exit 1
59  fi
60}
61
62echo -e "${lb}+++++++ Building $APP_NAME.apk +++++++${NC}"
63cd $PMC_PROJ
64exec mm -B "building $APP_NAME.apk"
65echo
66
67}
68
69function pmc_flash {
70
71echo -e "${y}Switching to root${NC}"
72adb root
73adb wait-for-device remount
74
75echo -e "${y}Uninstalling old apk from device${NC}"
76adb uninstall $APP_PACKAGE_NAME
77adb shell rm -r /system/priv-app/$APP_NAME.apk
78
79echo -e "${lb}Installing apk to device${NC}"
80cd $APK_ROOT
81#exec adb install $APP_NAME.apk "installing apk to device"
82#exec adb push $APP_NAME.apk /system/priv-app "installing apk to previliged dir"
83exec adb install -r $APP_NAME.apk "installing apk to previliged dir"
84
85}
86
87DO_BUILD=1
88DO_FLASH=1
89
90if [ $# -ne 0 ] ; then
91  DO_BUILD=0
92  DO_FLASH=0
93  while getopts "bf" ARG
94  do
95    case $ARG in
96      b) DO_BUILD=1 && echo "Build it we will.";;
97      f) DO_FLASH=1 && echo "Flash it we must.";;
98      ?) echo "Invalid Argument ${ARG}" && exit 1;;
99    esac
100  done
101fi
102
103if [ ${DO_BUILD} -eq "1" ] ; then
104  pmc_build
105fi
106
107if [ ${DO_FLASH} -eq "1" ] ; then
108  pmc_flash
109fi
110
111echo "All clear!"
112echo -e " ${r}U${brn}N${y}I${g}C${cy}O${lb}R${p}N ${r}P${brn}O${y}W${g}E${cy}R${lb}!${p}!${NC}"
113
114