#!/bin/sh
#
# Copyright (C) 2000-2002 Cameron J. Morland
#
# 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
#
# Author:             Cameron J. Morland and Devin Reade

# defaults
APPPATH_DEFAULT="/usr/bin"
MANPATH_DEFAULT="/usr/man/man1"
ROOTCONFFILE_DEFAULT="/etc/changetrack.conf"
ROOTHISTORYPATH_DEFAULT="/var/state/changetrack"
USERCONFFILE_DEFAULT="changetrack.conf"
USERHISTORYPATH_DEFAULT="changetrack.output"
MAILFROM_DEFAULT="changetrack@localhost"

PERLPATH=`which perl`

# enable or disable features
ESMAIL="# (install error) "
DSMAIL="# (install error) "
ENCOPY="# (install error) "
DNCOPY="# (install error) "

# 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

# include macros
.  $d/functions

# 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
	# use sed to get the first character of the response
	short=`echo $response | sed -e 's/^\(.\).*/\1/`
	#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
}

########## 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

########## Are my defaults OK for this machine?
ask "Application directory:" APPPATH $APPPATH_DEFAULT
ask "Manual page directory:" MANPATH $MANPATH_DEFAULT
ask "System-wide configuration file:" ROOTCONFFILE $ROOTCONFFILE_DEFAULT
ask "System-wide history files directory:" ROOTHISTORYPATH $ROOTHISTORYPATH_DEFAULT
ask "User configuration file:" USERCONFFILE $USERCONFFILE_DEFAULT
ask "User history files directory:" USERHISTORYPATH $USERHISTORYPATH_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"
ROOTCONFFILE_DEFAULT="$ROOTCONFFILE"
ROOTHISTORYPATH_DEFAULT="$ROOTHISTORYPATH"
USERCONFFILE_DEFAULT="$USERCONFFILE"
USERHISTORYPATH_DEFAULT="$USERHISTORYPATH"
MAILFROM_DEFAULT="$MAILFROM"
EOF

#
# Check to see if Mail::Sendmail is installed
#
GOTMAIL=`perl -e 'use Mail::Sendmail' 2>&1`

if [ "$GOTMAIL" == "" ]; then
    # it's installed. We comment out the one line.
    ESMAIL="# --sendmail enabled-- "
    DSMAIL=""
else
    # perl will have complained bitterly.
    more <<EOF
Mail::Sendmail appears to not be installed.
I will disable mail sending for now.

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

EOF
    # comment out the other line.
    ESMAIL=""
    DSMAIL="# --sendmail disabled-- "
fi

#
# Check to see if File::NCopy is installed
#

GOTNCOPY=`perl -e 'use File::NCopy' 2>&1`

if [ "$GOTNCOPY" == "" ]; then
    # it's installed. We comment out the one line.
    ENCOPY="# --ncopy enabled -- "
    DNCOPY=""
else
    # perl will have complained bitterly.
    more <<EOF
File::NCopy appears to not be installed.
I will use File::Copy for now, but this may disable features

The File::NCopy module can be installed by running the following
command as root:
        perl -MCPAN -e "install File::NCopy"

EOF
    # comment out the other line.
    ENCOPY=""
    DNCOPY="# --ncopy disabled-- "
fi

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

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

