#!/bin/sh
#
# Generate a changelog file for the signed fwupdate package, based on
# a changelog.in file and other state

DIR=$1
SOURCE=$2
ARCH=$3
IN="${DIR}/changelog.in"
OUT="${DIR}/changelog"

# Parse out fields from our changelg entry - want the signing-template
# one to match all the important details where we can
DISTRIBUTION="$(dpkg-parsechangelog --show-field Distribution)"
URGENCY="$(dpkg-parsechangelog --show-field Urgency)"
MAINT="$(dpkg-parsechangelog --show-field Maintainer)"
DATE="$(dpkg-parsechangelog --show-field Date)"

# If the version ends in "+bXXX", this is a binNMU. We don't want a new
# source package to look like that, so change it to ".bXXX" instead
VERSION="$(dpkg-parsechangelog --show-field Version)"
MANGLED_VERSION="$(echo $VERSION | sed -r 's/-/\+/;s/\+(b[[:digit:]]+)$/.\1/')"

printf "%s-%s-signed (%s) %s; urgency=%s\n" "${SOURCE}" "${ARCH}" "${MANGLED_VERSION}" "${DISTRIBUTION}" "${URGENCY}" > $OUT
printf "\n" >> $OUT
printf "  * Update to %s version %s\n" "${SOURCE}" "${VERSION}" >> $OUT
printf "\n" >> $OUT
printf " -- %s  %s\n" "${MAINT}" "${DATE}" >> $OUT
printf "\n" >> $OUT

cat $IN >> $OUT
rm -f $IN
