#!/bin/sh
#
# installations script
#

# global variables
SuSEbinpath=/usr/bin
localbinpath=/usr/local/bin
SourceDirectory=
DestDirectory=/usr/local/bin
HylafaxSpoolDir=/var/spool/hylafax
StandAloneInst=0
Update=0
Uninstall=0
CopyFiles=1
ImpConf=1
ChangeDevCapi=0
ConfigFileName=config.faxCAPI

#functions
function SearchForLib {
    if [ -e /lib/$1 ] || [ -e /usr/lib/$1 ] || [ -e /usr/local/lib/$1 ] ; then
        return 0
    fi
    echo "Can't find library $1!"
    return 1
}

function TestAndCopyFile {
    sourcefile=$2
    if [ -n "$5" ] && [ -e $5/$sourcefile ] ; then
        sourcefile=$5/$sourcefile
    elif [ -e $sourcefile ] ; then
        :
    elif [ -e src/$3/$sourcefile ] ; then
        sourcefile=src/$3/$sourcefile
    elif [ -e bin/$sourcefile ] ; then
        sourcefile=bin/$sourcefile
    elif [ -e $localbinpath/$sourcefile ] ; then
        sourcefile=$localbinpath/$sourcefile
    elif [ -e $SuSEbinpath/$sourcefile ] ; then
        sourcefile=$SuSEbinpath/$sourcefile
    else
        echo "Can't find $2."
        if [ $1 -ep 1 ] ; then
                exit 1
        fi
    fi
    if [ ! -d $4 ] ; then
        mkdir -p $4
        if [ $? -gt 0 ] ; then
            if [ $1 -eq 1 ] ; then
                exit 1
            else
                return $?
            fi
        fi
    fi
    echo "Copying $sourcefile to $4/$2."
    cp -f $sourcefile $4/$2
    if [ $? -gt 0 ] && [ $1 -eq 1 ] ; then
        exit 1
    fi
}

function SetConfigLine {
    if [ -z "$3" ] ; then
        grep -v "$2.*:.*/c2fax" $1 > $1.new
    else
        gawk 'BEGIN {repl = 0} END {if (repl == 0) {print SEARCHIT":"REPLACEIT}}
             {if ($1 != SEARCHIT) {print $0} else {repl = 1; print $1":"REPLACEIT}}
             ' FS=':' IGNORECASE=1 SEARCHIT="$2" REPLACEIT="$3" $1 > $1.new
    fi
    if [ "$4" != "no" ] ; then
        mv $1 $1.c2fax
    fi
    mv $1.new $1
}


# test command line
while [ $# -gt 0 ] ; do
    case $1
    in
    -s)
        StandAloneInst=1
        ImpConf=0
        ;;
    -p)
        if [ -n "$2" ] ; then
            HylafaxSpoolDir=$2
            shift
        fi
        ;;
    -b)
        if [ -n "$2" ] ; then
            SourceDirectory=$2
            shift
        fi
        ;;
    -d)
        if [ -n "$2" ] ; then
            DestDirectory=$2
            shift
        fi
        ;;
    -c)
        if [ -e /dev/capi20 ] ; then
            chmod 0666 /dev/capi20
            if [ $? -eq 0 ] ; then
                ChangeDevCapi=1
            fi
        else
            echo "Can't find /dev/capi20."
            exit 1
        fi
        ;;
    -U)
        Update=1
        ;;
    -u)
        Uninstall=1
        ;;
    -n)
        CopyFiles=0
        ;;
    *)
        echo "usage: `basename $0` [-n] [-s] [-c] [-u] [-p PATH] [-b PATH] [-d PATH]"
        echo -e "\t-n\tNot copying files only changing configs and inittab"
        echo -e "\t-s\tInstall stand alone, without checking for Hylafax"
        echo -e "\t-c\tChange mode of /dev/capi20 to 0666 during installation"
        echo -e "\t-U\tUpdate. Copy Files but don't change the config files"
        echo -e "\t-u\tUninstall. Remove files and changed lines from the config files"
        echo -e "\t-p\tSpecify path to Hylafax spool directory (default: /var/spool/fax)"
        echo -e "\t-b\tSpecify source path for the fax binaries"
        echo -e "\t-d\tSpecify destination path for the fax binaries"
        exit 1
        ;;
    esac
    shift
done


# Uninstallation
if [ $Uninstall -gt 0 ] ; then
    if [ $CopyFiles -gt 0 ] ; then
        rm $DestDirectory/c2faxsend
        rm $DestDirectory/c2faxrecv
    fi
    rm $HylafaxSpoolDir/etc/$ConfigFileName
    if [ $StandAloneInst -eq 0 ] && [ -e $HylafaxSpoolDir/etc/config ] ; then
        SetConfigLine $HylafaxSpoolDir/etc/config SendFaxCmd
        SetConfigLine /etc/inittab fr
    fi
    exit 0
fi


# search for c2faxsend
c2faxsendfile=c2faxsend
if [ -n "$SourceDirectory" ] && [ -e $SourceDirectory/$c2faxsendfile ] ; then
    c2faxsendfile=$SourceDirectory/$c2faxsendfile
elif [ -e $c2faxsendfile ] ; then
    :
elif [ -e src/faxsend/$c2faxsendfile ] ; then
    c2faxsendfile=src/faxsend/$c2faxsendfile
elif [ -e bin/$c2faxsendfile ] ; then
    c2faxsendfile=bin/$c2faxsendfile
elif [ -e $localbinpath/$c2faxsendfile ] ; then
    c2faxsendfile=$localbinpath/$c2faxsendfile
elif [ -e $SuSEbinpath/$c2faxsendfile ] ; then
    c2faxsendfile=$SuSEbinpath/$c2faxsendfile
else
    echo "Can't find $c2faxsendfile."
    exit 1
fi

# test the existing of the needed librarys
foundError=0
libsnotfound=`ldd $c2faxsendfile | grep "not found" | cut -f2 | cut -d " " -f1`
if [ -n "$libsnotfound" ] ; then
    for name in $libsnotfound
    do
        echo "Can't find library $name!"
    done
    foundError=1
fi

# search for hylafax config file
if [ $StandAloneInst -eq 0 ] && [ ! -e $HylafaxSpoolDir/etc/config ] ; then
    if [ -e /var/spool/hylafax/etc/config ] ; then
        HylafaxSpoolDir=/var/spool/hylafax
    elif [ -e /var/spool/fax/etc/config ] ; then
        HylafaxSpoolDir=/var/spool/fax
    else
        echo ""
        echo "Can't find Hylafax's config file: $HylafaxSpoolDir/etc/config."
        echo "Please adjust the Hylafax spool directory with -p [dir]"
        echo "or install CAPI4Hylafax stand alone with -s."
        foundError=1
    fi
fi

if [ $foundError -gt 0 ] ; then
    exit 2
fi

# copy files
if [ $CopyFiles -gt 0 ] ; then
    TestAndCopyFile 1 c2faxrecv faxrecv "$DestDirectory" "$SourceDirectory"
    TestAndCopyFile 1 c2faxsend faxsend "$DestDirectory" "$SourceDirectory"
    if [ $Update -eq 0 ] ; then
        if [ -e "/etc/$ConfigFileName" ] ; then
            echo "Backup /etc/$ConfigFileName to /etc/$ConfigFileName.c2fax"
            cp "/etc/$ConfigFileName" "/etc/$ConfigFileName.c2fax"
        fi
        TestAndCopyFile $ImpConf $ConfigFileName scripts "/etc" "$SourceDirectory"
        if [ $? -gt 0 ] ; then
            echo "Can't copy config file to /etc. This file isn't mandatory if you are working stand alone."
        else
            ConfigFileName="/etc/$ConfigFileName"
            SetConfigLine "$ConfigFileName" SpoolDir "\t\t$HylafaxSpoolDir" no
            SetConfigLine "$ConfigFileName" FaxRcvdCmd "\t\t$HylafaxSpoolDir/bin/faxrcvd" no
        fi
    else
        TestAndCopyFile $ImpConf "${ConfigFileName}.update" scripts "/etc" "$SourceDirectory"
    fi
else
    ConfigFileName="/etc/$ConfigFileName"
    if [ -e "$ConfigFileName" ] ; then
        SetConfigLine "$ConfigFileName" SpoolDir "\t\t$HylafaxSpoolDir" no
        SetConfigLine "$ConfigFileName" FaxRcvdCmd "\t\t$HylafaxSpoolDir/bin/faxrcvd" no
    else
        echo "Can't find config file \"$ConfigFileName\"! => Nothing to do."
        exit 1
    fi
fi

# adjust hylafax config file and inittab
if [ $StandAloneInst -eq 0 -a $Update -eq 0 ] ; then
    echo "Change $HylafaxSpoolDir/etc/config and /etc/inittab."
    SetConfigLine $HylafaxSpoolDir/etc/config SendFaxCmd "\t\t$DestDirectory/c2faxsend"
    SetConfigLine /etc/inittab fr "2345:once:$DestDirectory/c2faxrecv -q \"$HylafaxSpoolDir\""

    # patch faxrcvd
    if [ -e "$HylafaxSpoolDir/bin/faxrcvd" ] ; then
        cp "$HylafaxSpoolDir/bin/faxrcvd" "$HylafaxSpoolDir/bin/faxrcvd.c2fax"
        sed "/\[ *\$# *!= *[1-9] *\]/s/!=/-lt/" "$HylafaxSpoolDir/bin/faxrcvd.c2fax" > "$HylafaxSpoolDir/bin/faxrcvd"
        echo -e "\nfaxrcvd patched for the use of newer C4H or HylaFAX versions."
        echo "!- NOTE -!:  Please take a look in your faxrcvd script if it handles"
        echo "             the MSN (destination) as Parameter \"\$7\" correctly!"
    fi

    # change names of old Hylafax C4H config files
    for i in ${HylafaxSpoolDir}/etc/config.faxCAPI* ; do
        mv "$i" "$HylafaxSpoolDir/etc/old_`basename \"$i\"`"
    done
fi

# start dialog script to configure config.faxCAPI
. ./setupconffile "$HylafaxSpoolDir"

main_config_dialog "$ConfigFileName"

# that was it
echo -e "\nInstall was successfully!\n\n"
echo -e "!! Attention !! - The path of the config file has changed to /etc/config.faxCAPI !!\n\n"
if [ -n "`file \`which file\` | grep 64-bit`" -a -f "${DestDirectory}/c2faxrecv" -a \
     -n "`file \"${DestDirectory}/c2faxrecv\" | grep 32-bit`" ] ; then
    echo "The installed CAPI4Hylafax binaries are 32bit."
    echo -e "For 64bit binaries you have to compile the C4H sources.\n"
fi
if [ $StandAloneInst -eq 0 ] ; then
    echo "The Fax Receive Daemon c2faxrecv was added to /etc/inittab and will be started"
    echo -e "automatically. You could start it manually now to provide a restart.\n"
fi
if [ $ChangeDevCapi -eq 0 ] ; then
    echo "Don't forget to change mode of /dev/capi20 so that the binaries which are"
    echo -e "normally running from user \"fax\" have access rights to it.\n"
fi
echo ""
exit 0
