#! @SHELL@
#
#
# Copyright 2001-2020 S. Varshavchik.  See COPYING for
# distribution information.
#
# This scripts initializes Maildir/gpg, enabling GPG code for the given
# maildir.

GPG=@GPG@

maildir="$1"

# Sanity check

test ! -d "$maildir" && echo "$maildir: not a directory" >&2 && exit 1
test ! -d "$maildir/cur" && echo "$maildir: not a directory" >&2 && exit 1
test ! -d "$maildir/new" && echo "$maildir: not a directory" >&2 && exit 1
test ! -d "$maildir/tmp" && echo "$maildir: not a directory" >&2 && exit 1

umask 077

mkdir -p $maildir/gpg || exit 1

if test -f $maildir/gpg/options
then
	rm -f $maildir/gpg/options
	echo "$maildir/gpg/options removed, installing gpg.conf"
fi

cat >$maildir/gpg/gpg.conf <<EOF

use-agent
pinentry-mode loopback
escape-from-lines
lock-once
comment ""
no-secmem-warning

EOF

cat >$maildir/gpg/gpg-agent.conf <<EOF
allow-loopback-pinentry
EOF

owner=`@PERL@ -e '@a=stat "'$maildir'"; print "$a[4]:$a[5]";'`

chown -R $owner $maildir/gpg

echo "$maildir/gpg/gpg.conf and gpg-agent.conf created,"
echo "please verify that the options are correct."

echo "See INSTALL for more information."
