#!/bin/bash


ANDROID_UPLOAD_TRACK=${ANDROID_UPLOAD_TRACK:-alpha}
RELEASE_STATUS=${RELEASE_STATUS:-completed}
RELEASE_MESSAGE="${RELEASE_MESSAGE:-New version}"
GOOGLE_APPLICATION_CREDENTIALS="${GOOGLE_APPLICATION_CREDENTIALS:-}"


VERSION_AND_CODE="$(cat pubspec.yaml  | grep 'version:' | sed 's/version: //')"
VERISON_NAME="$(echo $VERSION_AND_CODE | cut -d+ -f1)"
VERSION_CODE="$(echo $VERSION_AND_CODE | cut -d+ -f2)"

WD="$(realpath $(dirname $0))"
export

pushd "${WD}/publish-android-js/"

npm install
./node_modules/.bin/tsc
npm install -g --save googleapis

node ${WD}/publish-android-js/publish-app-store.js \
"$ANDROID_UPLOAD_TRACK" "$VERSION_CODE" "$RELEASE_STATUS" "$VERSION_CODE $VERISON_NAME" "$RELEASE_MESSAGE" ${WD}/../build/app/outputs/bundle/release/app-release.aab \
|| exit 1

popd
