#!/bin/sh

set -e

PROJECT_VERSION="$1"

if [ -z "$1" ]; then
    echo "No version supplied"
    exit 1
fi

if ! command -v "ruff" &>/dev/null; then
    echo "please install ruff for formatting the python wrapper"
    exit 1
fi

if ! command -v "gofumpt" &>/dev/null; then
    echo "please install gofumpt for formatting the go code"
    exit 1
fi

if ! command -v "dch" &>/dev/null; then
    echo "please install dch to update the debian/changelog"
    exit 1
fi

if [ "$(git tag -l "${PROJECT_VERSION}")" ]; then
    echo "Version: ${PROJECT_VERSION} already has a tag"
    exit 1
fi

# First check if th
if [[ $(git diff) ]]; then
   echo "There are changes, commit them before releasing"
   exit 1
fi

# Format all go files
git ls-files | grep '.go$' | xargs -I {} gofumpt -w {} >/dev/null
if [[ $(git diff) ]]; then
    git add -u
    git commit -m "Format: Run Gofumpt"
fi

# Format all Python files
git ls-files | grep '.py$' | xargs -I {} ruff format --quiet {} >/dev/null
if [[ $(git diff) ]]; then
    git add -u
    git commit -m "Format: Run Ruff"
fi

# Get discovery files and verify signature
echo "getting and verifying discovery files..."
wget -q https://disco.eduvpn.org/v2/organization_list.json -O internal/discovery/organization_list.json
wget -q https://disco.eduvpn.org/v2/organization_list.json.minisig -O internal/discovery/organization_list.json.minisig
minisign -Vm "internal/discovery/organization_list.json" -P RWRtBSX1alxyGX+Xn3LuZnWUT0w//B6EmTJvgaAxBMYzlQeI+jdrO6KF || minisign -Vm "internal/discovery/organization_list.json" -P RWQKqtqvd0R7rUDp0rWzbtYPA3towPWcLDCl7eY9pBMMI/ohCmrS0WiM
wget -q https://disco.eduvpn.org/v2/server_list.json -O internal/discovery/server_list.json
wget -q https://disco.eduvpn.org/v2/server_list.json.minisig -O internal/discovery/server_list.json.minisig
minisign -Vm "internal/discovery/server_list.json" -P RWRtBSX1alxyGX+Xn3LuZnWUT0w//B6EmTJvgaAxBMYzlQeI+jdrO6KF || minisign -Vm "internal/discovery/server_list.json" -P RWQKqtqvd0R7rUDp0rWzbtYPA3towPWcLDCl7eY9pBMMI/ohCmrS0WiM

if [[ $(git diff) ]]; then
    git add -u
    git commit -m "Discovery: Update embedded files for new version

Signature server list:

$(cat internal/discovery/server_list.json.minisig)

Signature organization list:

$(cat internal/discovery/organization_list.json.minisig)
"
fi

# Update debian changelog
dch -v ${PROJECT_VERSION} --distribution unstable "New version"

# Update fedora spec
sed -i "s/^Version:.*/Version:        ${PROJECT_VERSION}/" rpm/specs/python3-eduvpn-common.spec
# We set the author and email the same as debian's
sed -i "/^%changelog/a * $(LC_ALL=C date +'%a %b %d %Y') ${DEBFULL_NAME} <${DEBEMAIL}> - ${PROJECT_VERSION}-1\n- New version\n" rpm/specs/python3-eduvpn-common.spec

# Replace version number
# replace in internal/commonver
sed -i "s/const Version = \".*\"/const Version = \"${PROJECT_VERSION}\"/" internal/commonver/commonver.go
sed -i "s/version = .*/version = ${PROJECT_VERSION}/" wrappers/python/setup.cfg
sed -i "s/COMMON_VERSION = \".*\"/COMMON_VERSION = \"${PROJECT_VERSION}\"/" wrappers/python/setup.py
sed -i "s/__version__ = \".*\"/__version__ = \"${PROJECT_VERSION}\"/" wrappers/python/eduvpn_common/__init__.py

git add -u
git commit -m "Version: Update to ${PROJECT_VERSION}"
