#!/bin/sh -e

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

#
# Figure out the system's mailname
#

# Cygwin hack since Cygwin lacks --fqdn option
echo -n "Please enter the full qualified hostname (foo.bar.baz) of your box: "
read syshostname
#syshostname=`hostname --fqdn`
if test -f /etc/mailname
then
	mailname="`head -1 /etc/mailname`"
else
	echo "Please enter the mail name of your system."
	echo "This is the hostname portion of the address to be shown"
	echo "on outgoing news and mail messages."
	echo "The default is $syshostname, your system's host name."
	echo
	echo -n "Mail name [$syshostname]: "
	read mailname
	echo

	if test -z "$mailname"
	then
		mailname=$syshostname
	fi
fi

echo -n "Please enter the full qualified hostname of your mail hub: "
read mailhub

echo -n "Please enter the SMTP port number [25]: "
read smtpport
if test -z "$smtpport"
then
	mailhub=$mailhub
else
	mailhub="$mailhub:$smtpport"
fi

#
# Generate configuration file
#
if test -s "$conffile"
then
	echo Configuration file $conffile already exists.
	exit 0
fi

cat >>$conffile <<EOF
#
# /etc/ssmtp.conf -- a config file for sSMTP sendmail.
#
# The person who gets all mail for userids < 1000
root=postmaster
# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and you mailhub is so named.
mailhub=$mailhub
# Where will the mail seem to come from?
rewriteDomain=$mailname
# The full hostname
hostname=$syshostname
# Set this to never rewrite the "From:" line (unless not given) and to
# use that address in the "from line" of the envelope.
#FromLineOverride=YES
EOF

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