#!/bin/bash

# Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>

# This is a simple program to turn of gpg-agent emulation by
# gnome-keyring in debian jessie.

# see https://bugs.debian.org/760102

USER_AUTO_DIR=~/.config/autostart
USER_AUTO_FILE="${USER_AUTO_DIR}/gnome-keyring-gpg.desktop"
SYSTEM_AUTO_FILE=/etc/xdg/autostart/gnome-keyring-gpg.desktop

if [ "$1" ]; then
    echo "$0 : disable GNOME keyring's gpg-agent emulation"
    echo ""
    echo "    Invoke this program without arguments to tell"
    echo "    GNOME keyring to not try to act as gpg-agent."
    echo ""
    echo " see also: https://bugs.debian.org/760102"
    exit 1
fi


if dpkg --get-selections | grep -qx 'pinentry-gtk2[[:space:]]*install'; then
    echo "You probably want to install the pinentry-gtk2 package."
fi


if [ -e "$USER_AUTO_FILE" ]; then
    if grep -qFx Hidden=true "$USER_AUTO_FILE"; then
        echo "GNOME Keyring's gpg-agent emulation is already disabled."
        echo "You may need to restart your GNOME session."
    else
        echo 'Hidden=true' >> "$USER_AUTO_FILE"
        echo "Added 'Hidden=true' to '$USER_AUTO_FILE'."
        echo "You probably need to restart your GNOME session."
    fi
elif [ -e "$SYSTEM_AUTO_FILE" ]; then
    mkdir -p "$USER_AUTO_DIR"
    cp  "$SYSTEM_AUTO_FILE" "$USER_AUTO_FILE"
    echo 'Hidden=true' >> "$USER_AUTO_FILE"
    echo "Disabled GNOME Keyring's gpg-agent emulation."
    echo "You probably need to restart your GNOME session."
else
    echo "Could not find '$SYSTEM_AUTO_FILE'."
    echo "Maybe GNOME keyring daemon isn't present on your system?"
    exit 1
fi

