#!/bin/sh

echo "🚀  preparing a new OJP Version"

BRANCHNAME=$(git branch --show-current)
BRANCHPREFIX="release"

if [[ $BRANCHNAME == $BRANCHPREFIX/* ]]
then
    echo "✅ assure working on a release branch branch"
else
    echo "💣 Aborting as we're on the wrong branch"
    exit 1
fi

VERSION=$(echo $BRANCHNAME | sed -e "s/^$BRANCHPREFIX\///")

echo "using $VERSION"

SEMVER=( ${VERSION//./ } )

if [ -z "${SEMVER[0]}" ]; then
    echo "🛑 Missing major number"
    exit 1
fi


if [ -z "${SEMVER[1]}" ]; then
    echo "🛑 Missing minor number"
    exit 1
fi

if [ -z "${SEMVER[2]}" ]; then
    echo "🛑 Missing patch number"
    exit 1
fi


echo "import Foundation
// This is automatically generated by 'generate-version-file.sh' as part of the `prepare_release.yml` workflow

public extension OJP {
    /// Current version of the SDK: $VERSION
    static let version = \"$VERSION\"
}" > Sources/OJP/Generated/Version.swift

echo "📝 done writing 'Sources/OJP/Generated/Version.swift'"

git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"

git commit -am "writing $VERSION to 'Sources/OJP/Generated/Version.swift'"
git push
