#!/bin/bash -e

LINUX_REPO=
BETA=
POSITIONAL=()
while [ $# -gt 0 ]; do
  case "$1" in
    --linux-repo=*)
      LINUX_REPO="${1#*=}"
      shift
      ;;
    --linux-repo)
      LINUX_REPO="$2"
      shift 2
      ;;
    --beta)
      BETA=1
      shift
      ;;
    *)
      POSITIONAL+=("$1")
      shift
      ;;
  esac
done
set -- "${POSITIONAL[@]}"

linux_git() {
  if [ -n "$LINUX_REPO" ]; then
    git -C "$LINUX_REPO" "$@"
  else
    git "$@"
  fi
}

if [ -z "$1" ]; then
  GIT_HASH="$(curl -Ss https://raw.githubusercontent.com/raspberrypi/firmware/stable/extra/git_hash)"
else
  GIT_HASH="$1"
fi
GIT_HASH="$(linux_git rev-parse --verify "$GIT_HASH^{commit}")"

KVER=$(
  MAKEFILE_HEAD=$(linux_git show "${GIT_HASH}:Makefile" | head)
  VERSION=$(echo "$MAKEFILE_HEAD" | sed -n 's/^VERSION = \(.*\)$/\1/p')
  PATCHLEVEL=$(echo "$MAKEFILE_HEAD" | sed -n 's/^PATCHLEVEL = \(.*\)$/\1/p')
  SUBLEVEL=$(echo "$MAKEFILE_HEAD" | sed -n 's/^SUBLEVEL = \(.*\)$/\1/p')
  echo "$VERSION.$PATCHLEVEL.$SUBLEVEL"
)

CURRENT=$(dpkg-parsechangelog -SVersion)

# In --beta, the local trixie-beta-base tracks the last released trixie,
# not previously-published betas, so without consulting trixie-beta we'd
# bump to the same version twice for back-to-back beta builds that
# share a KVER.
if [ -n "$BETA" ]; then
  git fetch origin pios/trixie-beta 2>/dev/null || true
  if git rev-parse --verify origin/pios/trixie-beta >/dev/null 2>&1; then
    PUBLISHED=$(git show origin/pios/trixie-beta:debian/changelog 2>/dev/null \
                | sed -nE '1s/^[^ ]+ \(([^)]+)\).*/\1/p')
    if [ -n "$PUBLISHED" ] && dpkg --compare-versions "$PUBLISHED" gt "$CURRENT"; then
      CURRENT="$PUBLISHED"
    fi
  fi
fi

CURRENT_KVER=$(echo "$CURRENT" | sed -E 's/^[0-9]+://; s/-[0-9]+\+rpt[0-9]+(~beta[0-9]+)?$//')
CURRENT_RPT=$(echo "$CURRENT" | sed -nE 's/.*\+rpt([0-9]+)(~beta[0-9]+)?$/\1/p')
CURRENT_BETA=$(echo "$CURRENT" | sed -nE 's/.*~beta([0-9]+)$/\1/p')

if [ "$CURRENT_KVER" != "$KVER" ]; then
  if [ -n "$BETA" ]; then
    NEW_VERSION="1:$KVER-1+rpt1~beta1"
  else
    NEW_VERSION="1:$KVER-1+rpt1"
  fi
elif [ -n "$BETA" ]; then
  if [ -n "$CURRENT_BETA" ]; then
    NEW_VERSION="1:$KVER-1+rpt${CURRENT_RPT}~beta$((CURRENT_BETA + 1))"
  else
    NEW_VERSION="1:$KVER-1+rpt$((CURRENT_RPT + 1))~beta1"
  fi
else
  if [ -n "$CURRENT_BETA" ]; then
    NEW_VERSION="1:$KVER-1+rpt${CURRENT_RPT}"
  else
    NEW_VERSION="1:$KVER-1+rpt$((CURRENT_RPT + 1))"
  fi
fi
dch -v "$NEW_VERSION" -b "Raspberry Pi OS release"

# Get orig tarball
origtargz -d

# Generate rpi.patch
mkdir -p debian/patches/rpi/
linux_git diff "v$KVER..$GIT_HASH" | grep -v "^index" > debian/patches/rpi/rpi.patch

if [ -n "$(git status --porcelain -- debian/patches/rpi)" ]; then
  git add debian/patches/rpi -f
  git commit -m "Update Raspberry Pi patch"
fi

# Extract upstream source
origtargz -u

# Update configs
while read -r patch; do
  patch -s -p1 < debian/patches/"$patch"
done < <(quilt series)
debian/bin/rpi/genconfigs
while read -r patch; do
  patch -s -p1 -R < debian/patches/"$patch"
done < <(quilt series | tac)

if [ -n "$(git status --porcelain -- debian/config)" ]; then
  git add debian/config -f
  git commit -m "Update debian/config"
fi

# Absorb intermediate commit messages into the changelog
gbp dch --ignore-branch \
  --multimaint-merge \
  --customizations=/usr/share/doc/git-buildpackage/examples/wrap_cl.py

# Append the Linux commit hash as the last bullet
dch "Linux commit: $GIT_HASH"

# Finalize the release entry
dch -r --distribution=trixie --force-distribution ""

# Move the current maintainer's section to the bottom of the entry
debian/bin/rpi/reorder-changelog

git add -f debian/changelog
git commit -m "Update changelog for $NEW_VERSION"
