#!/usr/bin/perl
#
# uw2cyrus - reads uw-imapd's .mailboxlist, and invokes mbox2cyrus on each
# mbox folder AFTER setting .qmail.
#
# To use: set $mbox2cyrus and edit '# CHANGE ME' appropriately (Note, if you
# are using Sendmail or Postfix, you want to make $dot_qmail a .forward file,
# and remove the preceding & on the '# CHANGE ME' line.)
#
# This script written by Danny Howard of Tellme Networks, and shared in the
# spirit of the "BSD style" license.
#
# Copyright 1999,2000, Danny Howard, Tellme Networks, Inc.
# <dannyman@tellme.com>
# 
# Tellme Open Source License
# 
# Permission is hereby granted under the copyrights of Tellme, free of charge,
# to any person obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the
# following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# 
# mbox2cyrus can be found at
# http://www.dannyland.org/~dannyman/warez/mbox2cyrus

$user = $ARGV[0] or die "Usage: $0 <user>\n";

$mbox2cyrus = "path/to/mbox2cyrus";

$mailboxlist = "/home/$user/.mailboxlist";
$spool = "/var/spool/mail/$user";

$tmpdir = "/home/$user/.uw2cyrus";
$dot_qmail = "/home/$user/.qmail";

if( -e $tmpdir ) { die "$tmpdir exists!\n"; }
if( -e $dot_qmail ) { die "$dot_qmail exists!\n"; }

open( DQ, ">$dot_qmail" ) or die "Can't open $dot_qmail: $!\n";
print DQ "&$user\@YOUR IMAP SERVER\n"; # CHANGE ME
close DQ;
{ my ($x,$x,$uid,$gid) = getpwnam($user); chown $uid, $gid, $dot_qmail; }

mkdir $tmpdir, 0755;
chdir $tmpdir;

# First, symlink spool
symlink $spool, $user;
system($mbox2cyrus, $user);

# Do mailboxlist
open( MBL, $mailboxlist ) or warn "Hrmmm, no mailboxlist ....\n";
while( <MBL> ) {
	chomp;
	$mbox = $_;
	$mbox =~ s}^/home/$user/}};
	$cbox = $mbox;
	$mbox = "/home/$user/$mbox";
	$cbox =~ tr}/}.};
	$cbox = "$user.$cbox";
#	print "$_\t$mbox\t$cbox\n";
	symlink $mbox, $cbox;
	system($mbox2cyrus, $cbox);
}
