#!/bin/bash

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

pidfile="$statedir/pid"

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 &
	STAT=$?
	echo "$!" > "$pidfile" 
	return $STAT
    fi

    return 0
}

start_keepass

