#!/bin/sh

#############################################################################
#
# HylaFAX Installation Script
#
# Initial version made by Carlos Martinez cmartinez@ghtek.com http://www.ghtek.com
# Updated by Jesper Knudsen at http://sme.swerts-knudsen.dk
#
# Revision History:
#
# Feb 24, 2004: 	Initial setup by Carlos Martinez 
# March 16, 2004:	Modified script to use much less modules and updated
#			to newer versions of GhostScript. Automated selection
#			of COM port.
# July 6, 2004:         Added support for download of RPMs from mirror site
#                       sme.swerts-knudsen.com if primary site is not
#                       responding
#############################################################################


cls()
{
for (( i=1 ; i<35 ; i++ )) 
do   
echo ""  
done
}

#--------------------------------------------------------------
# User Warning message 
#--------------------------------------------------------------
cls
echo "========================================================"
echo "= HylaFAX Installation Script                          ="
echo "=                                                      ="
echo "= This script will install HylaFAX on your system and  ="
echo "= help you to configure your modem. It has been tested ="
echo "= on SME 6.0 and is beta on 6.5, 5.5 and 5.6.          ="
echo "=                                                      ="
echo "= No express or implied warranties are provided and its="
echo "= usage is at your own risk.                           ="
echo "=                                                      ="
echo "= If you feel confortable with the above then press    ="
echo "= enter if not pres Ctrl+C to abort the installation   ="
echo "= script.                                              ="
echo "=                                                      ="
echo "========================================================"
read x
cls
echo "================================================================"
echo "= HylaFAX Installation Script (page 2)                         ="
echo "=                                                              ="
echo "= Before you continue:                                         ="
echo "=                                                              ="
echo "= 1)You should have your modem connected and powered on.       ="
echo "=                                                              ="
echo "= 2)You should know the phone number of your fax and           ="
echo "=   the name that will appear at the top of the fax page.      ="
echo "=                                                              ="
echo "= If you feel confortable with the above then press            ="
echo "= enter if not pres Ctrl+C to abort the installation script.   ="
echo "================================================================"
read x
cls

#--------------------------------------------------------------
# Make sure we know our working directory 
#--------------------------------------------------------------
cd /root
mkdir hfinstall
cd hfinstall   

PRIMARY=http://sme.swerts-knudsen.com
SECONDARY=http://sme.swerts-knudsen.dk

# determine download server....
mkdir downloadtest
cd downloadtest
wget -q --timeout=4 $PRIMARY/index.html
if [ -f index.html ]
then
# download from primary site OK
SITE=$PRIMARY
else
wget -q --timeout=4 $SECONDARY/index.html
if [ -f index.html ]
then
# download from mirror site
SITE=$SECONDARY
fi
fi
echo "Downloading RPMs and files from $SITE"
cd ..
rm -rf downloadtest

rpm -qa > rpmlist

IS_SME55=`pic rpmlist | grep -c SMEServer-5.5`
IS_SME56=`pic rpmlist | grep -c SMEServer-5.6`
IS_SME60=`pic rpmlist | grep -c SMEServer-6.0`
IS_SME65=`pic rpmlist | grep -c SMEServer-6.5`

if [ $IS_SME56 -ne 0 ]
then
echo "Installing HylaFAX on SME 5.6.....BETA INSTALLATION!!!"
else
if [ $IS_SME60 -eq 1 ]
then
echo "Installing HylaFAX on SME 6.0....."
else
if [ $IS_SME65 -ne 0 ]
then
echo "Installing HylaFAX on SME 6.5....."
else
if [ $IS_SME55 -ne 0 ]
then
echo "Installing HylaFAX on SME 5.5.....BETA INSTALLATION!!!"
echo "I do not do test for SME 5.5 but it should work BUT I have never tested it"
echo "If you dare anyways then press ENTER or CTRL-C to exit"
read x
else
echo "This installation only supports SME 5.5, 5.6, 6.0 and 6.5 - Exiting..."
pic rpmlist | grep SMEServer | mail install@swerts-knudsen.dk -s "HylaFAX Failed"
exit
fi
fi  
fi
fi


#--------------------------------------------------------------
# First we must install suporting modules (if not installed)
#--------------------------------------------------------------

if [ `pic rpmlist | grep ghostscript-7 | wc -l` -eq 0 ]
then
wget $SITE/downloads/Hylafax/Modules/ghostscript-7.05.6-0smea2.i386.rpm
fi

if [ `pic rpmlist | grep ghostscript-fonts-6 | wc -l` -eq 0 ]
then
wget $SITE/downloads/Hylafax/Modules/ghostscript-fonts-6.0-4rvda.noarch.rpm
fi

if [ `pic rpmlist | grep libtiff-3 | wc -l` -eq 0 ]
then
wget $SITE/downloads/Hylafax/Modules/libtiff-3.5.7-2.i386.rpm
fi

if [ `pic rpmlist | grep cups-libs-1 | wc -l` -eq 0 ]
then
wget $SITE/downloads/Hylafax/Modules/cups-libs-1.1.19-8smea1.i386.rpm
fi

if [ `pic rpmlist | grep metamail-2 | wc -l` -eq 0 ]
then
wget $SITE/downloads/Hylafax/Modules/metamail-2.7-28.i386.rpm
fi


rpm -Uvh  *.rpm
rm -f *.rpm

#--------------------------------------------------------------
# Then install Hylafax Main Module
#--------------------------------------------------------------
if [ `pic rpmlist | grep hylafax-4  | wc -l` -eq 0 ]
then
wget $SITE/downloads/Hylafax/Modules/hylafax-4.1.8-1rh7.i386.rpm 
rpm -Uvh  *.rpm
rm -f *.rpm
fi

cls
echo "================================================================"
echo "= Which COM port is your modem located on?                     ="
echo "= Type \"0\" for COM1 or \"1\" for COM2                            ="
echo "================================================================"
read COMPORT
if [ $COMPORT -eq 0 ]; then
echo "COM1 has been selected - press ENTER to continue"
else
if [ $COMPORT -eq 1 ]; then
echo "COM2 has been selected - press ENTER to continue"
else
echo "This script has been designed to work with COM1 and COM2 only"
echo "Use manual configuration for other settings."
echo "Installation terminated...."
exit
fi
fi   
read x

#--------------------------------------------------------------
# Setup the fax parameters
#--------------------------------------------------------------
cls
echo "============================================================"
echo "= HylaFAX has been succesfully installed                   ="
echo "=                                                          ="
echo "= - Now you must answer a couple questions so that         ="
echo "=   HylaFAX can work from your server.                     ="
echo "=                                                          ="
echo "= - If you have any problems or desire to run the          ="
echo "=   configuration process at a later time press Ctrl+C     ="
echo "=   When you are ready, run [faxsetup] to finish the       ="
echo "=   installation.                                          ="
echo "=                                                          ="
echo "= - If you dont know the answer to a question then just    ="
echo "=   hit enter.                                             ="
echo "=                                                          ="
echo "= - Your modem must be connected and powered ON at this    ="
echo "=   time. If it fails or is not connected, you can run     ="
echo "=   [faxaddmodem ttySx] at a later time.                   ="
echo "============================================================" 
read x

faxsetup

mkdir -p /etc/e-smith/templates-custom/etc/inittab
echo "fax:23457:respawn:/usr/sbin/faxgetty ttyS$COMPORT" > /etc/e-smith/templates-custom/etc/inittab/S85hylafax
/sbin/e-smith/expand-template /etc/inittab

# Make sure HylaFAX is restarted at boot
ln -s /etc/rc.d/init.d/hylafax /etc/rc.d/rc7.d/S92hylafax

# Restart HylaFAX
/sbin/init q
/etc/rc.d/init.d/hylafax restart

# Update a few support scripts.
mv /usr/sbin/xferfaxstats /usr/sbin/xferfaxstats.org
wget $SITE/downloads/Hylafax/Modules/xferfaxstats
install -m 0755 xferfaxstats /usr/sbin/xferfaxstats

mv /var/spool/hylafax/bin/faxrcvd /var/spool/hylafax/bin/faxrcvd.org
wget $SITE/downloads/Hylafax/Modules/faxrcvd
install -m 0755 faxrcvd /var/spool/hylafax/bin/faxrcvd

touch /var/spool/hylafax/etc/xferfaxlog
chmod 600 /var/spool/hylafax/etc/xferfaxlog
chown uucp:uucp /var/spool/hylafax/etc/xferfaxlog

# Restart HylaFAX once again
/etc/rc.d/init.d/hylafax restart

#--------------------------------------------------------------
# House Cleaning
#--------------------------------------------------------------
cd /root
rm -r hfinstall

#--------------------------------------------------------------
# Let me know of another success!
#--------------------------------------------------------------
ERR=`wget -q -t 1 -T 2 --delete-after http://sme.swerts-knudsen.dk/cgi-bin/egometer?HylaFAX_Script`

echo "HylaFAX succesfully installed"

