#!/usr/bin/perl 

# dspam_movefiles
#
# migrates an existing installation (2.7.6.10 and lower) to the new 
# directory storage format, which uses a directory named after the user
# to store db_home, database files, stats, etc.

use strict;
use vars qw { $userdir };

$userdir = shift;
if ($userdir eq "") {
  die "Syntax: dspam_movefiles [userdir]";
}

process_dir($userdir);

sub process_dir {
  my($dir) = @_;
  my(@files);

  opendir(DIR, $dir);
  @files = grep(!/^\.\.?$/, readdir(DIR));
  closedir(DIR);

  foreach(@files) {
    if (-d "$dir/$_") {
      process_dir("$dir/$_");
    } else {
      if ($_ =~ /(\S+)\.dict/) {
        my($user) = $1;
        print "Moving user $user\n";
        mkdir "$dir/$user", 0770;
        system("mv $dir/$user.* $dir/$user/");
      }
    }
  }
  return;
}
