#!/bin/bash
#
# The contents of this file are subject to the terms of the Common Development and
# Distribution License (the License). You may not use this file except in compliance with the
# License.
#
# You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
# specific language governing permission and limitations under the License.
#
# When distributing Covered Software, include this CDDL Header Notice in each file and include
# the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
# Header, with the fields enclosed by brackets [] replaced by your own identifying
# information: "Portions Copyright [year] [name of copyright owner]".
#
# Copyright 2013-2015 ForgeRock AS.
# Portions Copyright 2026 3A Systems, LLC

set -e
# Post rm script. Package files are removed automatically by the package manager.

if [ "$1" = "remove" ] ; then
    # Mask the removed unit (as dh_installsystemd does): the enable symlink
    # deliberately survives "remove" so the state is restored on re-install,
    # and the mask keeps systemd from logging it as dangling on every boot.
    if command -v deb-systemd-helper >/dev/null 2>&1 ; then
        deb-systemd-helper mask opendj.service >/dev/null 2>&1 || true
    fi
fi

if [ "$1" = "remove" ] || [ "$1" = "purge" ] ; then
    # postinst registers the rc links unconditionally, so drop them on every
    # host - a booted systemd is orthogonal to their existence.
    update-rc.d opendj remove >/dev/null 2>&1 || true
    if [ -d /run/systemd/system ] ; then
        systemctl --system daemon-reload >/dev/null 2>&1 || true
    fi
fi

if [ "$1" = "purge" ] ; then
    if command -v deb-systemd-helper >/dev/null 2>&1 ; then
        deb-systemd-helper purge opendj.service >/dev/null 2>&1 || true
        deb-systemd-helper unmask opendj.service >/dev/null 2>&1 || true
    fi
    echo "*OpenDJ package removed. Server data under ${deb.prefix} (config, db,"
    echo " changelogDb, logs) and the opendj system account are kept; remove"
    echo " them manually if they are no longer needed."
fi

echo
exit 0
# End of the post rm script
