#!/bin/bash
#Importing gpg key via cli
#https://d.sb/2016/11/gpg-inappropriate-ioctl-for-device-errors
GPG_CONF="$HOME/.gnupg/gpg.conf"
GPG_AGENT_CONF="$HOME/.gnupg/gpg-agent.conf"

function create_file
{
    FILE_PATH="$1"
    CONTENTS="$2"
    DIR=$(dirname "$FILE_PATH")

    mkdir -p "$DIR"
    chmod 700 "$DIR"
    echo "$CONTENTS" > "$FILE_PATH"
}

create_file "$GPG_CONF" "$(cat <<EOF
use-agent 
pinentry-mode loopback
EOF
)"

create_file "$GPG_AGENT_CONF" "$(cat <<EOF
allow-loopback-pinentry
EOF
)"

echo "RELOADAGENT" | gpg-connect-agent
