summaryrefslogtreecommitdiff
path: root/localmb2md
blob: 0e9fe5e97259f5f4157225d8c05dc3914923a3ab (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/sbin/adduser.local
  4. # Copyright 2005-2006 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localmb2md,v 1.6 2006-03-24 13:54:46 jonas Exp $
  7. #
  8. # Convert user account from mailboxes below /var/mail/ and ~/mail/ to
  9. # maildirs below ~/Maildir/ .
  10. #
  11. # If from a different account, execute it like this:
  12. #
  13. # su -s /bin/sh -c localmb2md - <user>
  14. #
  15. # FIXME: Make sure no MUAs or pop/imap daemons are running
  16. # TODO: Add --help (and perhaps --force) options
  17. #
  18. set -e
  19. for prg in mb2md procmail mail-lock mail-touchlock mail-unlock; do
  20. if [ ! -f "/usr/bin/$prg" ]; then
  21. echo "ERROR: $prg not installed!"
  22. exit 1
  23. fi
  24. done
  25. # Make sure Maildir is used if procmailrc exists already
  26. if [ -f ~/.procmailrc ] && egrep -v -q '^DEFAULT=\$HOME/Maildir/$' ~/.procmailrc; then
  27. echo 'ERROR: non-Maildir-aware ~/.procmailrc exists already.'
  28. echo ' Please edit manually to include the following line:'
  29. echo
  30. echo ' DEFAULT=$HOME/Maildir/'
  31. echo
  32. echo ' Make sure other rules use Maildirs too, if needed.'
  33. exit 1
  34. fi
  35. # Avoid incoming mail while messing with INBOX
  36. if [ -e "/var/mail/$USER" ]; then
  37. mail-lock
  38. mail-touchlock &
  39. # Save the PID of the mail-touchlock process
  40. BADGER="$!"
  41. # Convert INBOX and purge if not empty
  42. if [ -s "/var/mail/$USER" ]; then
  43. mb2md -m
  44. echo -n '' > "/var/mail/$USER"
  45. else
  46. # Create Maildir for INBOX if not there already
  47. mkdir -p ~/Maildir/cur ~/Maildir/new ~/Maildir/tmp
  48. chmod -R u=rw,go=,u+X ~/Maildir
  49. fi
  50. # Reroute future incoming mail to Maildir
  51. echo 'DEFAULT=$HOME/Maildir/' > ~/.procmailrc
  52. # Allow incoming mail again
  53. kill "${BADGER}"
  54. mail-unlock
  55. fi
  56. # Convert secondary mailboxes
  57. if [ -d ~/mail ]; then
  58. mv ~/mail ~/mail.abandoned
  59. mb2md -R -s mail.abandoned
  60. # Grab dovecot subscriptions
  61. if [ -f ~/mail/.subscriptions ]; then
  62. mv ~/mail/.subscriptions ~/Maildir/.subscriptions
  63. # ...or UW-imap ones
  64. elif [ -f ~/.mailboxlist ]; then
  65. mv ~/.mailboxlist ~/Maildir/.subscriptions
  66. fi
  67. fi