summaryrefslogtreecommitdiff
path: root/localmb2md
blob: b5864c44fe32b02b3a2f30897223c43407883aea (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.7 2006-03-24 14:03:00 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. echo "Locking INBOX"
  38. mail-lock
  39. mail-touchlock &
  40. # Save the PID of the mail-touchlock process
  41. BADGER="$!"
  42. # Convert INBOX and purge if not empty
  43. if [ -s "/var/mail/$USER" ]; then
  44. mb2md -m
  45. echo -n '' > "/var/mail/$USER"
  46. else
  47. # Create Maildir for INBOX if not there already
  48. mkdir -p ~/Maildir/cur ~/Maildir/new ~/Maildir/tmp
  49. chmod -R u=rw,go=,u+X ~/Maildir
  50. fi
  51. # Reroute future incoming mail to Maildir
  52. echo 'DEFAULT=$HOME/Maildir/' > ~/.procmailrc
  53. # Allow incoming mail again
  54. kill "${BADGER}"
  55. mail-unlock
  56. echo "INBOX unlocked again"
  57. fi
  58. # Convert secondary mailboxes
  59. if [ -d ~/mail ]; then
  60. mv ~/mail ~/mail.abandoned
  61. mb2md -R -s mail.abandoned
  62. # Grab dovecot subscriptions
  63. if [ -f ~/mail/.subscriptions ]; then
  64. mv ~/mail/.subscriptions ~/Maildir/.subscriptions
  65. # ...or UW-imap ones
  66. elif [ -f ~/.mailboxlist ]; then
  67. mv ~/.mailboxlist ~/Maildir/.subscriptions
  68. fi
  69. fi