#! /bin/csh -f

if ("$#argv" < "1") then
    echo "Usage:	install binary"
    exit 1
endif
if ("$#argv" > "2") then
    echo "Usage:	install binary"
    exit 1
endif

set binary = $argv[1]
set mortal = "FALSE"

if ("$#argv" == "2") then
    if ("$argv[2]" != "-DUSE_OLD_SENDMAIL") then
	echo "Illegal argument: $argv[2]"
	exit 1
    endif
    set mortal = "TRUE"
endif

if ("$binary" != "pgpsendmail") then
    if ("$?USRBINPATH" == "0") then
	# Where to install?
	echo "No USRBINPATH environment variable defined."
	echo "Don't know where to install  $binary"
	echo "You will have to do it yourself."
	exit 0
    endif
    if ( ! -f $USRBINPATH/$binary ) then
	cp -p $binary $USRBINPATH
	exit 0
    endif
    mv $USRBINPATH/$binary $USRBINPATH/${binary}.old
    cp -p $binary $USRBINPATH
    if ("$status" != "0") then
	mv $USRBINPATH/${binary}.old $USRBINPATH/$binary
	exit 1
    endif
    rm -f $USRBINPATH/${binary}.old
    exit 0
endif

if ( ! -r /usr/include/paths.h) then
    echo "Cannot read /usr/include/paths.h"
    exit 1
endif
if (`fgrep -c PATH_SENDMAIL /usr/include/paths.h` != "1") then
    echo "Single definition of PATH_SENDMAIL not found in /usr/include/paths.h"
    exit 1
endif
set tmp = `fgrep PATH_SENDMAIL /usr/include/paths.h | cut -d'"' -f 2`
if ( ! -x $tmp ) then
    echo "File: $tmp is not executable"
    exit 1
endif
if ("$tmp:t" != "sendmail") then
    echo "Filename: $tmp:t is not sendmail"
    exit 1
endif
set sendmail_dir = $tmp:h

if (-f ${sendmail_dir}/pgpsendmail) then
    # Existing copy
    if ("$USER" == "root") then
	if ("$?PGPPATH" != "0") then
	    echo "WARNING: Do not restart the  sendmail  daemon while you"
	    echo "still have a PGPPATH environment variable"
	endif
    endif
    if ( ! -w ${sendmail_dir}/pgpsendmail ) then
	echo "You cannot over-write  ${sendmail_dir}/pgpsendmail"
	exit 1
    endif
    cp -p pgpsendmail ${sendmail_dir}/pgpsendmail.new
    mv -f ${sendmail_dir}/pgpsendmail.new ${sendmail_dir}/pgpsendmail
    echo "PGPsendmail installed as ${sendmail_dir}/pgpsendmail"
    if ("$mortal" == "TRUE") then
	echo "PGPsendmail will expect the real sendmail is ${sendmail_dir}/sendmail"
    else
	echo "PGPsendmail will expect the real sendmail is ${sendmail_dir}/sendmail.real"
    endif
    exit 0
endif

# New copy
if ( ! -w ${sendmail_dir} ) then
    echo "You cannot put files in  ${sendmail_dir}"
    if ("$?USRBINPATH" != "0") then
	if (-w $USRBINPATH) then
	    echo "Writing PGPsendmail as:  $USRBINPATH/pgpsendmail  instead"
	    cp -p pgpsendmail $USRBINPATH
	    exit 0
	endif
    endif
    echo "PGPsendmail not installed. Do it yourself."
    exit 2
endif
cp -p pgpsendmail ${sendmail_dir}
cd ${sendmail_dir}
mv sendmail sendmail.real
ln -s pgpsendmail sendmail

echo "PGPsendmail installed as ${sendmail_dir}/pgpsendmail"
if ("$mortal" == "TRUE") then
    echo "PGPsendmail will expect the real sendmail is ${sendmail_dir}/sendmail"
else
    echo "PGPsendmail will expect the real sendmail is ${sendmail_dir}/sendmail.real"
endif

if ("$USER" == "root") then
    if ("$?PGPPATH" != "0") then
	echo "WARNING: Do not restart the  sendmail  daemon while you"
	echo "still have a PGPPATH environment variable"
    endif
endif

exit 0
