#!/usr/bin/env sh

set -e

APP_NAME='ImageViewer'
SIM_DEVICE='iPad (A16)'
SCHEME='ImageViewer'
SIMULATOR='iOS Simulator'
COMPANY='ImageViewer'

[ -z "$BUILD_TYPE" ] && BUILD_TYPE=Debug
DERIVED_DATA="./build"

sudo bash -c "echo '127.0.0.1 developerservices2.apple.com' >>/etc/hosts"

xcrun simctl boot "$SIM_DEVICE"
open -a Simulator

xcodebuild -scheme "$SCHEME" -configuration "$BUILD_TYPE" -derivedDataPath "$DERIVED_DATA/$BUILD_TYPE" -destination "platform=$SIMULATOR,name=$SIM_DEVICE"

APP_PATH="$DERIVED_DATA/Build/Products/Debug-iphonesimulator/$APP_NAME.app"
xcrun simctl install booted "$APP_PATH"
# xcrun simctl spawn booted log stream --predicate 'process == "ImageViewer"' --style syslog &
xcrun simctl launch booted "$COMPANY.$APP_NAME"

sudo sed -i '' '/developerservices2\.apple\.com/d' /etc/hosts
