#!/bin/bash

# renattach configuration script
# Copyright(C) 2001, Jem E. Berkes
# http://www.pc-tools.net/

# By default we look for sendmail (MTA) and procmail (local mailer)
# To specify an exact location, override with --sendmail and --procmail

OUR_MTA=sendmail
OUR_LM=procmail

# More defaults, where things will be installed
prefix=
bindir=/usr/sbin
mandir=/usr/man/man1
sysconfdir=/etc

####

echo
echo "Configuring renattach for your system"
echo

cmd_prev=
for cmd_option
do
        # If the previous option needs an argument, assign it.
        if test -n "$cmd_prev"; then
                eval "$cmd_prev=\$cmd_option"
                cmd_prev=
                continue
        fi


        case "$cmd_option" in

	-prefix | --prefix)
		cmd_prev=prefix ;;
	-prefix=* | --prefix=*)
		prefix=`echo $cmd_option | cut -d = -f 2` ;;

        -bindir | --bindir)
                cmd_prev=bindir ;;
        -bindir=* | --bindir=*)
                bindir=`echo $cmd_option | cut -d = -f 2` ;;

        -mandir | --mandir)
                cmd_prev=mandir ;;
        -mandir=* | --mandir=*)
                mandir=`echo $cmd_option | cut -d = -f 2` ;;

	-procmail | --procmail)
		cmd_prev=LOCAL_MAILER ;;
	-procmail=* | --procmail=*)
		LOCAL_MAILER=`echo $cmd_option | cut -d = -f 2` ;;

	-sendmail | --sendmail)
		cmd_prev=MTA_COMMAND ;;
	-sendmail=* | --sendmail=*)
		MTA_COMMAND=`echo $cmd_option | cut -d = -f 2` ;;
		
        -sysconfdir | --sysconfdir)
                cmd_prev=sysconfdir ;;
        -sysconfdir=* | --sysconfdir=*)
                sysconfdir=`echo $cmd_option | cut -d = -f 2` ;;

        -*)
                {
cat << EOF
The following switches can be used to override the defaults:

--prefix=path
	Install everything under different root. e.g,
	--prefix=\$HOME

--bindir=path
	Set directory where binaries will be installed
                
--mandir=path
	Set directory where man page will be installed
                
--sysconfdir=path
	Set directory where renattach.conf will be installed
                
--sendmail=path/filename
	Explicitly specify sendmail-like MTA (e.g. /usr/sbin/sendmail)
                
--procmail=path/filename
	Explicitly specify procmail-like mailer (e.g. /usr/bin/procmail)

EOF
                exit 1
                }
        esac
done

echo "Binaries will be installed in"
echo " $prefix$bindir"
echo "Man pages will be installed in"
echo " $prefix$mandir"
echo "renattach.conf will be installed in"
echo " $prefix$sysconfdir"
echo

if test -n "$MTA_COMMAND" ; then
	echo "MTA (sendmail or equivalent) is"
	echo " $MTA_COMMAND"
else
	echo "Checking for MTA ($OUR_MTA)"
	for mta_dir in \
		/usr/bin/ \
		/usr/sbin/ \
		/usr/lib/ \
		/usr/local/bin/ \
		/usr/local/sbin/ \
		/usr/local/lib/ \
		; \
	do
		if test -x "$mta_dir$OUR_MTA" ; then
			MTA_COMMAND=$mta_dir$OUR_MTA
			break;
		fi
	done

	if test -x "$MTA_COMMAND" ; then
		echo " $MTA_COMMAND"
	else
		echo " Error: unable to find $OUR_MTA"
		exit 1
	fi
fi

if test -n "$LOCAL_MAILER" ; then
	echo "Local mailer (procmail or equivalent) is"
	echo " $LOCAL_MAILER"
else
	echo
	echo "Checking for local mailer ($OUR_LM)"
	for lm_dir in \
	        /usr/bin/ \
	        /usr/sbin/ \
	        /usr/local/bin/ \
	        /usr/local/sbin/ \
	        ; \
	do
	        if test -x "$lm_dir$OUR_LM" ; then
	                LOCAL_MAILER=$lm_dir$OUR_LM
	                break;
	        fi
	done
	
	if test -x "$LOCAL_MAILER" ; then
	        echo " $LOCAL_MAILER"
	else
	        echo " Error: unable to find $OUR_LM"
	        exit 1
	fi
fi

grep -v "//" defs.base > defs.h
echo -e "#define MTA_COMMAND\t\"$MTA_COMMAND\"" >> defs.h
echo -e "#define LOCAL_MAILER\t\"$LOCAL_MAILER\"" >> defs.h
echo -e "#define CONF_FILE\t\"$prefix$sysconfdir/renattach.conf\"" >> defs.h
echo >> defs.h

echo "BINDIR=$prefix$bindir" > Makefile
echo "MANDIR=$prefix$mandir" >> Makefile
echo "CONFDIR=$prefix$sysconfdir" >> Makefile
echo "CC=gcc" >> Makefile
echo -e "CFLAGS=-Wall -O2\n" >> Makefile
echo -e "renattach : renattach.o settings.o" >> Makefile
echo -e "\t\$(CC) \$(CFLAGS) -o renattach renattach.o settings.o\n" >> Makefile
echo "clean:" >> Makefile
echo -e "\trm -f renattach" >> Makefile
echo -e "\trm -f *.o\n" >> Makefile
echo "install: renattach" >> Makefile

echo -e "\t@if [ ! -r \$(BINDIR) ] ; then mkdir -p \$(BINDIR) ; fi" >> Makefile
echo -e "\t@if [ ! -r \$(CONFDIR) ] ; then mkdir -p \$(CONFDIR) ; fi" >> Makefile
echo -e "\t@if [ ! -r \$(MANDIR) ] ; then mkdir -p \$(MANDIR) ; fi" >> Makefile
echo -e "\t./install.sh renattach \$(BINDIR) -s -m 755" >> Makefile
echo -e "\t./install.sh renattach.1.gz \$(MANDIR) -m 644" >> Makefile
echo -e "\t@if [ -r \$(CONFDIR)/renattach.conf ] ; \\" >> Makefile
echo -e "\t\tthen echo \"Not replacing existing .conf file\" ; \\" >> Makefile
echo -e "\t\telse ./install.sh renattach.conf \$(CONFDIR) -m 644 ; \\" >> Makefile
echo -e "\t\techo \"./install.sh renattach.conf \$(CONFDIR) -m 644\" ; fi\n" >> Makefile
echo "uninstall:" >> Makefile
echo -e "\trm -f \$(BINDIR)/renattach" >> Makefile
echo -e "\trm -f \$(MANDIR)/renattach.1.gz" >> Makefile
echo -e "\trm -i \$(CONFDIR)/renattach.conf\n" >> Makefile

echo
echo
echo "defs.h and Makefile have been created"
echo "Edit defs.h to tweak features, if you wish"
echo
echo "Now ready for 'make'"
echo
