#!/bin/sh
#
# install_tar.sh
#
# This shell script may be used to install XpressFax from the 
# ftp-able tar file copy of Version 1.0.5+
#
#

FROM_DIR=`pwd`
#
# Routine to ask a question with default passed and check for typos
# $?=1 for default response
# $1=question, $2=default, $3=opposite of $2
ask(){
        answer=undefined
        echo -n "$1 [$2]: "
        read answer
        until [ "$answer" = "" -o "$answer" = "$2" -o "$answer" = "$3" ];
        do
           echo "Please type '$2' or '$3'."
           echo -n "$1 [$2]: "
           read answer
        done
        [ "$answer" = "$2" -o "$answer" = "" ]
}

clear
echo "
********************************************************************************
********************************************************************************
**                                                                            **
**                         XpressFax Version  1.0.5+                          **
**                     FTP Tar File Installation Script                       **
**                                                                            **
********************************************************************************
********************************************************************************
"

if [ `whoami` != "root" ]; then
	echo "Alas, you must be root to run this script.   Please 'su' and try again."
	exit 1
fi

echo "This script is designed to install XpressFax 1.0.5+ from the network ftp tar "
echo "file \"xpressfax-1.0.5+.tar.Z\". "
echo " "
if ask "Would you like to continue with this installation process?" "y" "n"; then
	echo 0
else
	exit 0
fi

clear
echo "
********************************************************************************
********************************************************************************
**                                                                            **
**                         XpressFax Version  1.0.5+                          **
**                     FTP Tar File Installation Script                       **
**                                                                            **
********************************************************************************
********************************************************************************

"
INSTALL_OK=undefined
DEFAULT_DIR=/usr/xpressfax
until [ "$INSTALL_OK" = "OK" ]; 
do
	echo " "
	echo "Where would you like the software installed (/usr/xpressfax)? "
	read INSTALL_DIR
	if [ "$INSTALL_DIR" = "" ]; then
		INSTALL_DIR=/usr/xpressfax
		if [ -d $INSTALL_DIR ]; then
			INSTALL_OK=OK
		else
			mkdir $INSTALL_DIR
			INSTALL_OK=OK
		fi
	else
		if [ -d $INSTALL_DIR ]; then
			INSTALL_OK=OK
		else
			echo -n "$INSTALL_DIR doesn't exist.  Create it? "
			read ANSWER
			if [ "$ANSWER" = "y" -o "$ANSWER" = "" ]; then
				mkdir $INSTALL_DIR
				if [ -d $INSTALL_DIR ]; then
					INSTALL_OK=OK
				else
					echo "mkdir $INSTALL_DIR failed?"
				fi
			fi
		fi
	fi
done

#
# Export this for later install scripts to use
#
XPRESSFAX_HOME=$INSTALL_DIR
export XPRESSFAX_HOME LD_LIBRARY_PATH
cd $INSTALL_DIR

clear
echo "
********************************************************************************
********************************************************************************
**                                                                            **
**                         XpressFax Version  1.0.5+                          **
**                     FTP Tar File Installation Script                       **
**                                                                            **
********************************************************************************
********************************************************************************

"

USER_OK=not
until [ "$USER_OK" = "OK" ]; 
do
	echo -n "Enter the login name to use for the XpressFax software: "
	read USER
	if [ "$USER" = "" ]; then
		echo ""
	else
		T=`/usr/ucb/finger $USER | wc -l `
		if [ "$T" = "" -o $T -lt 2 ]; then
			USER_OK=false
		else
			USER_OK=OK
		fi
	fi 
done

HOMEDIR=`/bin/csh -c "cd ~$USER; pwd"`


clear
echo "
********************************************************************************
********************************************************************************
**                                                                            **
**                         XpressFax Version  1.0.5+                          **
**                     FTP Tar File Installation Script                       **
**                                                                            **
********************************************************************************
********************************************************************************

"
echo "XpressFax 1.0.5+ will be installed in $INSTALL_DIR "
echo "The fax administrator will be $USER"
echo  " "
if ask "Proceed with installation or abort?" "y" "n"; then
	echo ""
else
	exit 0
fi

clear
echo "
********************************************************************************
********************************************************************************
**                                                                            **
**                         XpressFax Version  1.0.5+                          **
**                     FTP Tar File Installation Script                       **
**                                                                            **
********************************************************************************
********************************************************************************

     EXTRACTING XPRESSFAX 1.0.5+ FROM TAR FILE WITH TAR VERIFICATION ACTIVE

"
cd $INSTALL_DIR
/bin/cat $FROM_DIR/xpressfax-1.0.5+.tar.Z | /usr/ucb/zcat - | tar xfv -
sleep 1

echo " "
echo "    RAW TAR EXTRACTION COMPLETE.   UPDATING AND CHOWING XPRESSFAX FILES"
echo ""
#
#
# Fix ownership
#
#
echo " "
echo "Changing ownership on $INSTALL_DIR to $USER"
echo " "
#
# chown toplevel and then cd into it to make sure we handle symlinks
#
/usr/etc/chown $USER $INSTALL_DIR
cd $INSTALL_DIR
/usr/etc/chown -R $USER .

/usr/etc/chown root $INSTALL_DIR/bin/faxlicense
/usr/etc/chown root $INSTALL_DIR/sbin/xpressfax_faxmaild
if [ "$INSTALL_DIR" = "/usr/xpressfax" ]; then
	echo ""
else
	/bin/cp $INSTALL_DIR/.faxrc  $INSTALL_DIR/.faxrc-default
	/bin/rm -rf /tmp/.faxrc$$
	/bin/sed -e 1,\$s/usr/xpressfax$INSTALL_DIRg $INSTALL_DIR/.faxrc > /tmp/.faxrc$$
	/bin/cp /tmp/.faxrc$$ $INSTALL_DIR/.faxrc
	/bin/rm -rf /tmp/.faxrc$$
fi

# 
# Make a copy of the administator .faxrc file in ~$USER
#
if [ "$INSTALL_DIR" = "$HOMEDIR" ]; then
	echo ""
else
	echo ""
	echo "Making a copy of the fax administrator .faxrc file for $USER "
	echo ""
	/bin/cp $INSTALL_DIR/.faxrc $HOMEDIR/.faxrc
fi


#
# Final mesasge
#
echo " "
echo "Physical extraction from tar file  to $INSTALL_DIR with "
echo "fax administrator defined as $USER now complete.   "
echo " "
echo -n "Would you like to continue with installation process? "
read CONT
if [ "$CONT" = "" -o "$CONT" = "y" ]; then
	exec $INSTALL_DIR/install/install_xpressfax.sh
else
	echo " "
	echo "To continue with installation at a later date run the program "
	echo "$INSTALL_DIR/install/install_xpressfax.sh "
	echo " "
fi
