From 986eaa0d7a1f675e3e2e311d00d56cfbb62b8026 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Mon, 21 Dec 2015 10:53:42 +0000
Subject: [PATCH] Email archiving command

---
 man/freedombone-archive-mail.1.gz | Bin 0 -> 595 bytes
 src/freedombone                   |  35 +++++++----------
 src/freedombone-archive-mail      |  61 ++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 22 deletions(-)
 create mode 100644 man/freedombone-archive-mail.1.gz
 create mode 100755 src/freedombone-archive-mail

diff --git a/man/freedombone-archive-mail.1.gz b/man/freedombone-archive-mail.1.gz
new file mode 100644
index 0000000000000000000000000000000000000000..ab90e298100246200ecffa7b831cf39d3002327d
GIT binary patch
literal 595
zcmV-Z0<8TXiwFRy)^}C_1BFsiZ__Xke$THs<%O3uWkpD%LWp)N?ZmoOOUne}#ZIoV
zneB^gXIuY0=QO}Tnush#=lj09?>=9#UBTWX)?_6)90^}3{eUAYw1HE|C#P_M6--{A
zzRe-$^XU&5b^-mX0BhkoVe%|1$`$0-v-Kj+Uaa|j1hZotfJ_aTZo|MWzh2*L%FFU)
z{s6pKl;^jXo9*Szb)GFki<K6}96;~kKphB=&;{)p5boFkDx=Xw2wkN>u;wnt6Hwxz
z2SKA7;i9fUJ4sIvTH##E*v>@V8iWH1A3z7jN>PKZQ2;ln5Ojn`Z45+lkDN8cw5TmY
zBo0Jfz*aG@B5Q@KQwi-Nx*F?*kO;nWP_x8C(j}ns<lqNMNN7vRr=v&7F7yx;&lL0%
z=Uh~MHR|Q10)w+dPm98ZM2Nu0Qt1D}9iSzqG4-f4S;ki2^Rl!bjAIbz{n42dcvPAl
z?2Ry#in7%_m}rDFIP_(1#gu?xwAC?5U^~#Z<b{DyQHu!^FzLl`0<w!kluVDxQ`e|}
zhk`x%^q#-BCaG`)jYpnJ310`L+hW*|T{&Cw9%jJbf0k*!Tk^);BFl>`rQz9PR?I=g
z*oNtN+~}w}S?~*vr3lJb<7WlOI)n~?E%0o&UESQ~*__{RO)+|5;k_jJ(UT<JuTXUN
zMWhc|A$_)4ZpO>aate#tyR*sS%~^jt*U@4KqQNQ5*P}^w*C{)H_)_ovN?p;nZ5`V4
h`_HJi<aZd#tDbd-a@q@Ky?^)0egfU-t$pJI003ZmEUo|m

literal 0
HcmV?d00001

diff --git a/src/freedombone b/src/freedombone
index 9063063e6..33a121bca 100755
--- a/src/freedombone
+++ b/src/freedombone
@@ -5187,6 +5187,19 @@ function email_archiving {
       return
   fi
 
+  # ensure that the mail archive script is up to date
+  if [ -f /usr/local/bin/${PROJECT_NAME}-archive-mail ]; then
+      cp /usr/local/bin/${PROJECT_NAME}-archive-mail /etc/cron.daily/archivemail
+  else
+      if [ -f /usr/bin/${PROJECT_NAME}-archive-mail ]; then
+          cp /usr/bin/${PROJECT_NAME}-archive-mail /etc/cron.daily/archivemail
+      else
+          echo "/usr/bin/${PROJECT_NAME}-archive-email was not found. ${PROJECT_NAME} might not have fully installed."
+          exit 62379
+      fi
+  fi
+  chmod +x /etc/cron.daily/archivemail
+
   # update to the next commit
   if [ -d $INSTALL_DIR/cleanup-maildir ]; then
       if grep -q "cleanup-maildir commit" $COMPLETION_FILE; then
@@ -5222,28 +5235,6 @@ function email_archiving {
   fi
 
   cp $INSTALL_DIR/cleanup-maildir/cleanup-maildir /usr/bin
-  echo '#!/bin/bash' > /etc/cron.daily/archivemail
-  echo 'for d in /home/*/ ; do' >> /etc/cron.daily/archivemail
-  echo '  USERNAME=$(echo "$d" | awk -F '"'"'/'"'"' '"'"'{print $3}'"'"')' >> /etc/cron.daily/archivemail
-  echo '  if [[ $USERNAME != "git" ]]; then' >> /etc/cron.daily/archivemail
-  echo '    if [ -d /home/$USERNAME/Maildir ]; then' >> /etc/cron.daily/archivemail
-  echo '      MUTTRC=/home/$USERNAME/.muttrc' >> /etc/cron.daily/archivemail
-  echo '      python /usr/bin/cleanup-maildir --archive-folder="archive" --maildir-root="/home/$USERNAME/Maildir" archive ""' >> /etc/cron.daily/archivemail
-  echo '      chown -R $USERNAME:$USERNAME /home/$USERNAME/Maildir/archive-*' >> /etc/cron.daily/archivemail
-  echo '      if [ -f $MUTTRC ]; then' >> /etc/cron.daily/archivemail
-  echo '        MUTT_MAILBOXES=$(grep "mailboxes =" $MUTTRC)' >> /etc/cron.daily/archivemail
-  echo '        BACKUP_DIRECTORY=archive-$(date +"%Y")' >> /etc/cron.daily/archivemail
-  echo '        if [[ $MUTT_MAILBOXES != *$BACKUP_DIRECTORY* ]]; then' >> /etc/cron.daily/archivemail
-  echo '          sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$BACKUP_DIRECTORY|g" $MUTTRC' >> /etc/cron.daily/archivemail
-  echo '          chown $USERNAME:$USERNAME $MUTTRC' >> /etc/cron.daily/archivemail
-  echo '        fi' >> /etc/cron.daily/archivemail
-  echo '      fi' >> /etc/cron.daily/archivemail
-  echo '    fi' >> /etc/cron.daily/archivemail
-  echo '  fi' >> /etc/cron.daily/archivemail
-  echo 'done' >> /etc/cron.daily/archivemail
-
-  echo 'exit 0' >> /etc/cron.daily/archivemail
-  chmod +x /etc/cron.daily/archivemail
 
   echo 'email_archiving' >> $COMPLETION_FILE
 }
diff --git a/src/freedombone-archive-mail b/src/freedombone-archive-mail
new file mode 100755
index 000000000..ee9e6a19e
--- /dev/null
+++ b/src/freedombone-archive-mail
@@ -0,0 +1,61 @@
+#!/bin/bash
+#
+# .---.                  .              .
+# |                      |              |
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
+#
+#                    Freedom in the Cloud
+#
+# Archives old email
+
+# License
+# =======
+#
+# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+PROJECT_NAME='freedombone'
+
+export TEXTDOMAIN=${PROJECT_NAME}-archive-mail
+export TEXTDOMAINDIR="/usr/share/locale"
+
+for d in /home/*/ ; do
+    USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
+    if [[ $USERNAME != "git" ]]; then
+        # for every user who has a mail directory
+        if [ -d /home/$USERNAME/Maildir ]; then
+            MUTTRC=/home/$USERNAME/.muttrc
+            # update archives
+            python /usr/bin/cleanup-maildir --archive-folder="archive" --maildir-root="/home/$USERNAME/Maildir" archive ""
+            # ensure the user has permissions on the archives
+            for archive_dir in /home/$USERNAME/Maildir/archive-* ; do
+                chown -R $USERNAME:$USERNAME $archive_dir
+            done
+            # add the archive to .muttrc if needed
+            if [ -f $MUTTRC ]; then
+                MUTT_MAILBOXES=$(grep "mailboxes =" $MUTTRC)
+                BACKUP_DIRECTORY=archive-$(date +"%Y")
+                if [[ $MUTT_MAILBOXES != *$BACKUP_DIRECTORY* ]]; then
+                    sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$BACKUP_DIRECTORY|g" $MUTTRC
+                    chown $USERNAME:$USERNAME $MUTTRC
+                fi
+            fi
+        fi
+    fi
+done
+
+exit 0
-- 
GitLab