#!/usr/bin/env bash

# Copyright (c) 2024, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o pipefail
this=`basename $0`

usage () {
cat << EOF
Generate a changelog for an NVIDIA Container Toolkit release

Usage: $this [-h] --previous-version <previous_version> --version <version>

Options:
  --previous-version    specify the previous version
  --version             specify the version for this release.
  --help/-h   show this help and exit

EOF
}

version=
previous_version=
# Parse command line options
while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        --previous-version)
        previous_version="$2"
        shift 2
        ;;
        --version)
        version="$2"
        shift 2
        ;;
        --help/-h)
            usage
            exit 0
            ;;
        *)  usage
            exit 1
            ;;
    esac
done

# Check that the required args were provided.
if [ -z ${version} ]; then
    echo -e "ERROR: --version is required"
    usage
    exit 1
fi

if [ -z ${previous_version} ]; then
    echo -e "ERROR: --previous-version is required"
    usage
    exit 1
fi

>&2 echo "version=$version, previous_version=$previous_version"

# Fetch the latest tags from the remote
remote=$( git remote -v | grep -E "NVIDIA/nvidia-container-toolkit(\.git)?\s" | grep -oE "^[a-z]+" | sort -u )
>&2 echo "Detected remote as '${remote}'"
git fetch ${remote} --tags

SHA=$(git rev-parse ${version})
if [[ $? -ne 0 ]]; then
    SHA="HEAD"
fi

>&2 echo "Using ${previous_version} as previous version"

# Print the changelog
echo "## What's Changed"
echo ""
if [[ ${version} != v*-rc.* && ${previous_version} == v*-rc.* ]]; then
echo "- Promote ${previous_version} to $version"
fi

# Iterate over the commit messages and ignore the ones that start with "Merge" or "Bump"
git log --pretty=format:"%s" ${previous_version}..$SHA -- ':!deployments/container' ':!tools' | grep -Ev "(^Merge )|(^Bump)|(no-rel-?note)|(^---)" |  sed 's/^\(.*\)/- \1/g'

echo ""
echo "### Changes in the Toolkit Container"
echo ""
git log --pretty=format:"%s" ${previous_version}..$SHA -- deployments/container tools | grep -Ev "(^Merge )|(no-rel-?note)|(^---)" |  sed 's/^\(.*\)/- \1/g'

LIB_NVIDIA_CONTAINER_REFERENCE=$( git ls-tree ${previous_version} third_party/libnvidia-container --object-only )
LIB_NVIDIA_CONTAINER_VERSION=$( git ls-tree $SHA third_party/libnvidia-container --object-only )

echo ""
if [[ "${LIB_NVIDIA_CONTAINER_REFERENCE}" != "${LIB_NVIDIA_CONTAINER_VERSION}" ]] && [[ $(git -C third_party/libnvidia-container log --pretty=format:"%s" $LIB_NVIDIA_CONTAINER_REFERENCE..$LIB_NVIDIA_CONTAINER_VERSION | grep -Ev "(^Merge )|(^Bump)|(no-rel-?note)|(^---)" |  sed 's/^\(.*\)/- \1/g' | wc -l) -gt 0  ]]; then
echo "### Changes in libnvidia-container"
echo ""
git -C third_party/libnvidia-container log --pretty=format:"%s" $LIB_NVIDIA_CONTAINER_REFERENCE..$LIB_NVIDIA_CONTAINER_VERSION | grep -Ev "(^Merge )|(^Bump)|(no-rel-?note)|(^---)" |  sed 's/^\(.*\)/- \1/g'
echo ""
fi

echo "**Full Changelog**: https://github.com/NVIDIA/nvidia-container-toolkit/compare/${previous_version}...${version}"
echo ""
