#!/usr/bin/env bash

PROJECT=${PROJECT-"sutton"}

KERNEL_FROM=("oem-24.04c")
KERNEL_TO=("oem-24.04d")

FIXES=(
    "debian/control"
    "s|${KERNEL_FROM[0]}|${KERNEL_TO[0]}|g"
    "debian/tests/meta"
    "s|${KERNEL_FROM[0]}|${KERNEL_TO[0]}|g"
)

apply_fixes() {
    local i=0 file
    while [ $i -lt ${#FIXES[@]} ]; do
        file="${FIXES[$i]}"
        i=$((i + 1))

        while [[ "${FIXES[$i]}" =~ ^s\|.*$ ]]; do
            sed -i "${FIXES[$i]}" "$file"
            i=$((i + 1))
        done
    done
}

apply_fixes
dch -i "FIX: Migrate kernel to linux-${KERNEL_TO[0]}" --distribution noble
git commit -am "fix(kernel): kernel linux-${KERNEL_FROM[0]} -> linux-${KERNEL_TO[0]}"
git push

if SOURCE_CHANGES=$(debuild -S | grep -oP 'dpkg-genchanges --build=source -O\K.+_source.changes$'); then
    PLATFORM=numbat-$(git rev-parse --abbrev-ref HEAD | cut -d'-' -f1)
    dput "ppa:oem-archive/$PROJECT-$PLATFORM" "$SOURCE_CHANGES"
fi
