#!/bin/bash
source "$ROOT_PATH"/scripts/common.sh
set -eo pipefail

# Initialize the files, vars and the git data:
git config --global user.email "svc-github@perimeter81.com"
git config --global user.name "Sayuser"

echo_green_bg "start to create change log and add files to commit and tag the commit"

PR_NUMBER="$1"
REPO_NAME="$2"
change_log="false"
package_json="false"
package_lock_json="false"
pom_xml="false"
to_commit="false"
git fetch --tags >/dev/null 2>&1

# get last tag:
last_tag=$(git describe --tags $(git rev-list --tags --max-count=1))
echo_green "last tag: $last_tag"

# get commit messages until the last tag:
if [ -z "$CHANGELOG_DESCRIPTION" ]; then
  echo_green "no CHANGELOG_DESCRIPTION env var. get commit messages until the last tag:"
  git log --pretty=format:"%s" HEAD...$last_tag  > tmp_release_notes.txt
  sed -i 's/^/- /' tmp_release_notes.txt
  echo_green "the number of commits until the last tag $last_tag is: $(cat tmp_release_notes.txt | wc -l)"
else
  echo_green "CHANGELOG_DESCRIPTION env var exist. add it to release notes:"
  echo_green "CHANGELOG_DESCRIPTION: $CHANGELOG_DESCRIPTION"
  echo "merged from PR [#$PR_NUMBER](https://github.com/$REPO_NAME/pull/$PR_NUMBER)" >> tmp_release_notes.txt
  echo_green "release notes:"
  cat tmp_release_notes.txt
  echo_green "end of release notes"
fi
echo -e "\n" >> tmp_release_notes.txt

git log --pretty=format:"%s" HEAD...$last_tag  > tag_release_notes.txt

# add info to release notes
echo -e "\n" >> tag_release_notes.txt
echo "build $BUILD_NUMBER from github actions on $(date):" >> tag_release_notes.txt
echo "merged from PR (#$PR_NUMBER)" >> tag_release_notes.txt
echo "by jira https://perimeter81.atlassian.net/browse/${JIRA_TICKET}" >> tag_release_notes.txt


# add commit messages to changelog file
if [ -f "CHANGELOG.md" ]; then
  echo_green "CHANGELOG.md exist. add release notes to changelog.md:"
  sed -i "7 i ## [$VERSION_TO_DEPLOY] - $(date +%F)" CHANGELOG.md
  sed -i "8 i ### Added" CHANGELOG.md
  sed -i "8r tmp_release_notes.txt" CHANGELOG.md
  to_commit="true"
  change_log="true"
  echo_green "done to add release notes to changelog.md"
fi

# update version in package.json
if [ -f "package.json" ]; then
  echo_green "package.json exist. update version in package.json:"
  n_version="${VERSION_TO_DEPLOY:1}"
  echo "VERSION_TO_DEPLOY=$n_version" >> $GITHUB_ENV
  jq ".version = \"${n_version}\"" package.json > tmp.$$.json && mv tmp.$$.json package.json
  to_commit="true"
  package_json="true"
  echo_green "done to update version in package.json"
fi

# update version in package-lock.json
if [ -f "package-lock.json" ]; then
  echo_green "package-lock.json exist. update version in package.json:"
  n_version="${VERSION_TO_DEPLOY:1}"
  echo "VERSION_TO_DEPLOY=$n_version" >> $GITHUB_ENV
  jq ".version = \"${n_version}\"" package-lock.json > tmp.$$.json && mv tmp.$$.json package-lock.json
  to_commit="true"
  package_lock_json="true"
  echo_green "done to update version in package-lock.json"
fi

if [ -f "pom.xml" ]; then
  if [ "$SVC_NAME" == "yarkon-gateway" ]; then
    n_version="${VERSION_TO_DEPLOY}"
  else
    n_version="${VERSION_TO_DEPLOY:1}"
  fi

  echo_green "new version: $n_version"
  xml_path=".project.properties.revision=\"$n_version\""
  echo "VERSION_TO_DEPLOY=$n_version" >> $GITHUB_ENV
  yq -p xml -o xml -i $xml_path pom.xml
  yq -p xml '.project.properties.revision' pom.xml
  to_commit="true"
  pom_xml="true"
fi

if [ $to_commit = "true" ]; then
  release_notes_lines=$(wc -l < "tag_release_notes.txt" | awk '{print $1}')
  if [[ $release_notes_lines -gt 20 ]]; then
    echo_green "$release_notes_lines lines in release notes (more then 20). add it to change log and tag notes without log it here"
  else
    echo_green "########### release notes: ############"
    cat tag_release_notes.txt
  fi

  if [ $change_log = "true" ]; then
    echo_green "git add changelog:"
    git add CHANGELOG.md
    echo_green "done to git add changelog"
  fi
  if [ $package_json = "true" ]; then
    echo_green "git add package.json:"
    git add package.json
    echo_green "done to git add package.json"
  fi
  if [ $package_lock_json = "true" ]; then
    echo_green "git add package-lock.json:"
    git add package-lock.json
    echo_green "done to git add package-lock.json"
  fi

  if [ $pom_xml = "true" ]; then
    echo_green "git add pom.xml:"
    git add pom.xml
    echo_green "done to git add pom.xml"
  fi

  echo_green "commit the changes in the files:"
  export "SKIP_COMMIT_MSG_CHECK=true"
  echo "SKIP_COMMIT_MSG_CHECK: $SKIP_COMMIT_MSG_CHECK"
  git commit -m "ci: update CHANGELOG.md and package.json"
  echo_green "done commit"

  echo_green "tag the commit locally"
  git tag ${VERSION_TO_DEPLOY} -F tag_release_notes.txt
  echo_green "done to tag the commit locally"

  echo_green "clean the release notes files:"
  rm tag_release_notes.txt
  rm tmp_release_notes.txt
  echo_green_bg "done!"
fi
