#!/bin/sh
#
# mlmmj-make-ml.sh - henne@hennevogel.de
#
# modified for Debian GNU/Linux by boll@debian.org

VERSION="0.1 (Debian GNU/Linux)"
DEFAULTDIR="/var/spool/mlmmj"
ETCDEFAULTDIR="/etc/mlmmj/lists"
TEXTPATH="/usr/share/mlmmj/text.skel"

USAGE="
mlmmj-make-ml $VERSION
$0 [OPTIONS]
-h	display this help text
-L	the name of the mailinglist
-s	your spool directory if not $DEFAULTDIR
-z	nuffn for now
"

while getopts ":hL:s:az:" Option
do
case "$Option" in 
	h )
	echo -e "$USAGE"
	exit 0
	;;
	z )
	echo -n "nothing"
	exit 0
	;;
	L )
	LISTNAME="$OPTARG"
	;;
	s )
	SPOOLDIR="$OPTARG"
	;;
	* )
	echo -e "$0: invalid option\nTry $0 -h for more information."
	exit 1
	;;
esac
done
let SHIFTVAL=$OPTIND-1
shift $SHIFTVAL

if [ -z "$SPOOLDIR" ]; then
	SPOOLDIR="$DEFAULTDIR"
fi

echo "Creating Directorys below $SPOOLDIR. Use '-s spooldir' to change"

if [ -z "$LISTNAME" ]; then
	echo -n "What should the name of the Mailinglist be? [mlmmj-test] : "
	read LISTNAME
	if [ -z "$LISTNAME" ]; then
	LISTNAME="mlmmj-test"
	fi
fi

LISTDIR="$SPOOLDIR/$LISTNAME"
ETCLISTDIR="$ETCDEFAULTDIR/$LISTNAME"

mkdir -p $LISTDIR
mkdir -p $ETCLISTDIR

for DIR in incoming queue queue/discarded archive subconf unsubconf \
	   bounce moderation subscribers.d digesters.d requeue \
	   nomailsubs.d
do
	mkdir "$LISTDIR"/"$DIR"
done

for DIR in text control
do
	mkdir "$ETCLISTDIR"/"$DIR"
	ln -s "$ETCLISTDIR"/"$DIR" "$LISTDIR"/"$DIR"
done

chown -R nobody:nogroup $LISTDIR


test -f "$LISTDIR"/index || touch "$LISTDIR"/index

echo -n "The Domain for the List? [] : "
read FQDN
if [ -z "$FQDN" ]; then
	FQDN="localhost.localdomain"
fi

echo -n "The emailaddress of the list owner? [postmaster] : "
read OWNER
if [ -z "$OWNER" ]; then
	OWNER="postmaster"
fi
echo "$OWNER" > "$ETCLISTDIR"/"control/owner"

if [ -z "$TEXTPATH" -o ! -d "$TEXTPATH" ]; then
	echo "**NOTE** Could not copy the texts for the list"
	echo "Please manually copy the files from $TEXTPATH"
else
	cp "$TEXTPATH"/* "$ETCLISTDIR"/"text"
fi

LISTADDRESS="$LISTNAME@$FQDN"
echo "$LISTADDRESS" > "$ETCLISTDIR"/control/"listaddress"

MLMMJRECIEVE=`which mlmmj-recieve 2>/dev/null`
if [ -z "$MLMMJRECIEVE" ]; then
	MLMMJRECIEVE="/usr/bin/mlmmj-recieve"
fi

MLMMJMAINTD=`which mlmmj-maintd 2>/dev/null`
if [ -z "$MLMMJMAINTD" ]; then
	MLMMJMAINTD="/usr/bin/mlmmj-maintd"
fi

ALIAS="$LISTNAME:  \"|$MLMMJRECIEVE -L $SPOOLDIR/$LISTNAME/\""

echo "I am adding the following to your /etc/aliases file:"
echo "$ALIAS"
echo -n "is this ok (if not, you need to do so yourself)? [y/N] : "
read OKIDOKI
case $OKIDOKI in
	y|Y)
		echo "$ALIAS" >> /etc/aliases
		;;
	n|N)
		exit 0
		;;
	*)
		echo "Options were: y, Y, n or N"
esac

echo " ** FINAL NOTES **
Remember to run newaliases"
