#!/bin/sh

########################################################
## ez-pine-gpg install                                ##
## part of ez-pine-gpg v0.4g // gpg filters for pine  ##
## http://Business-PHP.com/opensource/ez-pine-gpg/    ##
## (c) 17 Feb 2004, Atom Emet                         ##
## (c) 15 Nov 2004, Atom Emet                         ##
## Atom {at} Business-PHP.com                         ##
## Distributed under the GNU General Public License   ##
## http://www.gnu.org/copyleft/gpl.html               ##
##                                                    ##
##  ################################################  ##
##                                                    ##
## 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.                       ##
########################################################

###############################################
## announce what's happening and version number
echo 'Installing ez-pine-gpg v0.4g'
echo

#########################################
## make sure there's one argument or else
## display instructions and exit
if [ "${#}" != 1 ]
then
    echo "Usage:    ${0} /install/directory/" 1>&2
    echo 1>&2
    echo 'This installation script takes one argument which is' 1>&2
    echo 'the directory to install ez-pine-gpg into.' 1>&2
    echo 1>&2
    echo 'If this is a system-wide install "/usr/local/bin/" might be a good choice.' 1>&2
    echo "If this is an install just for you \"${HOME}/bin/\" is probably best." 1>&2
    exit 1
fi

##############################
## "install_dir" is argument 1
## with no trailing slashes
install_dir=`echo ${1} | sed 's,/*$,,'`

#####################################################
## make sure that "install_dir" really is a directory
if [ ! -d "${install_dir}" ]
then
    echo "\"${install_dir}\" - That's not a directory!" 1>&2
    echo "I can't install anything there!" 1>&2
    exit 2
fi

#########################################################
## make sure we have permission to write to "install_dir"
if [ ! -w "${install_dir}" ]
then
    echo "I can't write to \"${install_dir}\"" 1>&2
    echo "I can't install anything there!" 1>&2
    exit 3
fi

###############################################
## find a path that includes everything we need
path=`which 822fields egrep find gpg head sed tr date uname printf clear stty \
	'[' | xargs -n 1 dirname | sort -u | tr '\n' :`"${install_dir}"

######################################################
## for each script write a PATH into it and install it
for n in ez-pine-gpg-sign ez-pine-gpg-encrypt ez-pine-gpg-sign-and-encrypt ez-pine-gpg-symmetric ez-pine-gpg-incoming ez-pine-gpg-helper-recipient ez-pine-gpg-helper-sender
do
    ## remove any file of the same name, if it exists
    rm -f ${install_dir}/${n}
    ## make sure we can create a file there, by creating a file there
    echo > ${install_dir}/${n}
    ## while the file is still empty give it reasonable permissions
    chmod 0755 ${install_dir}/${n}
    ## place the script into the file while specifying the PATH
    sed "s!^PATH=.*\$!PATH=${path}!" $n > ${install_dir}/${n}
    ## notify user what just happened
    echo "installing:   ${install_dir}/${n}"
done

#############
## we're done
echo
echo "Done installing!"
echo
echo "Now configure pine filters."
echo "In pine, press: [M]ain [S]etup [C]onfigure"
echo "and add these filters:"
echo
echo "display-filters =  _LEADING(\"-----BEGIN PGP\")_ ${install_dir}/ez-pine-gpg-incoming"
echo "sending-filters =  ${install_dir}/ez-pine-gpg-sign-and-encrypt _INCLUDEALLHDRS_ _RECIPIENTS_"
echo "                   ${install_dir}/ez-pine-gpg-encrypt _RECIPIENTS_"
echo "                   ${install_dir}/ez-pine-gpg-sign _INCLUDEALLHDRS_"
echo
echo "You may also install the ez-pine-gpg-symmetric sending filter. See the README for details."

#######
## exit
exit 0
