#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of nvme.
# Copyright (c) 2026 SUSE LLC
#
# Authors: Daniel Wagner <dwagner@suse.de>

usage() {
    echo "Usage: release.sh [-d] VERSION"
    echo ""
    echo "The script does all necessary steps to create a new release."
    echo ""
    echo " -d:  no documentation update"
    echo " -f:  disable all sanity checks and just do the release"
    echo ""
    echo "Note: The version number needs to be exactly"
    echo "      '^v[\d]+.[\d]+(.[\d\]+(-rc[0-9]+)?$'"
    echo ""
    echo "example:"
    echo "  release.sh v2.1-rc0     # v2.1 release candidate 0"
    echo "  release.sh v2.1         # v2.1 release"
}

build_doc=true
force=false

while getopts "df" o; do
    case "${o}" in
        d)
            build_doc=false
            ;;
        f)
            force=true
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

VERSION=${1:-}

if [ -z "$VERSION" ] ; then
    usage
    exit 1
fi

cleanup() {
    if [ -z "${OLD_HEAD}" ] ; then
        exit
    fi
    git tag -d "Release $VERSION" "$VERSION"
    git reset --hard "${OLD_HEAD}"
}

register_cleanup() {
    OLD_HEAD="$(git rev-parse HEAD)"
}

unregister_cleanup() {
    OLD_HEAD=""
}

trap cleanup EXIT

# expected version regex
re='^v([0-9]+\.[0-9]+(\.[0-9]+)?)(-(rc|a|b)\.?[0-9]+)?$'

# use the version string provided from the command line
if [[ "$VERSION" =~ ${re} ]]; then
    echo "valid version $VERSION string"

    # remove the leading 'v'
    ver="${VERSION#v}"
else
    echo "invalid version string $VERSION"
    exit 1
fi

register_cleanup

cd "$(git rev-parse --show-toplevel)" || exit 1

if [ "$force" = false ] ; then
    if [[ -n $(git status -s) ]]; then
        echo "tree is dirty."
        if [[ "${dry_run}" = false ]]; then
            exit 1
        fi
    fi

    if [ "$(git rev-parse --abbrev-ref HEAD)" != "master" ] ; then
        echo "currently not on master branch. abort."
        exit 1
    fi
fi

if [ "$build_doc" = true ]; then
    ./scripts/update-docs.sh
    git add Documentation libnvme/doc
    git commit -s -m "doc: Regenerate all docs for $VERSION"
fi

BUILDDIR="$(mktemp -d)"

if ! meson setup -Dlibnvme=enabled -Djson-c=enabled "${BUILDDIR}" > "${BUILDDIR}/setup.log" 2>&1; then
    echo "release.sh: failed to configure a build for completion generation:" >&2
    cat "${BUILDDIR}/setup.log" >&2
    rm -rf -- "${BUILDDIR}"
    exit 1
fi

if ! meson compile -C "${BUILDDIR}" > "${BUILDDIR}/compile.log" 2>&1; then
    echo "release.sh: failed to build nvme for completion generation:" >&2
    cat "${BUILDDIR}/compile.log" >&2
    rm -rf -- "${BUILDDIR}"
    exit 1
fi

if ! "${BUILDDIR}/nvme" utils dump-command-metadata > "${BUILDDIR}/metadata.json" 2> "${BUILDDIR}/metadata.err"; then
    echo "release.sh: 'nvme utils dump-command-metadata' failed; is nvme built with json-c support?" >&2
    cat "${BUILDDIR}/metadata.err" >&2
    rm -rf -- "${BUILDDIR}"
    exit 1
fi

./completions/generate-completions.py \
    --bash completions/bash-nvme-completion.sh \
    --zsh completions/_nvme \
    < "${BUILDDIR}/metadata.json"
rm -rf -- "${BUILDDIR}"

if [[ -n $(git status -s -- completions/bash-nvme-completion.sh completions/_nvme) ]]; then
    git add completions/bash-nvme-completion.sh completions/_nvme
    git commit -s -m "completions: regenerate bash and zsh completions for $VERSION"
fi

# update meson.build
sed -i -e "0,/[ \t]version: /s/\([ \t]version: \).*/\1\'$ver\',/" meson.build
git add meson.build
git commit -s -m "Release $VERSION"

git tag -s -m "Release $VERSION" "$VERSION"
git push --dry-run origin "$VERSION"^{}:master tag "$VERSION"

read -p "All good? Ready to push changes to remote? [Yy]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    git push origin "$VERSION"^{}:master tag "$VERSION"
    unregister_cleanup
fi
