#!/usr/bin/env bash

SMPLAYER_VERSION_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SMPLAYER_PROJECT_ROOT="$(cd "$SMPLAYER_VERSION_SCRIPT_DIR/.." && pwd)"

_smplayer_version() {
  dart "$SMPLAYER_VERSION_SCRIPT_DIR/version.dart" "$@" \
    --project-root "$SMPLAYER_PROJECT_ROOT"
}

smplayer_current_version() {
  _smplayer_version current "$1"
}

smplayer_build_number() {
  _smplayer_version build-number "$1"
}

smplayer_has_flutter_option() {
  local option_name="$1"
  shift
  for argument in "$@"; do
    [[ "$argument" == "$option_name" || "$argument" == "$option_name="* ]] && return 0
  done
  return 1
}

smplayer_has_dart_define() {
  local define_name="$1"
  shift
  local previous_argument=''
  for argument in "$@"; do
    [[ "$argument" == "--dart-define=${define_name}="* ]] && return 0
    [[ "$previous_argument" == '--dart-define' && "$argument" == "${define_name}="* ]] && return 0
    previous_argument="$argument"
  done
  return 1
}
