#!/bin/sh
set -e

# Source debconf library
. /usr/share/debconf/confmodule

# Define package name
PACKAGE="apt-ostree"

# Function to log messages
log() {
    echo "$PACKAGE: $1" >&2
}

# Function to cleanup shell completions
cleanup_completions() {
    log "Cleaning up shell completions..."
    
    # Remove bash completion
    if [ -f /usr/share/bash-completion/completions/apt-ostree ]; then
        rm -f /usr/share/bash-completion/completions/apt-ostree
    fi
    
    # Remove zsh completion
    if [ -f /usr/share/zsh/vendor-completions/_apt-ostree ]; then
        rm -f /usr/share/zsh/vendor-completions/_apt-ostree
    fi
    
    # Reload bash completion if available (skip if problematic)
    # if [ -f /etc/bash_completion ]; then
    #     . /etc/bash_completion
    # fi
}

# Function to cleanup man pages
cleanup_man_pages() {
    log "Cleaning up man pages..."
    
    # Remove man page
    if [ -f /usr/share/man/man1/apt-ostree.1 ]; then
        rm -f /usr/share/man/man1/apt-ostree.1
    fi
    
    # Update man page database
    if command -v mandb >/dev/null 2>&1; then
        mandb -q || true
    fi
}

# Function to cleanup daemon service
cleanup_daemon() {
    log "Cleaning up apt-ostreed daemon..."
    
    # Check if systemd is available
    if ! command -v systemctl >/dev/null 2>&1; then
        log "Warning: systemd not available, skipping service cleanup"
        return 0
    fi
    
    # Stop the service if running
    if systemctl is-active --quiet apt-ostreed.service; then
        log "Stopping apt-ostreed service..."
        systemctl stop apt-ostreed.service || true
    fi
    
    # Disable the service
    if systemctl is-enabled --quiet apt-ostreed.service; then
        log "Disabling apt-ostreed service..."
        systemctl disable apt-ostreed.service || true
    fi
    
    # Reload systemd daemon
    systemctl daemon-reload || true
}

# Function to cleanup daemon files
cleanup_daemon_files() {
    log "Cleaning up daemon files..."
    
    # Remove systemd service files
    if [ -f /lib/systemd/system/apt-ostreed.service ]; then
        rm -f /lib/systemd/system/apt-ostreed.service
    fi
    
    if [ -f /lib/systemd/system/apt-ostreed.socket ]; then
        rm -f /lib/systemd/system/apt-ostreed.socket
    fi
    
    # Remove polkit policy
    if [ -f /usr/share/polkit-1/actions/org.projectatomic.aptostree1.policy ]; then
        rm -f /usr/share/polkit-1/actions/org.projectatomic.aptostree1.policy
    fi
    
    # Remove configuration files
    if [ -d /etc/apt-ostreed ]; then
        rm -rf /etc/apt-ostreed
    fi
    
    # Remove binary
    if [ -f /usr/libexec/apt-ostreed ]; then
        rm -f /usr/libexec/apt-ostreed
    fi
}

# Main execution
case "$1" in
    remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
        cleanup_completions
        cleanup_man_pages
        cleanup_daemon
        ;;
    purge)
        cleanup_completions
        cleanup_man_pages
        cleanup_daemon
        cleanup_daemon_files
        ;;
    *)
        log "Unknown action: $1"
        exit 1
        ;;
esac

exit 0
