#!/bin/sh
#
# Goal of this script is to transform a vpopmail Database
#
# It migrates a database designed to work with vpopmail compiled with --disable-many-domains
# into a database version working with vpopmail compiled with --enable-many-domains
#
# It also supposes vpopmail compiled with --enable-auth-module=mysql 
# 
# Typical usage is
#
# 1° Compile vpopmail with --enable-many-domains option on a new server
# 2° On this new server, import database version previously designed to work with vpopmail compiled with --disable-many-domains
# 3° Run this script
#
# Usage sample on http://qmailrocks.thibs.com/upgrade-from-qmailrocks.php
#
# Use at your own risks !
#
# July 2010 - Thibault RICHARD (thibs@NO-SPAMthibs.com)
#######################################################

DATABASE_NAME=vpopmail
DATABASE_ROOT_PASSWORD=XXXXX
VPOPMAIL_INSTALL_DIR=/home/vpopmail

# Create the vpopmail table with a fake account creation
$VPOPMAIL_INSTALL_DIR/bin/vadddomain test.com brol
$VPOPMAIL_INSTALL_DIR/bin/vadduser test@test.com brol
$VPOPMAIL_INSTALL_DIR/bin/vdeluser test@test.com
$VPOPMAIL_INSTALL_DIR/bin/vdeldomain test.com

for i in `echo "show tables" | mysql -p$DATABASE_ROOT_PASSWORD $DATABASE_NAME|grep -v Tables_in_`;
do
        if ! [ $i = dir_control ] && ! [ $i = lastauth ] && ! [ $i = vlog ] && ! [ $i = valias ] && ! [ $i = vpopmail ]; then
                domain_name=`echo $i | sed -e 's/\(.*\)_/\1./' -e 's/_/-/g'`
                echo $domain_name;
                mysql -p$DATABASE_ROOT_PASSWORD -D $DATABASE_NAME -B --skip-column-names -e "INSERT INTO vpopmail SELECT pw_name, '$domain_name', pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell, pw_clear_passwd FROM $i"
                mysql -p$DATABASE_ROOT_PASSWORD -D $DATABASE_NAME -B --skip-column-names -e "DROP TABLE $i"
        fi
done
