#!/usr/bin/env bash

# fail on errors
set -e

# Colors and formatting
RED='\033[1;31m'
GREEN='\033[1;32m'
BLUE='\033[1;34m'
NC='\033[0m' # No Color

readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ ! -f "$SCRIPT_DIR/fastlane/Fastfile" ]; then
  echo -e "${RED}fastlanew must be invoked from the root of the workspace.${NC}"
  exit 1
fi

# Get expected fastlane version from GEMFILE
readonly FASTLANE_VERSION=$(grep -o 'fastlane", "~> [0-9]\+\.[0-9]\+\.[0-9]\+' Gemfile | sed 's/fastlane", "~> //')

if [[ "$(FASTLANE_SKIP_UPDATE_CHECK=1 bundle exec fastlane --version)" != *"fastlane $FASTLANE_VERSION" ]]; then
  echo -e "${RED}Fastlane is not installed or the version is incorrect (expected $FASTLANE_VERSION). Please run ./INSTALL.${NC}"
  exit 1
fi

if [[ -n "${CI:-}" ]]; then
  export FASTLANE_SKIP_UPDATE_CHECK=1
fi

bundle exec fastlane "$@"
