#!/bin/bash -e
# uploadJS.sh

authToken=''
org=''
project=''
release=''
distribution=''
jsBundle=''
jsSourcemaps=''
root=''

argumentsCount=$#
for(( argumentIndex=1; argumentIndex<=argumentsCount; argumentIndex+=2 )); do
  argumentKey=${!argumentIndex}
  argumentValueIndex=$((argumentIndex+1))
  argumentValue=${!argumentValueIndex}
  if [[ $argumentKey == "--authToken" ]]; then
    authToken=$argumentValue
  elif [[ $argumentKey == "--org" ]]; then
    org=$argumentValue
  elif [[ $argumentKey == "--project" ]]; then
    project=$argumentValue
  elif [[ $argumentKey == "--release" ]]; then
    release=$argumentValue
  elif [[ $argumentKey == "--distribution" ]]; then
    distribution=$argumentValue
  elif [[ $argumentKey == "--jsBundle" ]]; then
    jsBundle=$argumentValue
  elif [[ $argumentKey == "--jsSourcemaps" ]]; then
    jsSourcemaps=$argumentValue
  elif [[ $argumentKey == "--root" ]]; then
    root=$argumentValue
  else
    echo "Unknown argument $argumentKey"
    exit 1
  fi
done

if [ -z "$authToken" ]
then
    echo "authToken is not set"
    exit 1
fi
if [ -z "$org" ]
then
    echo "org is not set"
    exit 1
fi
if [ -z "$project" ]
then
    echo "project is not set"
    exit 1
fi
if [ -z "$release" ]
then
    echo "release is not set"
    exit 1
fi
if [ -z "$distribution" ]
then
    echo "distribution is not set"
    exit 1
fi
if [ -z "$jsBundle" ]
then
    echo "jsBundle is not set"
    exit 1
fi
if [ -z "$jsSourcemaps" ]
then
    echo "jsSourcemaps is not set"
    exit 1
fi
if [ -z "$root" ]
then
    echo "root is not set"
    exit 1
fi

npx sentry-cli sourcemaps upload \
  --strip-prefix $root \
  $jsBundle $jsSourcemaps \
  --release "$release" \
  --dist "$distribution" \
  --auth-token $authToken \
  --org $org \
  --project $project