#!/bin/sh
#
# Copyright (C) Cameron J. Morland 2000
#
# 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.
#
# File Name:          install
#
# Project Name:       changetrack
#
# Module Description: changetrack installation utility
#
# $Revision: 1.3 $
#
# Author:             Cameron J. Morland

# defaults
APPPATH_DEFAULT="/usr/bin"
MANPATH_DEFAULT="/usr/man/man1"
ETCPATH_DEFAULT="/etc"
HISTORYPATH_DEFAULT="/var/state/changetrack"
MAILFROM_DEFAULT="changetrack@localhost"

# override the defaults if we've installed from here before
d=`dirname $0`
if [ -f $d/.config ]; then
    cat <<EOF
Using previous values from $d/.config
Delete that file if you want the factory defaults.
EOF
    . $d/.config
fi

# show the information about the license (whole thing awfully long)
clear
cat gnu_short.txt

echo ""

#
# Asks a question, giving a default value.  The user can either accept the
# default, or enter something else.
#
# Usage:  ask "question" variable_name default_value
#
ask () {
    question="$1"
    variable="$2"
    default="$3"
    echo -n "$question ($default) " 
    result=''
    read response
    if [ -z "$response" ]; then
	result="$default"
    else
	result="$response"
    fi
    eval $variable=$result
}

#
# Like 'ask', but forces the answer to begin with either 'y' or 'n', case-
# insensitive.  The returned value is always either 'yes' or 'no'.
#
# Usage:  yesno "question" variable_name default_value
#
yesno () {
    question="$1"
    variable="$2"
    default="$3"
    echo -n "$question ($default) " 
    result=''
    read response
    if [ -z "$response" ]; then
	result="$default"
    fi
    while [ -z "$result" ]; do
	short=`expr substr "$response" 1 1`
	if [ "$short" = 'y' -o "$short" = 'Y' ]; then
	    result=yes
	elif [ "$short" = 'n' -o "$short" = 'N' ]; then
	    result=no
	else
	    echo -n "please answer 'yes' or 'no': "
	    read response
	fi
    done
    eval $variable=$result
}

#
# Installs a file to it's proper location.  Unlike the install(1) program,
# the full pathname has to be given for the second argument.  At the same
# time as the install, the following strings are substituted:
#
#     %%APPPATH%%
#     %%MANPATH%%
#     %%ETCPATH%%
#     %%HISTORYPATH%%
#
# Usage:  my_install original destination mode
#
my_install () {
    src="$1"
    dst="$2"
    mode="$3"

    # create the destination directory
    dstdir=`dirname $dst`
    if [ ! -d $dstdir ]; then
	yesno "Should I create the directory '$dstdir'?" answer yes
	if [ "$answer" = no ]; then
	    echo "Aborting."
	    exit 1
	fi
	mkdir -p $dstdir
	if [ $? -ne 0 ]; then
	    echo "Failed to create directory '$dstdir'.  Aborting."
	    exit 1
	fi
    fi

    # a bit of paranoia
    if [ -r $dst ]; then
	rm -f $dst
	if [ $? -ne 0 ]; then
	    echo "$dst already existed and I could not delete it."
	    echo "Aborted."
	    exit 1
	fi
    fi

    # copy over the file, doing substitutions as required
    echo "creating $dst"
    escaped_mailfrom=`echo $MAILFROM | perl -pe 's,\@,\\\\@,;'`
    perl -p \
	-e "s,%%APPPATH%%,$APPPATH,g;" \
	-e "s,%%MANPATH%%,$MANPATH,g;" \
	-e "s,%%ETCPATH%%,$ETCPATH,g;" \
	-e "s,%%HISTORYPATH%%,$HISTORYPATH,g;" \
	-e "s,%%MAILFROM%%,$escaped_mailfrom,g;" \
	< $src > $dst
    if [ $? -ne 0 ]; then
	echo "Failed to copy $src to $dst."
	echo "Aborted."
	exit 1
    fi
    chmod $mode $dst
    if [ $? -ne 0 ]; then
	echo "Failed to chmod $dst to mode '$mode'."
	echo "Aborted."
	exit 1
    fi
}

########## Determine what's up with Perl
if [ `perl -e 'print 1;'` ] ;  then
	# some version of Perl exists
	echo "Perl detected"
	if [ `perl -e 'if($] >= 5) {print 1;} else {}'` ] ; then
		# Perl 5 or greater detected
		echo "Perl version OK."
		PERL=1
	else
		# Perl 5 not detected
		perl -e 'print "Perl version $] detected.\n"'	
		echo "Changetrack has been tested only with Perl 5."
		yesno "Install anyway?" YN no
		if [ "$YN" = no ]; then
			echo "Installation cancelled."
			echo "Perl is available at www.perl.com"
			exit 1
		else
			echo "Installing with old Perl."
			echo "Upgrading Perl is recommended."
			echo "Perl is available at www.perl.com"
			PERL=0
		fi
	fi
else
	# no version of Perl exists.
	cat <<EOF

Perl not detected.  Make sure Perl is in your path.
Changetrack requires at least Perl version 5, available from www.perl.com.

Installation cancelled.

EOF
	exit 1
fi

PERLPATH=`which perl`
export perlpath=`which perl`   # pass variable to perl. Ugh.

if [ ! $PERLPATH = "/usr/bin/perl" -a $PERL == 1 ] ; then
	# if it's already in the normal place, fine; 
	# if perl isn't version 5, the modification won't work anyway.
	echo "Perl found at $perlpath, should be /usr/bin/perl"
	yesno "Should I modify changetrack to run perl from $perlpath?" YN yes
	if [ "$YN" = yes ]; then
		echo "Telling changetrack to run from $PERLPATH."
		perl -i -pe "s,/usr/bin/perl,$perlpath,g;" changetrack
	else
		echo "Installing changetrack unmodified."
		echo "You should probably link perl to /usr/bin/perl"
	fi
fi

########## Are my defaults OK for this machine?
ask "Application directory:" APPPATH $APPPATH_DEFAULT
ask "Manual page directory:" MANPATH $MANPATH_DEFAULT
ask "Configuration file directory:" ETCPATH $ETCPATH_DEFAULT
ask "History files directory:" HISTORYPATH $HISTORYPATH_DEFAULT

cat <<EOF

If you want changetrack to send email notifications, you need to configure
the email address where the mail comes *from*.  This is because some MTAs
(mail transfer agents, such as sendmail) are configured to block email
addresses that do not resolve to a valid domain name.  As a rule in this
case, you must not use 'localhost' for the domain.

If you *don't* want to use email notifications, then you can safely use
'changetrack@localhost' or whatever else you want; the address will not
be used.

EOF
ask "Originating email address: " MAILFROM $MAILFROM_DEFAULT

#
# Save our configuration
#
date=`date`
cat > $d/.config <<EOF
#
# Configuration file created by $0
# $date
#
APPPATH_DEFAULT="$APPPATH"
MANPATH_DEFAULT="$MANPATH"
ETCPATH_DEFAULT="$ETCPATH"
HISTORYPATH_DEFAULT="$HISTORYPATH"
MAILFROM_DEFAULT="$MAILFROM"
EOF

my_install changetrack     $APPPATH/changetrack   0755
my_install changetrack.man $MANPATH/changetrack.1 0644

if [ -f "$ETCPATH/changetrack.conf" ]; then
	echo "$ETCPATH/changetrack.conf exists; not installing default file."
else
	yesno "Should I put a default changetrack.conf file in $ETCPATH?" YN yes
	if [ "$YN" = yes ]; then
		my_install changetrack.conf $ETCPATH/changetrack.conf 0644
	else
		echo "Omitting default configuration file."
	fi
fi

more <<EOF

If you want to enable email capabilities, you must ensure that the perl
module Mail::SendMail is installed on your machine, and then modify the
changetrack script so that the lines
        #use Mail::Sendmail
        exit();
are changed to 
        use Mail::Sendmail
        # exit();
Also, you should change the value of the 'mailfrom' variable to be that
of a real email address within your domain, or some antispam filters may
block changetrack's mail.

These modifications can be found in the changetrack script at about line 350.

The Mail::Sendmail module can be installed by running the following
command as root:
        perl -MCPAN "install Mail::Sendmail"

EOF
