mutt-edit-label 714 B

123456789101112131415161718192021222324252627282930
  1. #!/bin/bash
  2. FILE=$1
  3. clean_labels() {
  4. echo $* | \
  5. sed -r -e 's/,/ /g' -e 's/ +/ /g' -e 's/^\s*//' -e 's/\s*$//' | \
  6. tr '[[:lower:]]' '[[:upper:]]' | \
  7. tr ' ' '\n' | \
  8. sort -u | tr '\n' ' ' | \
  9. sed 's/\s*$//'
  10. }
  11. prepare_labels() {
  12. clean_labels $* | sed 's/ /, /g'
  13. }
  14. CURRENT_LABELS=$(clean_labels $(formail -z -x X-Label < "${FILE}"))
  15. read -e -p "Enter labels (space separated): " -i "$CURRENT_LABELS" NEW_LABELS
  16. NEW_LABELS=$(prepare_labels $NEW_LABELS)
  17. FILE_TMP=$(mktemp "$(basename $(readlink -f "${0}"))_XXXXXX")
  18. if formail -z -I "X-Label: $NEW_LABELS" < "${FILE}" >> "${FILE_TMP}" && \
  19. cp "${FILE_TMP}" "${FILE}" ; then
  20. rm -f "${FILE_TMP}"
  21. fi
  22. exit 0