#!/bin/bash

set -ex

# defunc auth - when a repo doesn't exist all servers will ask for auth --- but that's nonesense
# NB: launchpad should also be used before invent, germinte will try to access bzr from invent for some reason
export GIT_ASKPASS=true

if ! which dch >/dev/null; then
  echo >&2 "please install devscripts"
  exit 1
fi

if ! which debootstrap >/dev/null; then
  echo >&2 "please install debootstrap"
  exit 1
fi

OUTPUT_DIRECTORY=${OUTPUT_DIRECTORY:-$(pwd)}
# All possible architectures, we'll disqualifies ones without `desktop-$arch`.
ARCHIES="amd64 armhf arm64"

germinate-update-metapackage --vcs --output-directory $OUTPUT_DIRECTORY

for arch in $ARCHIES; do
  # Lest we error because the wildcard didn't match anything. And we need
  # the arch in there so we don't accidentally mangle .old files.
  [ ! -f $OUTPUT_DIRECTORY/desktop-$arch ] && continue
  for file in $(eval echo $OUTPUT_DIRECTORY/desktop-*$arch); do
    echo "Mangling $file..."
    sed -i '/ttf-.*/d' $file
    sed -i '/fonts-[^nh].*/d' $file
    sed -i '/fonts-[^nh]/d' $file
    sed -i '/kerneloops-daemon/d' $file
    # kerneloops in 18.04+ is the daemon
    sed -i '/kerneloops/d' $file
    sed -i '/whoopsie/d' $file
    sed -i '/^apport$/d' $file
  done
done

cd $OUTPUT_DIRECTORY
if [ -x .git ]; then # this can get run from CI builds which have no .git
  git checkout debian/changelog
fi
