#!/bin/bash
# Don't run this directly, use "debian/rules update-version" instead

set -e

cd $(dirname "$0")

OLD_BOOTSTRAP=$1
OLD=$2
NEW=$3

sed -i \
    -e "s/\<cargo-$OLD/cargo-$NEW/g" \
    -e "s/\<rustc-$OLD/rustc-$NEW/g" \
    -e "s/\<rust-$OLD/rust-$NEW/g" \
    -e "s/\<rustfmt-$OLD/rustfmt-$NEW/g" \
    -e "s/\<libstd-rust-$OLD/libstd-rust-$NEW/g" \
    -e "s/\<cargo-$OLD_BOOTSTRAP/cargo-$OLD/g" \
    -e "s/\<rustc-$OLD_BOOTSTRAP/rustc-$OLD/g" \
    -e "s/\<rust-$OLD_BOOTSTRAP/rust-$OLD/g" \
    -e "s/\<rustfmt-$OLD_BOOTSTRAP/rustfmt-$OLD/g" \
    -e "s/\<libstd-rust-$OLD_BOOTSTRAP/libstd-rust-$OLD/g" \
    control

WASI_REGEX='wasi-libc \(>= [^)]*\)'
wasi_ver=$(rmadison -s "$(distro-info --devel)" wasi-libc | awk -F'|' 'NR==1{gsub(/ /,"",$2);print $2}' | sed 's/-[^-~]*$//')
if [ -n "$wasi_ver" ]; then
    sed -ri -e "s|${WASI_REGEX}|wasi-libc (>= ${wasi_ver}~)|g" control
else
    echo >&2 "warning: could not determine archive wasi-libc version; leaving control unchanged"
fi

sed -i -e "s/\<rustc-$OLD\>/rustc-$NEW/g" source/lintian-overrides
