
if [ `echo $PREFIX | cut -c -1` = "@" ]; then
	SMDSERVER=~/Projects/syncmaildir/smd-server 
	SMDCLIENT=~/Projects/syncmaildir/smd-client 
else
	SMDSERVER=$PREFIX/bin/smd-server 
	SMDCLIENT=$PREFIX/bin/smd-client 
fi

H=$HOME
CONFDIR=$H/.smd
SSH=ssh
LOCKFILE=$CONFDIR/lock

REPNAME=
if [ ! -z "$1" ]; then
	REPNAME=$1
else
	REPNAME=default
fi
CONFFILE=$CONFDIR/config.$REPNAME

# backward compatibility code
if [ ! -f $CONFFILE ] && \
   [ "$REPNAME" = "default" ] && \
   [ -f $CONFDIR/config ]; then
	# we import the old conffile
	mkdir -p $CONFDIR/log
	mkdir -p $CONFDIR/fifo
	echo "From version 0.9.4, configuration files are named"
	echo "$CONFDIR/config.\$FOO, where FOO is an optional argument"
	echo "to smd-pull/smd-push. The default value of FOO is 'default'."
	echo "I'm renaming $CONFDIR/config to $CONFFILE."
	mv $CONFDIR/config $CONFFILE
fi

if [ ! -f $CONFFILE ]; then
	mkdir -p $CONFDIR/
	mkdir -p $CONFDIR/log
	mkdir -p $CONFDIR/fifo
	cat > $CONFFILE <<- EOT
	# No config file found, this is a template. You want to edit it.

	# Host name to be used with ssh as the server (use ~/.ssh/config 
	# for extra options). smd-pull will pull from this host, smd-push
	# will push to this host and use it as the id of the remote mailbox.
	#
	# We suggest creating an alias with your ~/.ssh/config like:
	# 
	#   Host smd-server-foo
	#     Compression yes
	#     Hostname your.real.server.name
	#     Username you
	#
	SERVERNAME=smd-server-$REPNAME


	# Host name to be used as the client. 
	# smd-pull will use this just as an id for the client. If you
	# plan to sync with multiple endpoints, you must use a different
	# client id for any of them, thus a pair localhostname-remotehostname
	# should be used
	#
	CLIENTNAME=`hostname`-$REPNAME

	# The mailbox to sync, the path is the same on both hosts, but
	# can be relative to the current working directory.
	MAILBOX="Mail/"

	# Log client to server and server to client communication.
	# This is useful only for debugging, since all network traffic
	# is dumped, including transmitted mails.
	DEBUG=false
	EOT
	echo No config file found: created a default one
	echo Please edit it: $CONFFILE
	exit 1
fi

if [ -f $LOCKFILE ]; then
	echo Already running.
	echo If this is not the case, remove $LOCKFILE by hand.
	exit 1
fi

touch $LOCKFILE

. $CONFFILE

CtL=$CONFDIR/fifo/c2l.$REPNAME
LtC=$CONFDIR/fifo/l2c.$REPNAME
LtS=$CONFDIR/fifo/l2s.$REPNAME
StL=$CONFDIR/fifo/s2l.$REPNAME
CtS=$CONFDIR/log/c2s.$REPNAME.log
StC=$CONFDIR/log/s2c.$REPNAME.log
CL=$CONFDIR/log/client.$REPNAME.log
SL=$CONFDIR/log/server.$REPNAME.log

[ -p $CtL ] || mkfifo $CtL 
[ -p $LtC ] || mkfifo $LtC
[ -p $LtS ] || mkfifo $LtS 
[ -p $StL ] || mkfifo $StL

mycat() { 
	cat 
}

MITM=mycat
VERBOSE=

if [ "$DEBUG" = "true" ]; then
	MITM=tee
	VERBOSE=-v
fi

cleanup() {
	rm -f $CONFDIR/lock
	kill $LOGGER1 2>/dev/null || true
	kill $LOGGER2 2>/dev/null || true
	kill $SERVER 2>/dev/null || true
	kill $CLIENT 2>/dev/null || true
}

trap cleanup "EXIT" 

