#!/usr/local/bin/perl

use strict;

# to change vdelivermail's ".qmail" files to ".vpopmail"

my %domains;
open(A, '/var/qmail/users/assign') || die "cannot open assign: $!\n";
while(<A>){
	if(/^\+[^:]+\-:([^:]+):\d+:\d+:([^:]+):/){
		$domains{$1} = $2;
	}
}
close A;

while(my($key,$val) = each %domains){
	if(open(VP, "$val/vpasswd")){
		while(<VP>){
			if(/^[^:]+:[^:]+:\d+:\d+:[^:]+:([^:]+)/){
				my $maildir = $1;
				if(opendir(DIR, $maildir)){
					foreach my $file (grep {/^\.qmail/} readdir DIR){
						if($file eq '.qmail'){
							rename("$maildir/.qmail","$maildir/.vpopmail") || warn "could not rename $maildir/.qmail: $!\n";
							print "renamed $maildir/$file to .vpopmail\n";
						}else{
							my ($new) = $file =~ /^\.qmail-(.+)/;
							rename("$maildir/$file","$maildir/.vpopmail-$new") || warn "could not rename $maildir/$file: $!\n";
							print "renamed $maildir/$file to .vpopmail-$new\n";
						}
					}
					closedir DIR;
				}else{
					warn "could not open $maildir for upgrade!\n";
				}
			}
		}
		close VP;
	}else{
		warn "$val/vpasswd not opened correctly!!\n";
	}
}
