#!/bin/sh
set -e

case "$1" in
  configure|abort-upgrade|abort-remove|abort-deconfigure)
    # Backward compatibility: older broken packages installed the ASCII key as
    # a file inside a directory (/usr/share/keyrings/saikyo-archive-keyring.asc/...).
    if [ ! -f /usr/share/keyrings/saikyo-archive-keyring.asc ] && [ -f /usr/share/keyrings/saikyo-archive-keyring.asc/saikyo-archive-keyring.asc ]; then
      mv -f /usr/share/keyrings/saikyo-archive-keyring.asc/saikyo-archive-keyring.asc /usr/share/keyrings/saikyo-archive-keyring.asc 2>/dev/null || true
      rmdir /usr/share/keyrings/saikyo-archive-keyring.asc 2>/dev/null || true
      chmod 0644 /usr/share/keyrings/saikyo-archive-keyring.asc 2>/dev/null || true
    fi

    # Convert shipped ASCII key to binary keyring used by apt Signed-By.
    if command -v gpg >/dev/null 2>&1 && [ -f /usr/share/keyrings/saikyo-archive-keyring.asc ]; then
      gpg --dearmor --yes -o /usr/share/keyrings/saikyo-archive-keyring.gpg /usr/share/keyrings/saikyo-archive-keyring.asc || true
      chmod 0644 /usr/share/keyrings/saikyo-archive-keyring.gpg || true
    fi
    ;;
esac

exit 0
