# Colored makepkg-like functions
msg_blue() {
    printf "${blue}==>${bold} $1${all_off}\n"
}

note() {
    printf "${blue}==>${yellow} NOTE:${bold} $1${all_off}\n"
}

all_off="$(tput sgr0)"
bold="${all_off}$(tput bold)"
blue="${bold}$(tput setaf 4)"
yellow="${bold}$(tput setaf 3)"

post_install() {
    note "Pepper Flash is now auto-detected from: /usr/lib/PepperFlash/ (AUR/pepper-flash)"
    note "Custom flags should be put directly in: ~/.config/chromium-flags.conf"
    note "The launcher is called: 'chromium-gost'"
    
    # System-wide package configuration.
    DEFAULTS_FILE="/etc/default/chromium-gost"

    # Add icons to the system icons
    XDG_ICON_RESOURCE="`command -v xdg-icon-resource 2> /dev/null || true`"
    if [[ -x ${XDG_ICON_RESOURCE} ]]; then
	for icon in product_logo_64.png product_logo_128.png product_logo_16.png product_logo_48.png product_logo_24.png product_logo_32.png product_logo_256.png; do
	    size="$(echo ${icon} | sed 's/[^0-9]//g')"
	    ${XDG_ICON_RESOURCE} install --size "${size}" "/opt/chromium-gost/${icon}" "chromium-gost"
	done
    else
	echo "Error: Could not find xdg-icon-resource" >&2
	return 1
    fi

    UPDATE_MENUS="`command -v update-menus 2> /dev/null || true`"
    if [ -x "$UPDATE_MENUS" ]; then
	update-menus
    fi

    # Updates defaults.list file if present.
    update_defaults_list() {
	# $1: name of the .desktop file
	local DEFAULTS_LIST="/usr/share/applications/defaults.list"
	if [ ! -f "${DEFAULTS_LIST}" ]; then
	    return
	fi
	# Split key-value pair out of MimeType= line from the .desktop file,
	# then split semicolon-separated list of mime types (they should not contain
	# spaces).
	mime_types="$(grep MimeType= /usr/share/applications/${1} | cut -d '=' -f 2- | tr ';' ' ')"
	for mime_type in ${mime_types}; do
	    if egrep -q "^${mime_type}=" "${DEFAULTS_LIST}"; then
    		if ! egrep -q "^${mime_type}=.*${1}" "${DEFAULTS_LIST}"; then
    		    default_apps="$(grep ${mime_type}= "${DEFAULTS_LIST}" | cut -d '=' -f 2-)"
    		    egrep -v "^${mime_type}=" "${DEFAULTS_LIST}" > "${DEFAULTS_LIST}.new"
    		    echo "${mime_type}=${default_apps};${1}" >> "${DEFAULTS_LIST}.new"
    		    mv "${DEFAULTS_LIST}.new" "${DEFAULTS_LIST}"
    		fi
	    else
    		# If there's no mention of the mime type in the file, add it.
    		echo "${mime_type}=${1};" >> "${DEFAULTS_LIST}"
	    fi
	done
    }
    update_defaults_list "chromium-gost.desktop"

    # This function uses sed to insert the contents of one file into another file,
    # after the first line matching a given regular expression. If there is no
    # matching line, then the file is unchanged.
    insert_after_first_match() {
	# $1: file to update
	# $2: regular expression
	# $3: file to insert
	sed -i -e "1,/$2/ {
	    /$2/ r $3
	    }" "$1"
    }

    # If /usr/share/gnome-control-center/default-apps/gnome-default-applications.xml
    # exists, it may need to be updated to add ourselves to the default applications
    # list. If we find the file and it does not seem to contain our patch already
    # (the patch is safe to leave even after uninstall), update it.
    GNOME_DFL_APPS=/usr/share/gnome-control-center/default-apps/gnome-default-applications.xml
    if [ -f "$GNOME_DFL_APPS" ]; then
	# Conditionally insert the contents of the file "default-app-block" after the
	# first "<web-browsers>" line we find in gnome-default-applications.xml
	fgrep -q "Chromium-Gost" "$GNOME_DFL_APPS" || insert_after_first_match \
	    "$GNOME_DFL_APPS" \
	    "^[ 	]*<web-browsers>[ 	]*$" \
	    "/opt/chromium-gost/default-app-block"
    fi

    # This function performs the setup for the chrome management service process.
    # It creates a new chromemgmt group, creates the signing key file, and updates
    # permissions for both the signing key file and the binary.
    chrome_management_service_setup() {
	if [ ! -f "$DEFAULTS_FILE" ]; then
	    return
	fi
        if ! grep -q "install_device_trust_key_management_command=true" "$DEFAULTS_FILE"; then
	    return
	fi
	getent group chromemgmt > /dev/null || groupadd chromemgmt
	chgrp chromemgmt "/opt/chromium-gost/chrome-management-service"
	chmod 2755 "/opt/chromium-gost/chrome-management-service"
	mkdir -p "/etc/chromium/policies/enrollment"
	SIGNING_KEY_FILE="/etc/chromium/policies/enrollment/DeviceTrustSigningKey"
	if [ ! -e "$SIGNING_KEY_FILE" ]; then
	    touch "$SIGNING_KEY_FILE"
	    chgrp chromemgmt "$SIGNING_KEY_FILE"
	    chmod 664 "$SIGNING_KEY_FILE"
	fi
    }
    chrome_management_service_setup

    ## MAIN ##
    # Run the cron job immediately to perform repository configuration.
    nohup sh /etc/cron.daily/chromium-gost > /dev/null 2>&1 &
}

pre_remove() {
    # Remove icons from the system icons
    XDG_ICON_RESOURCE="`command -v xdg-icon-resource 2> /dev/null || true`"
    if [ -x "$XDG_ICON_RESOURCE" ]; then
	for icon in product_logo_64.png product_logo_128.png product_logo_16.png product_logo_48.png product_logo_24.png product_logo_32.png product_logo_256.png ; do
	    size="$(echo ${icon} | sed 's/[^0-9]//g')"
	    "$XDG_ICON_RESOURCE" uninstall --size "${size}" "chromium-gost"
	done
    else
	echo "Error: Could not find xdg-icon-resource" >&2
	return 1
    fi

    UPDATE_MENUS="`command -v update-menus 2> /dev/null || true`"
    if [ -x "$UPDATE_MENUS" ]; then
	update-menus
    fi
}

post_remove() {
    # System-wide package configuration.
    DEFAULTS_FILE="/etc/default/chromium-gost"
    # Only remove the defaults file if it is not empty. An empty file was probably
    # put there by the sysadmin to disable automatic repository configuration, as
    # per the instructions on the package download page.
    if [ -s "$DEFAULTS_FILE" ]; then
	# Make sure the package defaults are removed before the repository config,
	# otherwise it could result in the repository config being removed, but the
	# package defaults remain and are set to not recreate the repository config.
	# In that case, future installs won't recreate it and won't get auto-updated.
	rm "$DEFAULTS_FILE" || return 1
    fi
}
