#!/bin/sh

FAXBASE="`echo $1 | tr '/' ''`"
FAXPATH="`echo $2 | tr '/' ''`"
CONVERT="`echo $3 | tr '/' ''`"
EDITOR="`echo $4 | tr '/' ''`"
SPOOLDIR=`grep "SPOOLDIR=" local.cfg | tr '/' '' | sed -e "/SPOOLDIR=/s///"`
SPLDIR=`echo $SPOOLDIR | tr '' '/'`

echo -e "\n>> Customizing system wide xfaxrc with\nFAXBASE=$1 SPOOLDIR=$SPLDIR EDITOR=$4"
cat proto/xfaxrc | sed -e "/FAXBASE/s//$FAXBASE/g" | sed -e "/SPOOLDIR/s//$SPOOLDIR/g" | sed -e "/EDITOR/s//$EDITOR/g" | tr '' '/' > lib/xfaxrc

echo -e "\n>> Customizing generic.tex with\nFAXBASE=$1"
cat proto/covers/generic.tex | sed -e "/FAXBASE/s//$FAXBASE/g" | sed -e "/SPOOLDIR/s//$SPOOLDIR/g" | tr '' '/' > lib/covers/generic.tex

echo -e "\n>> Customizing g3view with\nPATH=$2"
cat proto/bin/g3view | sed -e "/PATH=/s//PATH=\"$FAXPATH\"/g" \
                     | tr '' '/' > lib/bin/g3view

echo -e "\n>> Customizing g3toxpm with\nPATH=$2 CONVERT=$3"
cat proto/bin/g3toxpm | sed -e "/PATH=/s//PATH=\"$FAXPATH\"/g" | tr '' '/' \
            | sed -e "/CONVERT=/s//CONVERT=\"$CONVERT\"/g" > lib/bin/g3toxpm

echo -e "\n>> Customizing anytog3 with\nPATH=$2"
cat proto/bin/anytog3 | sed -e "/PATH=/s//PATH=\"$FAXPATH\"/g" | tr '' '/' \
                      > lib/bin/anytog3


echo -e "\n>> Customizing faxprint with\nPATH=$2"
cat proto/bin/faxprint | sed -e "/PATH=/s//PATH=\"$FAXPATH\"/g" | tr '' '/' \
                      > lib/bin/faxprint

echo -e "\n>> Customizing faxqueue with\nSPOOLDIR=$SPLDIR"
cat proto/bin/faxqueue | sed -e "/SPOOLDIR=/s//SPOOLDIR=\"$SPOOLDIR\"/g" \
            | tr '' '/' > lib/bin/faxqueue

echo -e "\n>> Assembling faxdirect"
echo -e "\
#!/bin/sh
#
# Modified faxdirect script for use for Xfax
#\n" > lib/bin/faxdirect
cat local.cfg proto/bin/faxdirect >> lib/bin/faxdirect

echo -e "\n>> Assembling faxserver"
echo -e "\
#!/bin/sh
#
# Modified faxserver script for use for Xfax
#\n" > lib/bin/faxserver
cat local.cfg proto/bin/faxserver >> lib/bin/faxserver

chmod a+rx lib/bin/*

echo -e "\nXfax distribution successfully built.
Type now \"make install\", as root.\n"
