Imported Upstream version 1.5.4
[platform/upstream/gpgme.git] / tests / start-stop-agent
1 #!/bin/sh
2 # Copyright (C) 2013 g10 Code GmbH
3 #
4 # This file is free software; as a special exception the author gives
5 # unlimited permission to copy and/or distribute it, with or without
6 # modifications, as long as this notice is preserved.  This file is
7 # distributed in the hope that it will be useful, but WITHOUT ANY
8 # WARRANTY, to the extent permitted by law; without even the implied
9 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
11 if [ -z "$(command -v gpg-connect-agent)" ]; then
12    echo "gpg-agent not installed and thus not started" >&2
13    exit 0;
14 fi
15
16 GPG_AGENT_INFO=
17 export GPG_AGENT_INFO
18
19 token=$(echo "gpgme-$(pwd)" | tr ' ' '_')
20
21 if [ "$1" = "--stop" ]; then
22   if [ "$(gpg-connect-agent getval\ $token /bye 2>/dev/null | head -1)" \
23         != "D set" ]; then
24     echo "gpg-agent not running" >&2
25     exit 0
26   fi
27   echo "stopping gpg-agent " >&2
28   gpg-connect-agent KILLAGENT /bye >/dev/null 2>&1
29   exit 0
30 fi
31
32 if [ "$(gpg-connect-agent getval\ $token /bye 2>/dev/null | head -1)" \
33       = "D set" ]; then
34   echo "gpg-agent already running" >&2
35   exit 0
36 fi
37
38 echo "starting gpg-agent " >&2
39 gpg-connect-agent putval\ $token\ set /bye >/dev/null 2>&1
40 if [ "$(gpg-connect-agent getval\ $token /bye 2>/dev/null | head -1)" \
41       != "D set" ]; then
42   echo "error starting gpg-agent" >&2
43   exit 1
44 fi
45 exit 0