#!/usr/bin/perl

# vconvert-cdb-big.pl 0.9
#
# Convert VPopMail's vpasswd.cdb files to mySQL database - BIG SITE layout.
# Copyright (C) 2000 WizOffice.com Pte Ltd (Singapore)
# Author: Michael Boman Olsson <michael@wizoffice.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

use DBI qw(:sql_types);

$DOMAIN = "replace-me-with-your-domain.com";

# Where did you install VPopMail ?
$VPOPMAILHOME = "/home/vpopmail";

# The name of your mySQL server.
$SERVER = "localhost";

# Username to login to the mySQL server.
$USER = "replace-me-with-vpopmail-mysql-user";

# The password for mySQL server.
$PASSWORD = "replace-me-with-the-password-for-the-above-user";

# Database for VPopMail.
$DATABASE = "vpopmail";

# You shouldn't need to change anything after this line.

$DOMAIN_TABLE = $DOMAIN;

$DOMAIN_TABLE=~s/[\.-]/_/g;

# Open a connection to the database server.
local $dbh = DBI->connect( "dbi:mysql:$DATABASE:$SERVER",
		                "$USER",
		                "$PASSWORD",
		                {
		                  RaiseError => 1,
		                  AutoCommit => 1
		                }
		              ) or die "Database connection not made: $DBI::errstr";
            

	# Open VPopMail's vpasswd file.
	$VPASSWD_FILE = "$VPOPMAILHOME/domains/$DOMAIN/vpasswd";
	open(VPASSWD_FILE) or die "CAN'T OPEN $VPASSWD_FILE";

	# Create the domain table.
	my $sql = "CREATE TABLE $DOMAIN_TABLE ( pw_name varchar(32) DEFAULT '' NOT NULL, pw_passwd varchar(255) DEFAULT '' NOT NULL, pw_uid int(11), pw_gid int(11), pw_gecos varchar(255), pw_dir varchar(255), pw_shell varchar(255), KEY stress_test_com_idx (pw_name))"; 

	my $sth = $dbh->prepare( $sql );
	print "sql : $sql\n";
	$sth->execute();

	# Copy data from vpasswd file to database.
	while (<VPASSWD_FILE>) {
		($v_name, $v_passwd, $v_uid, $v_gid, $v_gecos, $v_dir, $v_shell) = split(/:/, $_); 

		# Remove the linefeed in each row.
		$v_shell=~s/\n//g;

		my $sql = "INSERT INTO $DOMAIN_TABLE VALUES ( '$v_name', '$v_passwd', '$v_uid', '$v_gid', '$v_gecos', '$v_dir', '$v_shell' )";

		my $sth = $dbh->prepare( $sql ); 
		print "sql : $sql\n";
		$sth->execute();

	}
	# Close the vpasswd file.
	close (VPASSWD_FILE);



# Disconnect from the database.

$sth->finish();

$dbh->disconnect();
