1#!/bin/bash
2###############################################################################
3# Copyright 2015 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7###############################################################################
8#
9# ios_setup.sh: Sets environment variables used by other iOS scripts.
10
11# File system location where we mount the ios devices.
12IOS_MOUNT_POINT="/tmp/mnt_iosdevice"
13
14# Location on the ios device where all data are stored. This is
15# relative to the mount point.
16IOS_DOCS_DIR="Documents"
17
18# Temporary location to assemble the app into an .ipa package.
19IOS_PCKG_DIR="/tmp/ios_pckg"
20
21# Directory with the Skia source.
22SKIA_SRC_DIR=$(cd "${SCRIPT_DIR}/../../.."; pwd)
23
24# Provisioning profile - this needs to be set up on the local machine.
25PROVISIONING_PROFILE=""
26
27# Code Signing identity - this needs to be set up on the local machine.
28CODE_SIGN_IDENTITY="iPhone Developer"
29
30IOS_RESULTS_DIR="results"
31
32# BUILDTYPE is 'Debug' by default.
33if [[ -z "$BUILDTYPE" ]]; then
34  BUILDTYPE="Debug"
35fi
36
37# Out dir is $SKIA_SRC_DIR/out by default.
38if [[ -z "$SKIA_OUT" ]]; then
39  SKIA_OUT="$SKIA_SRC_DIR/out"
40fi
41
42# Location of XCode build products.
43if [[ -z "$XCODEBUILD" ]]; then
44  XCODEBUILD="${SKIA_SRC_DIR}/xcodebuild"
45fi
46
47# Name of the iOS app.
48IOS_APP=iOSShell.ipa
49
50# Location of the compiled iOS code.
51IOS_OUT=${XCODEBUILD}/${BUILDTYPE}-iphoneos
52
53# Location of the compiled iOS app.
54IOS_APP_PATH=${IOS_OUT}/${IOS_APP}
55
56ios_uninstall_app() {
57  ideviceinstaller -U "$IOS_BUNDLE_ID"
58}
59
60ios_package_app() {
61  rm -rf $IOS_PCKG_DIR
62  mkdir -p $IOS_PCKG_DIR/Payload  # this directory must be named 'Payload'
63  cp -rf "${IOS_OUT}/iOSShell.app" "${IOS_PCKG_DIR}/Payload/"
64  pushd $IOS_PCKG_DIR
65  zip -r ${IOS_APP} Payload
66  cp ${IOS_APP} ${IOS_APP_PATH}
67  popd
68}
69
70ios_install_app() {
71  ideviceinstaller -i ${IOS_APP_PATH}
72}
73
74ios_rm() {
75  local TARGET="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
76
77  ios_mount
78  rm -rf "$TARGET"
79  ios_umount
80}
81
82ios_mkdir() {
83  local TARGET="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
84  ios_mount
85  mkdir -p "$TARGET"
86  ios_umount
87}
88
89ios_cat() {
90  local TARGET="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
91  ios_mount
92  RET="$(cat $TARGET)"
93  ios_umount
94  echo -e "$RET"
95}
96
97# ios_mount: mounts the iOS device for reading or writing.
98ios_mount() {
99  # If this is already mounted we unmount it.
100  if $(mount | grep --quiet "$IOS_MOUNT_POINT"); then
101    >&2 echo "Device already mounted at: $IOS_MOUNT_POINT - Unmounting."
102    ios_umount || true
103  fi
104
105  # Ensure there is a mount directory.
106  if [[ ! -d "$IOS_MOUNT_POINT" ]]; then
107    mkdir -p $IOS_MOUNT_POINT
108  fi
109  ifuse --container $IOS_BUNDLE_ID $IOS_MOUNT_POINT
110  sleep 1
111  >&2 echo "Successfully mounted device."
112  #find $IOS_MOUNT_POINT
113}
114
115# ios_umount: unmounts the ios device.
116ios_umount() {
117  umount $IOS_MOUNT_POINT
118  sleep 1
119}
120
121# ios_restart: restarts the iOS device.
122ios_restart() {
123  ios_umount || true
124  idevicediagnostics restart
125}
126
127# ios_pull(ios_src, host_dst): Copies the content of ios_src to host_dst.
128# The path is relative to the 'Documents' folder on the device.
129ios_pull() {
130  # read input params
131  local IOS_SRC="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
132  local HOST_DST="$2"
133
134  ios_mount
135  if [[ -d "${HOST_DST}" ]]; then
136    cp -r "$IOS_SRC/" "$HOST_DST"
137  else
138    cp -r "$IOS_SRC" "$HOST_DST"
139  fi
140  ios_umount
141}
142
143# ios_push(host_src, ios_dst)
144ios_push() {
145  # read input params
146  local HOST_SRC="$1"
147  local IOS_DST="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$2"
148
149  ios_mount
150  rm -rf $IOS_DST
151  mkdir -p "$(dirname $IOS_DST)"
152  cp -r "$HOST_SRC" "$IOS_DST"
153  ios_umount
154}
155
156ios_path_exists() {
157  local TARGET_PATH="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
158  local RET=1
159  ios_mount
160  if [[ -e $TARGET_PATH ]]; then
161    RET=0
162  fi
163  ios_umount
164  return $RET
165}
166
167#   ANDROID_LS=`$ADB $DEVICE_SERIAL shell ls -ld $ANDROID_DST`
168#   HOST_LS=`ls -ld $HOST_SRC`
169#   if [ "${ANDROID_LS:0:1}" == "d" -a "${HOST_LS:0:1}" == "-" ];
170#   then
171#     ANDROID_DST="${ANDROID_DST}/$(basename ${HOST_SRC})"
172#   fi
173
174
175#   ANDROID_LS=`$ADB $DEVICE_SERIAL shell ls -ld $ANDROID_DST`
176#   if [ "${ANDROID_LS:0:1}" == "-" ]; then
177#     #get the MD5 for dst and src
178#     ANDROID_MD5=`$ADB $DEVICE_SERIAL shell md5 $ANDROID_DST`
179#     if [ $(uname) == "Darwin" ]; then
180#       HOST_MD5=`md5 -q $HOST_SRC`
181#     else
182#       HOST_MD5=`md5sum $HOST_SRC`
183#     fi
184
185#     if [ "${ANDROID_MD5:0:32}" != "${HOST_MD5:0:32}" ]; then
186#       echo -n "$ANDROID_DST "
187#       $ADB $DEVICE_SERIAL push $HOST_SRC $ANDROID_DST
188#     fi
189#   elif [ "${ANDROID_LS:0:1}" == "d" ]; then
190#     for FILE_ITEM in `ls $HOST_SRC`; do
191#       adb_push_if_needed "${HOST_SRC}/${FILE_ITEM}" "${ANDROID_DST}/${FILE_ITEM}"
192#     done
193#   else
194#     HOST_LS=`ls -ld $HOST_SRC`
195#     if [ "${HOST_LS:0:1}" == "d" ]; then
196#       $ADB $DEVICE_SERIAL shell mkdir -p $ANDROID_DST
197#       adb_push_if_needed $HOST_SRC $ANDROID_DST
198#     else
199#       echo -n "$ANDROID_DST "
200#       $ADB $DEVICE_SERIAL shell mkdir -p "$(dirname "$ANDROID_DST")"
201#       $ADB $DEVICE_SERIAL push $HOST_SRC $ANDROID_DST
202#     fi
203#   fi
204# }
205
206# setup_device "${DEVICE_ID}"
207