#!/bin/bash

statedir="/tmp/keepassxc"
mkdir -p "$statedir"

pidfile="$statedir/pid"
fifo="$statedir/fifo"

keepass_running()
{
    test -e "$pidfile" && {
	KPID="$(<$pidfile)"
	if ps -q "$KPID" >/dev/null 2>&1; then
	    zenity --info --text "Keepass läuft schon unter pid $KPID"
	    return 0
	fi
	rm "$pidfile"
    }

    return 1
}

start_keepass()
{
    if ! keepass_running; then
	flatpak run org.keepassxc.KeePassXC --pw-stdin "$@" \
	    ~/Nextcloud/data/Gemeinsam/keepass/LinusKeyPass.kdbx \
	    < ~/.local/share/keepass &> /dev/null &
	echo "$!" > "$pidfile" 
    fi
}

test -e "$fifo" || mkfifo "$fifo"
while true; do
    read l < "$fifo"
    case "$l" in
	start-keepass)
	    start_keepass
	    ;;
	done)
	    exit 0
	    ;;
    esac
done


