IS_TESTFLIGHT=false
while getopts s:a:t:d: flag
do
    case "${flag}" in
        s) SCHEME=${OPTARG};;
        a) APP=${OPTARG};;
        t) IS_TESTFLIGHT=true;;
        d) DEBUG=true;;
    esac
done

if $IS_TESTFLIGHT; then
    echo "creating tag for build version: ${BUILD_VERSION}";
    if curl --request POST --header "PRIVATE-TOKEN: ${PROJECT_ACCESS_TOKEN}" "${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/repository/tags?tag_name=${BUILD_VERSION}&ref=${CI_COMMIT_SHA}&message=NO_PIPE"; then
        echo "${BUILD_VERSION} tag created";
    else
        echo "failed to create tag for ${BUILD_VERSION}";
        exit 1
    fi
fi

archive_build_configuration="Release"
if [ $DEBUG ]; then
    archive_build_configuration="Debug"
fi

echo "build SCHEME: ${SCHEME} for app ${APP}" with archive build configuration: ${archive_build_configuration};

xcodebuild > /dev/null archive -workspace RutubeTv.xcworkspace -scheme "${SCHEME}" -configuration "${archive_build_configuration}" -archivePath "${APP}.xcarchive" -allowProvisioningUpdates -destination generic/platform=tvOS
echo "maked archive"
echo "maked archivePath and push to testflight"
xcodebuild > /dev/null -exportArchive -archivePath "${APP}.xcarchive" -exportOptionsPlist "ExportOptions.plist"  -exportPath . -allowProvisioningUpdates
