# Enable VT100 Emulation
TERM=vt100; export TERM	# Solaris hack to obtain nice dialogs
# Sets path to dialog
DPATH=/opt/opensun/bin
# Set dbmail commands
DBADD=/usr/sbin/dbmail-adduser
DBMAN=/usr/sbin/dbmail-maintenance
# Set Mysql Parameters
MYSQL=/opt/mysql/bin/mysql
MYSQLHOST=localhost
MYSQLUSER=mysq-dbmail-user
MYSQLPASSWD=mysql-dbmail-passwrd
# Default parameters
MAILQUOTE=10M
PMASTERPASS="hello"	# Default password for postmaster account

# Reads domains from mysql:dbmail.transport and adds a counter
domlist() {

listdom=`$MYSQL -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASSWD -s -e "SELECT domain from dbmail.transport ORDER BY domain"\
 | grep -v domain | grep -v localhost`

domcont=0
for domain in $listdom
do
	domcont=`expr $domcont + 1`
	echo "$domcont $domain"
done	
}

# Add new domain to mysql:dbmail.transport
# Add postmaster account to domain, because listdomuser() requiere at least one user
# to work properly.
adddomain() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Add Domain " --no-shadow \
--inputbox "\nWrite down the new domain.\n\n" 15 50 2> /var/tmp/domain.$$

sel=$?
newdomain=`cat /var/tmp/domain.$$`
rm /var/tmp/domain.$$

case $sel in
0)
	$MYSQL -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASSWD -s -e "INSERT INTO dbmail.transport VALUES ('$newdomain','dbmail:')"
	$DBADD a postmaster@$newdomain {crypt:}$PMASTERPASS 0 0 postmaster@$newdomain 2> /dev/null
	domtasks
	;;
1 | 255)
	domtasks
	;;
esac
}

# Delete domain in /etc/postfix/transport
deldomain() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Delete Domain " --no-shadow \
--yesno "\nYou really want to delete domain $sdomain ?\n\n" 8 50

sel=$?
case $sel in
0)	$MYSQL -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASSWD -s -e "DELETE FROM dbmail.transport WHERE domain='$sdomain'"
	domtasks
	;;
1 | 255)
	usertasks $sdomain
	;;
esac
}

# Generate domain task menu
domtasks() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Domain Tasks " --no-shadow \
--extra-button --extra-label "Add Domain" \
--menu "\nUp/Down selects domain.\n\n" 20 50 11 \
`domlist` 2> /var/tmp/menuitem.$$

sel=$?
menuitem=`cat /var/tmp/menuitem.$$`
rm /var/tmp/menuitem.$$

case $sel in
0)
	sdomain=`domlist | grep -w $menuitem | awk '{print $2}'`
	usertasks $sdomain
	;;
3)
	adddomain
	;;
1 | 255)
	mainmenu
	;;

esac
}

# --- Account Management ---

# Generates user list of selected domain
listdomuser() {
listuser=`$MYSQL -h $MYSQLHOST -s -e "SELECT userid from dbmail.users where userid like '%@$1'" | grep -v userid | awk -F@ '{print $1}'`

usercont=0
for user in $listuser
do
	usercont=`expr $usercont + 1`
	echo "$usercont $user"
done
}

# Starts new user procedure
newlogin() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Add new account to $1 " --no-shadow \
--inputbox "\nWrite down the new account login name.\n\n" 20 50 2> /var/tmp/username.$$

opt=$?
new_username=`cat /var/tmp/username.$$`
rm /var/tmp/username.$$

case $opt in
0)	newpassword $new_username
	;;
1 | 255)
	usertasks $1
	;;
esac
}

# New user password
newpassword() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Choose password for $1@$sdomain " --no-shadow \
--cancel-label "Back" \
--inputbox "\nWrite down account password.\n\n" 20 50 2> /var/tmp/password.$$

opt=$?
new_password=`cat /var/tmp/password.$$`
rm /var/tmp/password.$$

case $opt in
0)	newquote $new_username
	;;
1 | 255)
	newlogin $sdomain
	;;
esac
}

# New user mail quote
newquote() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Define mail quote for $1@$sdomain " --no-shadow \
--cancel-label "Back" \
--inputbox "\nWrite account mail quote. Mail quote is declared in bytes. Suffix number with letter M to declare quote in Megabytes. Write 0 for no quote.\n\n" 20 50 $MAILQUOTE 2> /var/tmp/quote.$$

opt=$?
new_quote=`cat /var/tmp/quote.$$`
rm /var/tmp/quote.$$

case $opt in
0)	addaccount $new_username $new_password $new_quote
	;;
1 | 255)
	newpassword $sdomain
	;;
esac
}

# Creates account
addaccount() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title "New account confirmation" --no-shadow \
--yesno "\nCreate the account $1@$sdomain with a $3 mail quote?\n\n" 15 50

opt=$?
case $opt in
0)	$DBADD a $1@$sdomain {crypt:}$2 0 $3 $1@$sdomain > /var/tmp/addresult.$$
	$DPATH/dialog --colors \
	--backtitle "\ZbDBMAIL Configuration Interface" \
	--no-shadow \
	--msgbox "`cat /var/tmp/addresult.$$`" 15 70
	rm /var/tmp/addresult.$$
	usertasks $sdomain
	;;
1 | 255)
	newquote $1
	;;
esac
}

# Deletes account
delaccount() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Delete account confirmation " --no-shadow \
--yesno "\nDelete the account $1@$2?\n\n" 15 50

opt=$?
case $opt in
0)	$DBADD d $saccount@$sdomain > /var/tmp/addresult.$$
	$DPATH/dialog --colors \
	--backtitle "\ZbDBMAIL Configuration Interface" \
	--no-shadow \
	--msgbox "`cat /var/tmp/addresult.$$`" 15 70
	rm /var/tmp/addresult.$$
	usertasks $sdomain
	;;
1 | 255)
	accmanage $saccount
	;;
esac
}

# Modify account general fuction
modaccount() {

case $3 in
"+a")
   BOXMSG="Write down new alias"
   ;;
"-a")
   BOXMSG="Write down alias to delete"
   ;;
"+p")
   BOXMSG="Write down new password"
   ;;
"-q")
   BOXMSG="Write down new mail quote. Mail quote is declared in bytes. Suffix number with letter M to declare quote in Megabytes. Write 0 for no quote."
   ;;
"-u")
   BOXMSG="Write down new name for account $2. You must provied a full address to performe this operation. For example test@example.com"
   ;;
esac

$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Modify account $2 " --no-shadow \
--inputbox "\n$BOXMSG\n\n" \
20 60 2> /var/tmp/modpara.$$

opt=$?
new_para=`cat /var/tmp/modpara.$$`
rm /var/tmp/modpara.$$

case $opt in
0)	if [ $3 = "-u" ]
        then $DBADD c $2 $3 $new_para -a $saccount@$sdomain +a $new_para > /var/tmp/addresult.$$
        else $DBADD c $2 $3 $new_para > /var/tmp/addresult.$$
        fi
        
	$DPATH/dialog --colors \
	--backtitle "\ZbDBMAIL Configuration Interface" \
	--no-shadow \
	--msgbox "`cat /var/tmp/addresult.$$`" 20 70
	rm /var/tmp/addresult.$$
	
	if [ $3 = "-u" ]
	   then domtasks
	   else accmanage $saccount
	fi
	;;
1 | 255)
	accmanage $saccount
	;;
esac
}

# Account Management
accmanage() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Manage $1 account" --no-shadow \
--extra-button --extra-label "Delete Account" \
--menu "\nUp/Down to select a task.\n\n" 20 67 11 \
Add	"Add alias" \
Password	"Set account password" \
Quote	"Change mail quote" \
Remove	"Remove alias" \
Rename	"Rename/Moves account" \
Show	"Display current settings" \
2> /var/tmp/menuitem.$$

opt=$?
menuitem=`cat /var/tmp/menuitem.$$`
rm /var/tmp/menuitem.$$

case $opt in
0)	
	case $menuitem in
	Add)	modaccount alias $saccount@$sdomain +a
		;;
	Password)
		modaccount password $saccount@$sdomain +p
		;;
	Quote)	modaccount quote $saccount@$sdomain -q
		;;
	Remove)	modaccount alias $saccount@$sdomain -a
		;;
	Rename)	modaccount rename $saccount@$sdomain -u
		;;
	Show)	$DBADD s $saccount@$sdomain > /var/tmp/addresult.$$
		$DPATH/dialog --colors \
		--backtitle "\ZbDBMAIL Configuration Interface" \
		--no-shadow \
		--msgbox "`cat /var/tmp/addresult.$$`" 20 70
		rm /var/tmp/addresult.$$
		accmanage $saccount
		;;
	esac
	;;
3)	delaccount $saccount $sdomain
	;;
1 | 255)
	usertasks $sdomain
	;;
esac
}

# Generates user tasks menu
usertasks() {

$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Manage $1 domain" --no-shadow \
--extra-button --extra-label "Add User" \
--help-button --help-label "Delete Domain" \
--menu "\nUp/Down to select a user.\n\n" 20 67 11 \
`listdomuser $1` \
2> /var/tmp/menuitem.$$

opt=$?
menuitem=`cat /var/tmp/menuitem.$$`
rm /var/tmp/menuitem.$$

case $opt in
0)	saccount=`listdomuser $sdomain | grep -w $menuitem | awk '{print $2}'`
	accmanage $saccount
	;;
2)	deldomain
	;;
3)	newlogin $sdomain
	;;
1 | 255)
	domtasks
	;;
esac
}

# Maintenace general function
maintenance() {
$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Maintenace Task Confirmation " --no-shadow \
--yesno "\nExecute $1 maintenace task?\n\n" 15 50

sel=$?
case $sel in
0)	# You can insert here a custom DBMail stop procedure
	$DBMAN $2 > /var/tmp/addresult.$$
	$DPATH/dialog --colors \
	--backtitle "\ZbDBMAIL Configuration Interface" \
	--no-shadow \
	--tailbox /var/tmp/addresult.$$ 15 70
	rm /var/tmp/addresult.$$
	# You can insert here a custom DBMail start procedure
	mainmenu
	;;
1 | 255)
	mainmenu
	;;
esac
}

# Main Menu
mainmenu() {

$DPATH/dialog --colors \
--backtitle "\ZbDBMAIL Configuration Interface" \
--title " Main Menu " --no-shadow \
--cancel-label "Exit" \
--menu "\nUp/Down to choose an option.\nEnter to select.\n\n" 20 60 6 \
Delete "Set messages to final deletion" \
Fix "Search for unconnect mailboxes/messages" \
Manage "Manage domains/accounts" \
Purge "Purge deleted messages" 2> /var/tmp/menuitem.$$

opt=$?
menuitem=`cat /var/tmp/menuitem.$$`
rm /var/tmp/menuitem.$$

case $opt in
0)
	case $menuitem in
	Delete)	maintenance delete -d
		;;
	Fix)	maintenance fix -f
		;;
	Manage)	domtasks
		;;
	Purge)  maintenance purge -p
		;;
	esac
	;;
1 | 255)
	clear
	exit 0
	;;
esac
}

# --- Start Script ---

mainmenu
