#! /usr/local/bin/perl -w use strict; use vars qw($Progname $Usage $dir $subdir $user $verbose); $Progname = "mdforward-migrate"; $Usage = "usage: $Progname [-v] [directory]\n"; $dir = shift @ARGV; if (defined $dir and $dir eq "-v") { $verbose = 1; $dir = shift @ARGV; } $dir ||= "$ENV{HOME}/.mdforward"; if ($dir !~ m,^/,) { warn "$Progname: directory must be an absolute pathname\n"; die $Usage; } if (@ARGV) { die $Usage; } $user = $ENV{USER}; die "$Progname: \$USER not set in environment\n" unless defined $user; chdir $dir or die "$Progname: cannot chdir to $dir: $!\n"; opendir(DIR, $dir) or die "$Progname: cannot open directory $dir: $!\n"; while (defined($subdir = readdir(DIR))) { next if $subdir eq "."; next if $subdir eq ".."; next unless -d $subdir; if ($subdir !~ /^[a-z0-9:_\.\-]+$/) { warn "$Progname: skipping $subdir\n"; next; } # Don't test for symlinks; whole domains can have the same # set of addresses so we must process them twice. Ignore # anything therefore that starts off "$user-" later on. if (not chdir($subdir)) { die "$Progname: can't chdir to $dir/$subdir: $!\n"; } opendir(SUBDIR, ".") or die "$Progname: can't open directory $dir/$subdir: $!\n"; my $domain = $subdir; $domain =~ s/\./:/g; # Slurp all the files; we're about to modify this directory # so readdir() is not the best idea. my @files = readdir(SUBDIR); close(SUBDIR); for my $control (@files) { next if $control eq "."; next if $control eq ".."; if ($control eq "default" or $control =~ /^$user-.*-/) { warn "$Progname: no link created for $subdir/$control\n" if $verbose; next; } my $target = "$user-$domain-$control"; if (-e $target) { warn "$Progname: $subdir/$target already exists\n" if $verbose; next; } if (not -e $control) { warn "$Progname $subdir/$control doesn't exist, skipping\n"; next; } if (not -f $control) { warn "$Progname: $subdir/$control is not a regular file, skipping\n"; next; } if (symlink($control, $target)) { warn "$Progname: symlink $subdir/$control to $subdir/$target\n" if $verbose; } else { die "$Progname: symlink $subdir/$control to $subdir/$target: $!\n"; } } chdir $dir or die "$Progname: could not chdir back to $dir: $!\n"; } closedir(DIR); exit 0;