#! /bin/sh 
# 
# Ugh!  Yet another borne shell install script.   I really do dislike working
# in borne shell and no doubt it will show.
#
# Get information to add a new modem
# Then use the information to add the modem to the modem database,
# create the new spool directory and log/acct files,
# chown the device associated with the fax name, etc.
#
#
if [ "$XPRESSFAX_HOME" = "" ]; then
	XPRESSFAX_HOME=`dirname $0`/..
	if [ "$XPRESSFAX_HOME" = "./.." ]; then
		XPRESSFAX_HOME=`pwd`/..
	fi
fi
if [ "$XPRESSFAX_FAXRC" = "" ]; then
	XPRESSFAX_FAXRC=$XPRESSFAX_HOME/.faxrc
fi

# 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
if [ `whoami` != "root" ]; then
	echo "Sorry, only root may run this script."
	exit 1
fi


installation_message(){
echo "
********************************************************************************
********************************************************************************
**                                                                            **
**                         XpressFax Version  1.0.5                           **
**                       FaxModem Installation Script                         **
**                                                                            **
********************************************************************************
********************************************************************************

This script will enable you to add a new faxmodem to the XpressFax Package by
executing the following functions:

	* Creation of fax spool directory and log files for new faxmodems
	* Addition to 'faxdevices' file of a new faxmodem entry 
	* Change of Ownership of device in /dev to xpressfax 

"
}

clear
installation_message
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                           **
**                       FaxModem Installation Script                         **
**                                                                            **
********************************************************************************
********************************************************************************

"
echo -n "Enter the modem name to use for the new faxmodem "
read MODEM_NAME
echo ""
echo  "Enter the directory name to use for the modem (eg: Any, Dir2, etc)"
read DIR_NAME
echo ""
echo "Enter the area code of the phone line which plugs into this faxmodem"
echo -n "(eg: 818, 415, 408, etc.): "
read AREA
echo ""
echo "Enter the phone number of the phone line which plugs into this faxmodem"
echo -n "(eg: 818 584 1048): "
read PHONE
echo ""
DONE=notok
until [ $DONE = "OK" ];
do
	echo "Enter the full path to the device name in /dev to which this faxmodem is"
	echo -n "attached (eg: /dev/cu0a, /dev/tty80, etc): "
	read DEVICE
	if [ -f $DEVICE ]; then
		DONE="OK"
	fi
done
clear
echo "
********************************************************************************
********************************************************************************
**                                                                            **
**                         XpressFax Version  1.0.5                           **
**                       FaxModem Installation Script                         **
**                                                                            **
********************************************************************************
********************************************************************************

You have asked to add a modem named \""$MODEM_NAME"\" to the XpressFax database of
modems. \""$MODEM_NAME"\" is connected to physical device \""$DEVICE"\" and will spool
into the subdirectory \""$DIR_NAME"\" of the defined XpressFax Spool Directory.  
This modem device is connected to a phone line with the number \""$PHONE"\" 
which is in area code \""$AREA"\"."


if ask  "Use this information to create the device? " "y" "n"; then
	echo
else
	echo "Aborting..."
	exit 1
fi

VALUE=`ls -lt $DEVICE | awk -F, '{print $2}' | awk '{print $1}'`
if [ $VALUE -lt 128 ]; then
	echo "
Sorry, $DEVICE must have minor device number greater than 128 in order to
guarantee nonblocking opens.   You must select device like /dev/tty80 for
multiport extender boxes, or make devices cua and cub for the onboard 
SPARCstation ports.  These latter two may be created by typing:

	mknod /dev/cu0a c 12 128
	mknod /dev/cu0b c 12 129
	
"
	exit 1
fi

# 
# Chown the device entry 
#
FAX_LOGIN=`ls -lt $XPRESSFAX_HOME/sbin/xpressfax_modem | awk -F" " '{print $3}'`
if [ "$FAX_LOGIN" = "" ]; then
	echo ""
else
	chown $FAX_LOGIN $DEVICE; chmod 700 $DEVICE
fi

#
# Turn off software carrier on the device
#
/usr/etc/ttysoftcar -n $DEVICE


TOPD=`grep FAXServerSpool $XPRESSFAX_FAXRC | awk -F: '{print $2}'`

#
# Get full path name to spool directory, make it and logfiles, chown it
# to xpressfax and make it owner-readable only
#
SPOOL=$TOPD/$DIR_NAME
mkdir $SPOOL
touch $SPOOL/logfile
touch $SPOOL/acctfile
if [ "$FAX_LOGIN" = "" ]; then
	echo  ""
else
	chown -R $FAX_LOGIN $SPOOL; chmod 700 $SPOOL
fi

#
#
DONE=notok
until [ $DONE = "OK" ];
do
	if [ -f $XPRESSFAX_HOME/data/faxdevices.lock ]; then
		echo "waiting for free lock on $XPRESSFAX_HOME/data/faxdevices"
		sleep 1
	else
		touch $XPRESSFAX_HOME/data/faxdevices.lock
		echo "$MODEM_NAME	$DIR_NAME	Terminated	0	$PHONE	Date String	$DEVICE	1	1	$AREA		" >> $XPRESSFAX_HOME/data/faxdevices
		/bin/rm $XPRESSFAX_HOME/data/faxdevices.lock
		DONE="OK"
	fi
done

echo " Done updating $XPRESSFAX_HOME/data/faxdevices."
echo " "
if ask "Would you like to start an 'xpressfax_modem' daemon for $MODEM_NAME now?" "y" "n"; then
	$XPRESSFAX_HOME/sbin/xpressfax_modem $MODEM_NAME &
fi
exit 0
