#! /usr/bin/env bash
set -eo pipefail

# pqccomms-import-legacy invokes this as root to do the steps that require root
# privileges.
LEGACY_PKG="$1"
if [ -z "$LEGACY_PKG" ]; then
	echo "usage: $0 <legacy_package>" >&2
	exit 1
fi

# Copy the legacy CA certificate.  This was packaged in the legacy package (so
# do not delete it, we will uninstall instead), but it is not packaged in the
# newer pqccomms-client.  Instead the client setup places it when importing the
# site config JSON, so we have to place it now.
cp /usr/local/share/ca-certificates/"$LEGACY_PKG-ca.crt" \
	/usr/local/share/ca-certificates/pqccomms-client-ca.crt

# Remove the legacy package
apt-get remove -y "$LEGACY_PKG"

# Update CA certificates
update-ca-certificates --fresh
