#!/bin/bash

# $Id$

conffile=$1
if [ -z $1 ]
then
  conffile=/etc/email/email.conf
fi

conffvalue()
{
	:
}

if test -s $conffile
then
if awk --version > /dev/null
then
conffvalue()
{
	# TODO THIS NEEDS TO DEAL WITH 0 OR MORE SPACES AFTER THE '=' SIGN
	awk -F= "/^[ \t\f\n\r\v]*[^#]/ && \$1 ~ re {\
			split(\$2, a, \"'\"); \
			print(length(a[2]) > 0 ? a[2] : (\$2 == \" ''\" ? \"\" : \$2))\
		}" \
		re="$1" /etc/email/email.conf
#	awk -F= "/^[ \t\f\n\r\v]*[^#]/ && \$1 ~ re {print \$2; exit}" \
#	    re="$1" $conffile
}
else
echo "(You seem to be missing awk, so I can't read the values currently"
echo "stored in $conffile; you'll have to re-enter them manually if"
echo "you want to re-use them.  $conffile will not be overwritten"
echo "unless you confirm the overwrite at the end of the configuration.)"
echo
echo
fi
fi

SMTP_SERVER=$(conffvalue SMTP_SERVER)
if [ -z "$SMTP_SERVER" ]
then
	SMTP_SERVER_PREFIX="# "
else
	SMTP_SERVER_PREFIX=
fi
SMTP_SERVER=${SMTP_SERVER:-127.0.0.1}

SMTP_PORT=$(conffvalue SMTP_PORT)
if [ -z "$SMTP_PORT" ]
then
	SMTP_PORT_PREFIX="# "
else
	SMTP_PORT_PREFIX=
fi
SMTP_PORT=${SMTP_PORT:-25}

SENDMAIL_BIN=$(conffvalue SENDMAIL_BIN)
if [ -z "$SENDMAIL_BIN" ]
then
	SENDMAIL_BIN_PREFIX="# "
else
	SENDMAIL_BIN_PREFIX=
fi
SENDMAIL_BIN=${SENDMAIL_BIN:-/usr/sbin/sendmail -t -i}

MY_NAME=$(conffvalue MY_NAME)
if [ -z "$MY_NAME" ]
then
	MY_NAME_PREFIX="# "
else
	MY_NAME_PREFIX=
fi

MY_EMAIL=$(conffvalue MY_EMAIL)
if [ -z "$MY_EMAIL" ]
then
	MY_EMAIL_PREFIX="# "
else
	MY_EMAIL_PREFIX=
fi

REPLY_TO=$(conffvalue REPLY_TO)
if [ -z "$REPLY_TO" ]
then
	REPLY_TO_PREFIX="# "
else
	REPLY_TO_PREFIX=
fi

SIGNATURE_FILE=$(conffvalue SIGNATURE_FILE)
if [ -z "$SIGNATURE_FILE" ]
then
	SIGNATURE_FILE_PREFIX="# "
else
	SIGNATURE_FILE_PREFIX=
fi
SIGNATURE_FILE=${SIGNATURE_FILE:-&/email.sig}

SIGNATURE_DIVIDER=$(conffvalue SIGNATURE_DIVIDER)
if [ -z "$SIGNATURE_DIVIDER" ]
then
	SIGNATURE_DIVIDER_PREFIX="# "
else
	SIGNATURE_DIVIDER_PREFIX=
fi
SIGNATURE_DIVIDER=${SIGNATURE_DIVIDER:----}

ADDRESS_BOOK=$(conffvalue ADDRESS_BOOK)
if [ -z "$ADDRESS_BOOK" ]
then
	ADDRESS_BOOK_PREFIX="# "
else
	ADDRESS_BOOK_PREFIX=
fi
ADDRESS_BOOK=${ADDRESS_BOOK:-&/email.address.template}

SAVE_SENT_MAIL=$(conffvalue SAVE_SENT_MAIL)
if [ -z "$SAVE_SENT_MAIL" ]
then
	SAVE_SENT_MAIL_PREFIX="# "
else
	SAVE_SENT_MAIL_PREFIX=
fi
SAVE_SENT_MAIL=${SAVE_SENT_MAIL:-\~}

TEMP_DIR=$(conffvalue TEMP_DIR)
if [ -z "$TEMP_DIR" ]
then
	TEMP_DIR_PREFIX="# "
else
	TEMP_DIR_PREFIX=
fi
TEMP_DIR=${TEMP_DIR:-/tmp}

GPG_BIN=$(conffvalue GPG_BIN)
if [ -z "$GPG_BIN" ]
then
	GPG_BIN_PREFIX="# "
else
	GPG_BIN_PREFIX=
fi
GPG_BIN=${GPG_BIN:-/bin/gpg}

GPG_PASS=$(conffvalue GPG_PASS)
if [ -z "$GPG_PASS" ]
then
	GPG_PASS_PREFIX="# "
else
	GPG_PASS_PREFIX=
fi

SMTP_AUTH=$(conffvalue SMTP_AUTH)
if [ -z "$SMTP_AUTH" ]
then
	SMTP_AUTH_PREFIX="# "
else
	SMTP_AUTH_PREFIX=
fi
smtp_auth_response=1
if [ "$SMTP_AUTH" = "LOGIN" ]
then
	smtp_auth_response=2
fi
if [ "$SMTP_AUTH" = "PLAIN" ]
then
	smtp_auth_response=3
fi

SMTP_AUTH_USER=$(conffvalue SMTP_AUTH_USER)
if [ -z "$SMTP_AUTH_USER" ]
then
	SMTP_AUTH_USER_PREFIX="# "
else
	SMTP_AUTH_USER_PREFIX=
fi

SMTP_AUTH_PASS=$(conffvalue SMTP_AUTH_PASS)
if [ -z "$SMTP_AUTH_PASS" ]
then
	SMTP_AUTH_PASS_PREFIX="# "
else
	SMTP_AUTH_PASS_PREFIX=
fi

default="$MY_NAME"
echo -n "Please enter your "From:" name (e.g., John Doe) [$default]: "
read -e MY_NAME
if [ -z "$MY_NAME" -a -z "$default" ]
then
	MY_NAME_PREFIX="# "
else
	MY_NAME_PREFIX=
fi
MY_NAME=${MY_NAME:-$default}

default="$MY_EMAIL"
echo -n "Please enter your "From:" email address [$default]: "
read -e MY_EMAIL
if [ -z "$MY_EMAIL" -a -z "$default" ]
then
	MY_EMAIL_PREFIX="# "
else
	MY_EMAIL_PREFIX=
fi
MY_EMAIL=${MY_EMAIL:-$default}

default="$REPLY_TO"
echo -n "Please enter your "Reply-to:" email address [$default]: "
read -e REPLY_TO
if [ -z "$REPLY_TO" -a -z "$default" ]
then
	REPLY_TO_PREFIX="# "
else
	REPLY_TO_PREFIX=
fi
REPLY_TO=${REPLY_TO:-$default}

default=1
smtp_server=$(conffvalue SMTP_SERVER)
if [ -n "$smtp_server" ]
then
	default=2
fi

echo -n "Enter 1 to use sendmail or 2 to use a SMTP server [$default]: "
read -e smtp_mode
smtp_mode=${smtp_mode:-$default}

if [ "$smtp_mode" -eq "1" ]
then
	SMTP_SERVER_PREFIX="# "
	SMTP_PORT_PREFIX="# "
	SMTP_AUTH_PREFIX="# "
	SMTP_AUTH_USER_PREFIX="# "
	SMTP_PASS_PREFIX="# "

	default="$SENDMAIL_BIN"
	echo -n "Please enter the sendmail command line [$default]: "
	read -e SENDMAIL_BIN
	if [ -z "$SENDMAIL_BIN" -a -z "$default" ]
	then
		SENDMAIL_BIN_PREFIX="# "
	else
		SENDMAIL_BIN_PREFIX=
	fi
	SENDMAIL_BIN=${SENDMAIL_BIN:-$default}
fi

if [ "$smtp_mode" -eq "2" ]
then
	SENDMAIL_BIN_PREFIX="# "

	default="$SMTP_SERVER"
	echo -n "Please enter the address of your SMTP server [$default]: "
	read -e SMTP_SERVER
	if [ -z "$SMTP_SERVER" -a -z "$default" ]
	then
		SMTP_SERVER_PREFIX="# "
	else
		SMTP_SERVER_PREFIX=
	fi
	SMTP_SERVER=${SMTP_SERVER:-$default}

	default="$SMTP_PORT"
	echo -n "Please enter the SMTP port number [$default]: "
	read -e SMTP_PORT
	if [ -z "$SMTP_PORT" -a -z "$default" ]
	then
		SMTP_PORT_PREFIX="# "
	else
		SMTP_PORT_PREFIX=
	fi
	SMTP_PORT=${SMTP_PORT:-$default}

	default=$smtp_auth_response
	echo -n "Please select SMTP authentication (1 for none, 2 for LOGIN, 3 for PLAIN) [$default]: "
	read -e smtp_auth_reply
	smtp_auth_reply=${smtp_auth_reply:-$default}

	if [ "$smtp_auth_reply" -eq 1 ]
	then
		SMTP_AUTH=
		SMTP_AUTH_USER_PREFIX="# "
		SMTP_AUTH_PASS_PREFIX="# "
	fi
	if [ "$smtp_auth_reply" -eq 2 ]
	then
		SMTP_AUTH=LOGIN
	fi
	if [ "$smtp_auth_reply" -eq 3 ]
	then
		SMTP_AUTH=PLAIN
	fi

	if [ "$smtp_auth_reply" -ge 2 ]
	then
		default="$SMTP_AUTH_USER"
		echo -n "Please enter your SMTP username [$default]: "
		read -e SMTP_AUTH_USER
		if [ -z "$SMTP_AUTH_USER" -a -z "$default" ]
		then
			SMTP_AUTH_USER_PREFIX="# "
		else
			SMTP_AUTH_USER_PREFIX=
		fi
		SMTP_AUTH_USER=${SMTP_AUTH_USER:-$default}

		default=$(conffvalue SMTP_AUTH_PASS)
		echo -n "Please enter your SMTP password [$default]: "
		read -e SMTP_AUTH_PASS
		if [ -z "$SMTP_AUTH_PASS" -a -z "$default" ]
		then
			SMTP_AUTH_PASS_PREFIX="# "
		else
			SMTP_AUTH_PASS_PREFIX=
		fi
		SMTP_AUTH_PASS=${SMTP_AUTH_PASS:-$default}
	fi
fi

cat >$conffile <<EOF
# This is the DEFAULT configuration file for email. 
#
# Please CHANGE THE VALUES below to suit your environment.
# Don't forget to set the shell environment variable EDITOR
############################################################

############################################################
# SMTP Server and Port number you use
############################################################
${SMTP_SERVER_PREFIX}SMTP_SERVER = '$SMTP_SERVER'
${SMTP_PORT_PREFIX}SMTP_PORT = '$SMTP_PORT'

############################################################
# If you'd rather use sendmail binary, specify it and the
# command line switches to use, here.  If you have both
# this option and SMTP_SERVER set, SMTP_SERVER will be of
# higher priority than SENDMAIL.
############################################################
${SENDMAIL_BIN_PREFIX}SENDMAIL_BIN = '$SENDMAIL_BIN'

############################################################
# Your email address: If you'd like To have your name to
# show in the from field instead of just your email address,
# then keep the format below and edit it to your email
# and name.
############################################################
${MY_NAME_PREFIX}MY_NAME  = '$MY_NAME'
${MY_EMAIL_PREFIX}MY_EMAIL = '$MY_EMAIL'
${REPLY_TO_PREFIX}REPLY_TO = '$REPLY_TO'

############################################################
# Signature file and settings: Where is the signature file
# and How do you want the signature divider To be printed?
# You can comment these out if you do not like signatures
############################################################
${SIGNATURE_FILE_PREFIX}SIGNATURE_FILE    = '$SIGNATURE_FILE'
${SIGNATURE_DIVIDER_PREFIX}SIGNATURE_DIVIDER = '$SIGNATURE_DIVIDER'

############################################################
# This is where you store your address book... If you don't
# want address book functionality, just comment it out and
# use regular email addresses.
############################################################
${ADDRESS_BOOK_PREFIX}ADDRESS_BOOK = '$ADDRESS_BOOK'

############################################################
# If you would like to have a copy Of your final email saved
# after it is sent, please specify a directory to save
# it in below... If not, just comment this field out and it
# won't save a sent file other wise, it will save it as
# email.sent in the directory below
############################################################
${SAVE_SENT_MAIL_PREFIX}SAVE_SENT_MAIL = '$SAVE_SENT_MAIL'

############################################################
# With email, temporary files are stored with a random name
# Starting with .EM.  You must specify which directory you
# would like these temporary files stored while email is in
# Operation...  After email is done executing, it will remove
# Every temporary file it used.  The most common storage area
# for temporary files system wide is the /tmp directory
# if TEMP_DIR is not set, you can set the environment variable
# TMPDIR. If that is not set, email will just use /tmp
############################################################
${TEMP_DIR_PREFIX}TEMP_DIR = '$TEMP_DIR'

############################################################
# You should put the absolute path of your GPG binary
# In this variable.  If you do not know the absolute path
# just putting "gpg" will suffice if GPG is in your PATH
# environment variable path.
############################################################
${GPG_BIN_PREFIX}GPG_BIN = '$GPG_BIN'

###########################################################
# You can bypass email asking you for a password for gpg
# when you are encrypting or signing emails if you store
# it here.  Keep this safe! Make sure that people can't
# read your personal ~/.email.conf file if you're storing
# your password here!
###########################################################
${GPG_PASS_PREFIX}GPG_PASS = '$GPG_PASS'

###########################################################
# You can use SMTP_AUTH with email.  You just need to 
# specify which type of SMTP AUTH you will use. Email 
# support two types of SMTP AUTH: LOGIN and PLAIN.  
# Typically, LOGIN is the most used amongst SMTP servers.
# If you are unsure, ask your ISP.
###########################################################
${SMTP_AUTH_PREFIX}SMTP_AUTH = '$SMTP_AUTH'

###########################################################
# Setting your SMTP username is MANDITORY.  Please 
# uncomment the option below and set your username in order
# to use SMTP AUTH
###########################################################
${SMTP_AUTH_USER_PREFIX}SMTP_AUTH_USER = '$SMTP_AUTH_USER'

###########################################################
# Setting your password is not manditory.  If you do not
# set your password, then email will prompt you for one
# if it sees that you are trying to use SMTP_AUTH.
###########################################################
${SMTP_AUTH_PASS_PREFIX}SMTP_AUTH_PASS = '$SMTP_AUTH_PASS'
EOF

echo
echo
echo "Please check the configuration file $conffile for correctness."
echo
echo
