post_install() {
    :
#!/bin/bash
# Program:
#	script to be run after package installation

echo "run post install script, action is $1..."

update-mime-database /usr/share/mime || true
#update-desktop-database || true
if [ -x "/usr/bin/update-desktop-database" ]; then 
	update-desktop-database || true
else
    echo "/usr/bin/update-desktop-database command is missing! apt install desktop-file-utils and run this command again."
fi
#add chrome-sandbox setuid access  
if [ -e "/opt/zoom/cef/chrome-sandbox" ]; then 
	chown root /opt/zoom/cef/chrome-sandbox && chmod 4755 /opt/zoom/cef/chrome-sandbox || true
fi

# AppArmor profile install if ABI file exists
ABI_PATH="/etc/apparmor.d/abi/4.0"
ZOOM_PROFILE_PATH="/etc/apparmor.d/zoom"
IDENTIFIER="zoom-default-configuration"

if [ -f "$ABI_PATH" ]; then
    echo "Installing AppArmor profile to $ZOOM_PROFILE_PATH"

    should_write=false
    if [ -f "$ZOOM_PROFILE_PATH" ]; then
        if grep -q "$IDENTIFIER" "$ZOOM_PROFILE_PATH"; then
            echo "Existing profile contains identifier '$IDENTIFIER', proceeding to overwrite."
            should_write=true
        else
            echo "Existing profile does not contain identifier '$IDENTIFIER', skipping overwrite."
            should_write=false
        fi
    else
    	should_write=true
    fi

    if [ "$should_write" = true ]; then
        cat <<EOF | tee "$ZOOM_PROFILE_PATH" > /dev/null
# This is a default AppArmor profile for Zoom WebviewHost
# Identifier: zoom-default-configuration
# Remove the above line to prevent automatic updates by scripts.

abi <abi/4.0>,
include if exists <tunables/global>

profile zoom /opt/zoom/ZoomWebviewHost flags=(unconfined) {
  userns,

  # Site-specific additions and overrides. See local/README for details.
  include if exists <local/zoom>
}
EOF

        # Load or replace the AppArmor profile
        if command -v apparmor_parser >/dev/null 2>&1; then
            echo "Loading AppArmor profile..."
            apparmor_parser -r "$ZOOM_PROFILE_PATH" || true
        else
            echo "apparmor_parser not found! Please install it with: sudo apt install apparmor-utils"
        fi
    fi
fi


}
post_upgrade() {
    :
#!/bin/bash
# Program:
#	script to be run after package upgrade

echo "run post upgrade script, action is $1..."

#add chrome-sandbox setuid access  
if [ -e "/opt/zoom/cef/chrome-sandbox" ]; then 
	chown root /opt/zoom/cef/chrome-sandbox && chmod 4755 /opt/zoom/cef/chrome-sandbox || true
fi

# AppArmor profile install if ABI file exists
ABI_PATH="/etc/apparmor.d/abi/4.0"
ZOOM_PROFILE_PATH="/etc/apparmor.d/zoom"
IDENTIFIER="zoom-default-configuration"

if [ -f "$ABI_PATH" ]; then
    echo "Installing AppArmor profile to $ZOOM_PROFILE_PATH"

    should_write=false
    if [ -f "$ZOOM_PROFILE_PATH" ]; then
        if grep -q "$IDENTIFIER" "$ZOOM_PROFILE_PATH"; then
            echo "Existing profile contains identifier '$IDENTIFIER', proceeding to overwrite."
            should_write=true
        else
            echo "Existing profile does not contain identifier '$IDENTIFIER', skipping overwrite."
            should_write=false
        fi
    else
    	should_write=true
    fi

    if [ "$should_write" = true ]; then
        cat <<EOF | tee "$ZOOM_PROFILE_PATH" > /dev/null
# This is a default AppArmor profile for Zoom WebviewHost
# Identifier: zoom-default-configuration
# Remove the above line to prevent automatic updates by scripts.

abi <abi/4.0>,
include if exists <tunables/global>

profile zoom /opt/zoom/ZoomWebviewHost flags=(unconfined) {
  userns,

  # Site-specific additions and overrides. See local/README for details.
  include if exists <local/zoom>
}
EOF

        # Load or replace the AppArmor profile
        if command -v apparmor_parser >/dev/null 2>&1; then
            echo "Loading AppArmor profile..."
            apparmor_parser -r "$ZOOM_PROFILE_PATH" || true
        else
            echo "apparmor_parser not found! Please install it with: sudo apt install apparmor-utils"
        fi
    fi
fi

}
post_remove() {
    :
#!/bin/bash
# Program:
#	script to be run after package removal

echo "run post uninstall script, action is $1 ..."

[ "$1" = "remove" ] || [ "$1" = "purge" ] || exit 0

if [ -L "/usr/bin/zoom" ]; then 
	rm /usr/bin/zoom 
fi 

ZOOM_PROFILE_PATH="/etc/apparmor.d/zoom"
IDENTIFIER="zoom-default-configuration"

if [ -f "$ZOOM_PROFILE_PATH" ]; then
    if grep -q "$IDENTIFIER" "$ZOOM_PROFILE_PATH"; then
        echo "Removing AppArmor profile: $ZOOM_PROFILE_PATH (matched identifier '$IDENTIFIER')"
        rm "$ZOOM_PROFILE_PATH"
    else
        echo "AppArmor profile exists but does not contain identifier '$IDENTIFIER'. Skipping removal."
    fi
fi

}
