#!/bin/sh
# $Id: gpg_kmail.sh,v 1.5 2004/08/28 00:47:16 ewalstad Exp $
#
# Copyright (C) 2004 Eric Walstad <eric@ericwalstad.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# This script is meant to setup using the encryption plugins with KMail
# It works for me on my Fedora Core 2 systems.
# Your mileage may vary.
#
# If you modify the script to make it work on your system, please
# share your changes with me so that I can update this script.
# Good luck!  -E

# Modify thes variables to suit your system's setup
BASE_DIR="/home/ewalstad/var/download"        # My download directory
ENC_DIR=$BASE_DIR/encryption                  # The directory into which the tarballs will go
GPG_CONF="~/.gnupg/gpg.conf"                  # The location of the GnuPG config file
GPG_AGENT_CONF="~/.gnupg/gpg-agent.conf"      # The location of the gpg-agent config file.
                                              # It might not exist yet, which is fine.
STARTUP_CONF="/usr/bin/startkde"              # The location of the kde startup script.
KEY_ID="8659C7DA7A61292E"                     # Enter the key id for your gnupg key here

THE_DATE=$(date +%Y%m%d%s)

mv $ENC_DIR $BASE_DIR/encryption.$THE_DATE
mkdir $ENC_DIR
cd $ENC_DIR

wget ftp://ftp.gnupg.org/gcrypt/alpha/libgpg-error/libgpg-error-0.7.tar.gz
wget ftp://ftp.gnupg.org/gcrypt/alpha/libgpg-error/libgpg-error-0.7.tar.gz.sig
wget ftp://ftp.gnupg.org/gcrypt/alpha/libgcrypt/libgcrypt-1.1.94.tar.gz
wget ftp://ftp.gnupg.org/gcrypt/alpha/libgcrypt/libgcrypt-1.1.94.tar.gz.sig
wget ftp://ftp.gnupg.org/gcrypt/alpha/libassuan/libassuan-0.6.6.tar.gz
wget ftp://ftp.gnupg.org/gcrypt/alpha/libassuan/libassuan-0.6.6.tar.gz.sig
wget ftp://ftp.gnupg.org/gcrypt/alpha/libksba/libksba-0.9.8.tar.gz
wget ftp://ftp.gnupg.org/gcrypt/alpha/libksba/libksba-0.9.8.tar.gz.sig
wget ftp://ftp.gnupg.org/gcrypt/alpha/gnupg/gnupg-1.9.9.tar.gz
wget ftp://ftp.gnupg.org/gcrypt/alpha/gnupg/gnupg-1.9.9.tar.gz.sig
wget ftp://ftp.gnupg.org/gcrypt/gpgme/gpgme-0.3.16.tar.gz
wget ftp://ftp.gnupg.org/gcrypt/gpgme/gpgme-0.3.16.tar.gz.sig
wget ftp://ftp.gnupg.org/gcrypt/alpha/cryptplug/cryptplug-0.3.16.tar.gz
wget ftp://ftp.gnupg.org/gcrypt/alpha/cryptplug/cryptplug-0.3.16.tar.gz.sig
wget ftp://ftp.gnupg.org/gcrypt/pinentry/pinentry-0.7.1.tar.gz
wget ftp://ftp.gnupg.org/gcrypt/pinentry/pinentry-0.7.1.tar.gz
wget ftp://ftp.gnu.org/gnu/pth/pth-2.0.1.tar.gz
wget ftp://ftp.gnu.org/gnu/pth/pth-2.0.1.tar.gz.sig

tar xzf libgpg-error-0.7.tar.gz
tar xzf gnupg-1.9.9.tar.gz
tar xzf gpgme-0.3.16.tar.gz
tar xzf libgcrypt-1.1.94.tar.gz
tar xzf libksba-0.9.8.tar.gz
tar xzf pinentry-0.7.1.tar.gz
tar xzf cryptplug-0.3.16.tar.gz
tar xzf libassuan-0.6.6.tar.gz
tar xzf pth-2.0.1.tar.gz
ln -s cryptplug-0.3.16 cryptplug
ln -s gnupg-1.9.9 gnupg
ln -s gpgme-0.3.16 gpgme
ln -s libgcrypt-1.1.94 libgcrypt
ln -s libksba-0.9.8 libksba
ln -s pinentry-0.7.1 pinentry
ln -s libassuan-0.6.6 libassuan
ln -s pth-2.0.1 pth
ln -s libgpg-error-0.7 libgpg-error

cd $ENC_DIR/libgpg-error && ./configure && make && su -c "make install"
cd $ENC_DIR/libgcrypt && ./configure && make && make check && su -c "make install"
cd $ENC_DIR/libassuan && ./configure && make && su -c "make install"
cd $ENC_DIR/libksba && ./configure && make && make check && su -c "make install"
cd $ENC_DIR/pth && ./configure --prefix=/usr && make && su -c "make install"
cd $ENC_DIR/gnupg && ./configure && make && make check && su -c "make install"
cd $ENC_DIR/gpgme  && ./configure --without-gpgsm && make && make check && su -c "make install"
cd $ENC_DIR/pinentry/qt && ln -s $ENC_DIR/pinentry/pinentry/pinentry.h pinentry.h
cd $ENC_DIR/pinentry && ./configure --disable-pinentry-gtk --disable-pinentry-curses --disable-fallback-curses && make && su -c "make install"
cd $ENC_DIR/cryptplug && ./configure && make && su -c "make install"

su -c "cp /etc/ld.so.conf /etc/ld.so.conf.$(date +%Y%m%d%s)"
su -c "echo \"/usr/local/lib\" >> /etc/ld.so.conf"
su -c "/sbin/ldconfig"

gpg --rebuild-keydb-caches

mv $GPG_CONF $GPG_CONF.$THE_DATE
sed 's/# use-agent/use-agent/g' $GPG_CONF.$THE_DATE > $GPG_CONF

mv $GPG_AGENT_CONF $GPG_AGENT_CONF.$THE_DATE
echo "pinentry-program /usr/local/bin/pinentry-qt" > $GPG_AGENT_CONF
echo "no-grab" >> $GPG_AGENT_CONF
echo "default-cache-ttl 1800" >> $GPG_AGENT_CONF

eval "$(gpg-agent --daemon)"

echo "test" | gpg -ase -r 0x$KEY_ID | gpg

echo "More info can be found in the HOWTO at:"
echo "<http://kmail.kde.org/kmail-pgpmime-15-16-howto.html>"
echo "also:"
echo "<http://savannah.gnu.org/faq/?group_id=5802&question=How_to_install_and_use_gpg-agent.txt>"
echo "Configure KMail to use CryptPlugs"
echo "Basically you want to add a 'CryptoPlugin' called OpenPGP that points to:"
echo "/usr/local/lib/cryptplug/gpgme-openpgp.so"
echo "After doing that, you should be able to choose the OpenPGP plugin"
echo "for signing/encrypting in KMail's composer."
echo "NOTE: to have KMail work at startup, you must modify:"
echo "$STARTUP_CONF"
echo "adding the following line at the top of the file (after the first set of comments):"
echo 'eval "$(gpg-agent --daemon)"'
# Anyone know how to (simply) stuff lines of text inside a text file?
# The lines below would probably work, but it would leave you with two
# #!/bin/sh
# lines, which I think is ugly and may cause problems.  I didn't try it
# and I ran out of time to muck with it.
#cp $STARTUP_CONF $STARTUP_CONF.$THE_DATE
#echo "#!/bin/sh" > $STARTUP_CONF
#echo "# EW Added for KMail and gpg-agent integration" >> $STARTUP_CONF
#echo 'eval "$(gpg-agent --daemon)"' >> $STARTUP_CONF
#cat $STARTUP_CONF.$THE_DATE >> $STARTUP_CONF

