#!/bin/bash

if [ -z $2 ]; then
cat <<END
Usage: $0 AVD_NAME AVD_SERIAL
Runs all tests (JVM and instrumented) with coverage report

Parameters:
    AVD_NAME        the name of the Android Virtual Device
    AVD_SERIAL      the port number to use (e.g. 5560)
END
    exit 1
fi

if [ ! -f "${ANDROID_HOME}/platform-tools/adb" ]; then
    echo "Error: ANDROID_HOME is not set correctly"
    exit 1
fi

AVD_NAME=$1
AVD_SERIAL=$2
ADB="${ANDROID_HOME}/platform-tools/adb -s emulator-${AVD_SERIAL}"
EMULATOR="${ANDROID_HOME}/tools/emulator"
GRADLE="./gradlew --no-daemon --stacktrace"
PACKAGE_NAME=org.isoron.uhabits
OUTPUTS_DIR=app/build/outputs

info() {
    if [ ! -z "$TEAMCITY_VERSION" ]; then
        echo "###teamcity[progressMessage '$1']"
    else
        local COLOR='\033[1;32m'
        local NC='\033[0m'
        echo -e " $COLOR*$NC $1"
    fi
}

fail() {
    $ADB shell reboot -p
    exit 1
}

#--------------------------------------------------------------------------------
info "Starting emulator"
$EMULATOR -avd ${AVD_NAME} -port ${AVD_SERIAL} -no-audio -no-window &
$ADB wait-for-device || fail
sleep 10

#--------------------------------------------------------------------------------
info "Running adb as root"
$ADB root || fail

#--------------------------------------------------------------------------------
info "Cleaning output directory"
rm -rf ${OUTPUTS_DIR}
mkdir -p ${OUTPUTS_DIR}

#--------------------------------------------------------------------------------
info "Building instrumentation APKs"
$GRADLE assembleDebug assembleAndroidTest  || fail

#--------------------------------------------------------------------------------
info "Installing APKs"
$ADB install -r ${OUTPUTS_DIR}/apk/app-debug.apk  || fail
$ADB install -r ${OUTPUTS_DIR}/apk/app-debug-androidTest.apk || fail

#--------------------------------------------------------------------------------
info "Granting permissions"
$ADB shell pm grant org.isoron.uhabits android.permission.SET_ANIMATION_SCALE || fail

#--------------------------------------------------------------------------------
info "Running instrumentation tests"
$ADB shell am instrument -r -e coverage true -e size medium \
    -w ${PACKAGE_NAME}.test/android.support.test.runner.AndroidJUnitRunner \
    > ${OUTPUTS_DIR}/instrument.txt

java -jar tools/automator-log-converter-1.5.0.jar ${OUTPUTS_DIR}/instrument.txt || fail

#--------------------------------------------------------------------------------
info "Fetching generated artifacts"
mkdir -p ${OUTPUTS_DIR}/failed
$ADB pull /mnt/sdcard/test-screenshots/ ${OUTPUTS_DIR}/failed
$ADB pull /storage/sdcard/test-screenshots/ ${OUTPUTS_DIR}/failed
$ADB pull /sdcard/Android/data/${PACKAGE_NAME}/files/test-screenshots/ ${OUTPUTS_DIR}/failed

$ADB shell rm -r /mnt/sdcard/test-screenshots/ 
$ADB shell rm -r /storage/sdcard/test-screenshots/ 
$ADB shell rm -r /sdcard/Android/data/${PACKAGE_NAME}/files/test-screenshots/ 

#--------------------------------------------------------------------------------
info "Fetching logcat"
$ADB logcat -d > ${OUTPUTS_DIR}/logcat.txt

#--------------------------------------------------------------------------------
info "Building coverage report"
mkdir -p ${OUTPUTS_DIR}/code-coverage/connected/
$ADB pull /data/user/0/${PACKAGE_NAME}/files/coverage.ec \
    ${OUTPUTS_DIR}/code-coverage/connected/
$GRADLE --no-daemon coverageReport  || fail

#--------------------------------------------------------------------------------
info "Uninstalling test APK"
$ADB uninstall ${PACKAGE_NAME}.test || fail

#--------------------------------------------------------------------------------
info "Stopping emulator"
$ADB shell reboot -p

exit $failed
