#!/bin/bash

DEF_FORWARDING="yourmail@example.com"
DEF_MAILSERVER="example.com"
DEF_MYSQL_USER="youruser"
DEF_MYSQL_PASS="--password=yourpassword"

if [ "$FORWARDING" = "" ]; then
    FORWARDING=$DEF_FORWARDING
fi

if [ "$MAILSERVER" = "" ]; then
    MAILSERVER=$DEF_MAILSERVER
fi

if [ "$MYSQL_USER" = "" ]; then
    MYSQL_USER=$DEF_MYSQL_USER
fi

if [ "$MYSQL_PASS" = "ask" ]; then
    MYSQL_PASS="-p"
fi

if [ "$MYSQL_PASS" = "" ]; then
    MYSQL_PASS=$DEF_MYSQL_PASS
fi

case $1 in
    l|list)
	case $2 in
	    u|users|user)
		ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"select * from users order by email\" 2>&1" 2>/dev/null
		;;
	    d|domains|domain)
		ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"select * from domains order by domain\" 2>&1" 2>/dev/null
		;;
	    f|forwardings|"")
		case $3 in
		    all)
			ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"select * from forwardings order by source\" 2>&1" 2>/dev/null
			;;
		    "")
			echo "Note: Using forwarding $FORWARDING"
			ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"select * from forwardings where destination = '$FORWARDING' order by source\" 2>&1" 2>/dev/null
			;;
		    *)
			ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"select * from forwardings where destination = '$3' order by source\" 2>&1" 2>/dev/null
			;;
		esac
		;;
	    *)
		echo "Unsupported argument given to l|list. Please try $0 help."
		;;
	esac
	;;
    a|add)
    	case $2 in
	    u|user)
		if [ "$3" == "" -o "$4" == "" ]; then
		    echo "Too few arguments given to a|add u|user. Please try $0 help."
		    exit;
		fi
		ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"insert into users VALUE('$3','$4')\" 2>&1" 2>/dev/null
		;;
	    d|domain)
		if [ "$3" == "" ]; then
		    echo "Too few arguments given to a|add d|domain. Please try $0 help."
		    exit;
		fi
		ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"insert into domains VALUE('$3')\" 2>&1" 2>/dev/null
		;;
	    f|forwarding)
		if [ "$3" == "" ]; then
		    echo "Too few arguments given to a|add f|forwarding. Please try $0 help."
		    exit;
		fi
		case $4 in
		    "")		
			echo "Note: Using forwarding $FORWARDING"
			ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"insert into forwardings VALUE('$3','$FORWARDING')\" 2>&1" 2>/dev/null
			;;
		    *)
			ssh $MAILSERVER "mysql -u $MYSQL_USER $MYSQL_PASS -D mail -t -e \"insert into forwardings VALUE('$3','$4')\" 2>&1" 2>/dev/null
			;;
		esac
	esac
	;;
    h|help|"")
    	echo "Possible commands are:"
	echo "	(l|list) [(f|forwardings) [\$destination|all|""]|(u|users|user)|(d|domains|domain)]"
	echo "		Lists forwardings [to address \$destination|to all addresses|to default address defined in the script], users or domains. Defaults to forwardings."
	echo "	(a|add) ((f|forwarding) \$source [\$destination]|(u|user) \$email \$password|(d|domain) $domain)"
	echo "		Adds the specified entry to the table. If you’re adding a forwarding and no destination is given, FORWARDING is used."
	echo "You can set the variables MYSQL_USER, MYSQL_PASS, MAILSERVER and FORWARDING according to your needs. If they are not defined, default values will be applied."
	;;
esac

