#!/bin/sh

######################################################################
# Copyright © 2013 - 2014 Tino Reichardt
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2, as
# published by the Free Software Foundation.
######################################################################
# ctime /TR 2013-08-22
# mtime /TR 2014-04-11 (licensing details added)
######################################################################

# first $home/bin
export PATH="$HOME/bin:/usr/bin:/bin"

# ORIGINAL_RECIPIENT is since Postfix 2.5
export RECIPIENT=$ORIGINAL_RECIPIENT

#DOMAIN=example.org
#HOME=/home/username
#LOCAL=username
#RECIPIENT=username@example.org
#ORIGINAL_RECIPIENT=lala-lala@example2.org
#PWD=/var/spool/postfix
#SENDER=sender@remote-example.org
#USER=username

# USER="username" -> okay
# SENDER -> okay
LOCAL=`echo $RECIPIENT|cut -d@ -f1`
DASH="-"
EXT=$LOCAL
DOMAIN=`echo $RECIPIENT|cut -d@ -f2`

# HOME="$HOME" -> each domain gets it's own Inbox ;)
HOME=""
test "$DOMAIN" = "domain1.example.org"  && HOME="/home/username/mail/domain1"
test "$DOMAIN" = "domain2.example.org"  && HOME="/home/username/mail/domain2"
test "$DOMAIN" = "domain3.example.org"  && HOME="/home/username/mail/domain3"
test "$DOMAIN" = "example.org"          && HOME="/home/username/mail/other"
test "$DOMAIN" = "lala.example.org"     && HOME="/home/username/mail/lala"
test "$DOMAIN" = "example.com"          && HOME="/home/username/mail/othercom"

if [ "x$HOME" = "x" ]; then
  HOME="/home/username/mail/default"
fi

# Delivered-To is correctly created by qmail-local

# you can remove headers like this, if you want:
#grep -v '^Delivered-To: username@example.org' | \
#grep -v '^X-Original-To:' | \

# usage: qmail-local [ -nN ] -- user homedir local dash ext domain sender aliasempty
seekablepipe qmail-local -- \
  "$USER" "$HOME" "$LOCAL" "$DASH" \
  "$EXT" "$DOMAIN" "$SENDER" ./
e=$?

# log it in the end... for debugging...
echo "qmail-local $e \"$USER\" \"$HOME\" \"$LOCAL\" \
  \"$DASH\" \"$EXT\" \"$DOMAIN\" \"$SENDER\"  \
  ./" >> $HOME/.maillog

(($e == 111)) && exit 75
(($e == 100)) && exit 77
exit $e

######################################################################
# exit codes of .forward:
# OK		0	/* successful termination */
# USAGE		64	/* command line usage error */
# DATAERR	65	/* data format error */
# NOINPUT	66	/* cannot open input */
# NOUSER	67	/* addressee unknown */
# NOHOST	68	/* host name unknown */
# UNAVAILABLE	69	/* service unavailable */
# SOFTWARE	70	/* internal software error */
# OSERR		71	/* system error (e.g., can't fork) */
# OSFILE	72	/* critical OS file missing */
# CANTCREAT	73	/* can't create (user) output file */
# IOERR		74	/* input/output error */
# TEMPFAIL	75	/* temp failure; user is invited to retry */
# PROTOCOL	76	/* remote error in protocol */
# NOPERM	77	/* permission denied */
# CONFIG	78	/* configuration error */
######################################################################
