#!/usr/bin/env bash
#parses  BUILD_VERSION_SHORT from variable BUILD_VERSION_SHORT(RELEASE_VERSION or RELEASE_VERSION_KIDS)

while getopts v:n: flag
do
    case "${flag}" in
        v) VERSION=${OPTARG};;
        n) CI_VARIABLE_NAME=${OPTARG};;
    esac
done

#increment version
BUILD_VERSION=${RELEASE_VERSION}
echo "export build_version: ${BUILD_VERSION}"

VERSION_ARRAY=($(echo "$BUILD_VERSION" | tr '.' '\n'))
export BUILD_VERSION_SHORT=${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}
echo "build_version_short: ${BUILD_VERSION_SHORT}"

#echo "Updating CI $CI_VARIABLE_NAME variable value to ${BUILD_VERSION} with token ${PROJECT_ACCESS_TOKEN}";
#if curl --request PUT --header "PRIVATE-TOKEN: ${PROJECT_ACCESS_TOKEN}" "${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/variables/${CI_VARIABLE_NAME}" --form "value=${BUILD_VERSION}"; then
#    echo "${CI_VARIABLE_NAME} variable value updated";
#else
#    echo "failed to update ${CI_VARIABLE_NAME} variable value";
#fi


echo "setting versions in project file"
sed -i.original "s/CURRENT_PROJECT_VERSION = [0-9\.]*;/CURRENT_PROJECT_VERSION = 0;/g" RutubeTv.xcodeproj/project.pbxproj #build version
sed -i.original "s/MARKETING_VERSION = [0-9\.]*;/MARKETING_VERSION = ${BUILD_VERSION_SHORT};/g" RutubeTv.xcodeproj/project.pbxproj ##version
sed -i.original 's/CODE_SIGN_STYLE = Manual;/CODE_SIGN_STYLE = Automatic;/' RutubeTv.xcodeproj/project.pbxproj
sed -i.original '/DEVELOPMENT_TEAM\[sdk=appletvos\*\] = DFX963574W;/d' RutubeTv.xcodeproj/project.pbxproj
sed -i.original 's/"PROVISIONING_PROFILE_SPECIFIER\[sdk=appletvos\*\]" = tvOSDevelopProvision19_08;/"PROVISIONING_PROFILE_SPECIFIER\[sdk=appletvos\*\]" = "";/g' RutubeTv.xcodeproj/project.pbxproj
OPTIND=1
