#!/bin/sh
# CML Configure
#       Copyright 1993-2004 (C) SAITO Yasuhiro.
#       All rights reserved.
#
###########################################################################
SD=/tmp/SD.$$
PF=/tmp/PF.$$
TM=/tmp/TM.$$
SH=/tmp/SH.$$
TB=/tmp/SH.$$

unset LANG  >/dev/null 2>&1

trap "rm -f $SD $PF $TM $SH $TB; exit 0" 0 1 2 3 15

COMMAND="basename cat chmod gzip cp date df echo ed egrep expr fgrep tr
         head ln ls mkdir mv rm sed sleep sort split tail tar telnet touch
         uuencode wc"

OLDPATH=$PATH
echo "" > $PF

c=
T="`echo 'test\c'`"
if [ "$T" = "test" ]
then
	c="\c"
fi

n=
T="`echo -n 'test'`"
if [ "$T" = "test" ]
then
	n="-n"
fi
AWK=awk
echo $n "Checking for gawk ... $c"
WHICH=`./cmdpath gawk`

set $WHICH
if [ "$1" != "no" ]
then
	CMD=`basename $WHICH`
	echo "Done."
	if [ "$CMD" = "gawk" ]
	then
		AWK=gawk
		echo "s/\/$CMD//" > $SD
		echo $WHICH | sed -f $SD >> $PF
	fi
fi

for i in $COMMAND
do
	echo $n "Checking for $i ... $c"
	WHICH=`./cmdpath $i`
	CMD=`basename $WHICH`
	echo "Done."
	if [ "$CMD" != "$i" ]
	then
		while :
		do
			echo    "       $i: command not found in PATH."
			echo $n "       Where is '$i' ? Enter command path.: $c "
			read P
			if [ -d $P ]
			then
				PATH=$PATH:$P
				export PATH
				echo $n "Checking for $i again ... $c"
				WHICH=`./cmdpath $i`
				CMD=`basename $WHICH`
				echo "Done."
				if [ "$CMD" = "$i" ]
				then
					break
				fi
			fi
		done
	fi
	echo "s/\/$CMD//" > $SD
	echo $WHICH | sed -f $SD >> $PF
done

for i in `sort $PF | grep \/ | uniq`
do
	CMDPATH=$CMDPATH$i:
done
PATH=$OLDPATH
export PATH

AWKPATH=`./cmdpath $AWK`
echo $n "Enter awk command path. [$AWKPATH] $c"
read ANS
if [ "$ANS" ]
then
	AWKPATH=$ANS
fi

GREPPATH=`./cmdpath grep`
echo $n "Enter grep command path. [$GREPPATH] $c"
read ANS
if [ "$ANS" ]
then
	GREPPATH=$ANS
fi

echo $n "Checking df command ... $c"

df | grep '%' >/dev/null 2>&1
if [ $? -eq 0 ]
then
	DFACT=1
else
	DFACT=0
fi

echo "Done."


DATE=0
JCMD=0
echo $n "Checking action of date command ... $c"

date| tr ' ' '\012' | egrep '^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]' 2>&1 >/dev/null
if [ $? -eq 0 ]
then
	JCMD=1
	DATE=2
fi

date '+%H:%M' 2>&1 | egrep '^[0-9][0-9]:[0-9][0-9]' 2>&1 >/dev/null
if [ $? -eq 0 ]
then
	DATE=1
fi

echo "Done."

SENDMAILWRAPPER=

echo $n "Checking for sendmail... $c"

for i in "/usr/lib/sendmail"    \
         "/usr/ucblib/sendmail" \
         "/usr/bin/sendmail"    \
         "/usr/sbin/sendmail"   \
	 "/var/qmail/bin/sendmail"
do
	if [ -f $i ]
	then
		$i -bs <<EOF >$TM 2>&1
		quit
EOF
		egrep "^220" $TM >/dev/null 2>&1
		if [ $? -eq 0 ]
		then
			SENDMAILWRAPPER=$i
			break
		fi
	fi
done

if [ -f $TM ]
then
	echo "Done."
else
	echo "not found."
fi

if [ "$SENDMAILWRAPPER" = "" ]
then
	while :
	do
		P=
		echo $n "    Where is 'sendmail' ? Enter command path.: $c "
		read P
		echo $n "Checking for sendmail again... $c"
		if [ -d $P ]
		then

			$P/sendmail -bs <<EOF >$TM 2>&1
			quit
EOF

			egrep "^220" $TM >/dev/null 2>&1
			if [ $? -eq 0 ]
			then
				SENDMAILWRAPPER=$P/sendmail
				echo "Done."
				break
			else
				echo "$P/sendmail is not sendmail."
			fi
		else
			echo "$P/sendmail not found."
		fi
	done
fi

SENDMAIL=$SENDMAILWRAPPER
SENDMAILOPTION1="-oi -t"
SENDMAILOPTION2="-oi"
SENDMAILOPTION3="-oem -odb"

SENDMAILTYPE=

echo $n "Checking for sendmail type... $c"
set `egrep "^220" $TM | tr -d '\015'`
SP=$4
if [ "$SP" = "" ]
then
	for q in "/var/qmail/bin/qmail-inject"  \
		 "/usr/sbin/qmail-inject"       \
		 "/usr/local/sbin/qmail-inject" \
		 "$P/qmail-inject"
	do
		 if [ -f $q ]
		 then
		    SP="qmail"
		    SENDMAIL=$q
		    SENDMAILOPTION1=
		    SENDMAILOPTION2=
		    SENDMAILOPTION3=
		    break
		 else
		    SP="unknown type"
		 fi
	done
fi

SENDMAILTYPE=$SP

echo "$SENDMAILTYPE."

echo $n "Checking domain name ... $c"

$SENDMAILWRAPPER -bs <<EOF >$TM 2>&1
quit
EOF

DOM=`grep 220 $TM | sed -e 's/220[ -]//' | awk '{ print $1 }'`
echo "Done."

echo $n "Enter domain name. [$DOM] $c"
read ANS
if [ "$ANS" ]
then
	DOM=$ANS
fi

CMAS=1
echo $n "Do you want to install cmas ? [y] $c"
read ANS
if [ "$ANS" -a "$ANS" != "y" -a "$ANS" != "Y" ]
then
        CMAS=0
fi

DIR=/usr/local/etc/cml
echo $n "Enter install directory. [$DIR] $c"
read ANS
if [ "$ANS" ]
then
	DIR=$ANS
fi

TMPDIR=/tmp
echo $n "Enter temp directory. [$TMPDIR] $c"
read ANS
if [ "$ANS" ]
then
	TMPDIR=$ANS
fi

SDIR=$DIR
echo $n "Enter spool directory. [$SDIR] $c"
read ANS
if [ "$ANS" ]
then
	SDIR=$ANS
fi

echo $SENDMAILTYPE | grep -i postfix >/dev/null 2>&1
if [ $? -eq 0 ]
then
	echo ""
	echo "	NOTICE"
	echo "	======"
	echo "	You are going to use POSTFIX."
	echo "	Please confirm 'allow_mail_to_commands = alias,forward,include'"
	echo "	is included main.cf ('include' is important)."
	echo ""
fi

echo $SENDMAILTYPE | grep -i qmail >/dev/null 2>&1
if [ $? -eq 0 ]
then
	echo ""
	echo "	NOTICE"
	echo "	======"
	echo "	You are going to use qmail."
	echo "	See details in the documents at contrib/Patch-kit-for-qmail/."
	echo ""
fi

cat <<EOF > $SH
#!/bin/sh
# Common ML Tools Install
# Copyright 1993-2004 (C) SAITO Yasuhiro.
# All rights reserved.
#
##############################################################
EOF

echo "SENDMAIL=$SENDMAIL"         >> $SH
echo "SENDMAILOPTION1=\"$SENDMAILOPTION1\"" >> $SH
echo "SENDMAILOPTION2=\"$SENDMAILOPTION2\"" >> $SH
echo "SENDMAILOPTION3=\"$SENDMAILOPTION3\"" >> $SH
echo "SENDMAILTYPE=$SENDMAILTYPE" >> $SH
echo "DOMAIN=$DOM"                >> $SH
echo "DRIVER=$DIR"                >> $SH
echo "MSPOOL=$SDIR"               >> $SH
echo "CMAS=$CMAS"                 >> $SH
echo "DATE=$DATE"                 >> $SH
echo "JCMD=$JCMD"                 >> $SH
echo "AWK=$AWKPATH"               >> $SH
echo "GREP=$GREPPATH"             >> $SH
echo "TMPDIR=$TMPDIR"             >> $SH
echo "DFACT=$DFACT"               >> $SH
echo "###############################################################" >> $SH

echo "PATH=$DIR:$CMDPATH" >> $SH

set `cat Version` "unknown"
echo "VERSION=$1"       >> $SH

cat $SH install.form.SH > Install.sh

echo "#!/bin/sh"    >  $TB
echo "AWK=$AWKPATH" >> $TB  

echo $n "Do you want to execute 'make install' ? [n] $c"
read ANS
if [ "$ANS" != "y" -a "$ANS" != "Y" ]
then
	echo "End Configure."
	echo "Execute 'make install' to install CML."
	exit 0
fi

/bin/sh Install.sh
exit 0
