#!/bin/csh -f
# updlists
# -------------------------------------------------------------------------
# Shell script to update my mailing lists from the Mozilla Thunderbird 
# address book.
# -------------------------------------------------------------------------
# Revision History:
# $Log$
# -------------------------------------------------------------------------

if ($1:q == "-h" || $1:q == "--help") then
   echo "Usage: $0:t"
   exit 1
endif

# Keep old LDIF and MAB copies of address book, so I can revert to it if
# necessary after a disaster like accidentally deleting an entire mailing 
# list.
# Note: Keep 2 old sets just in case I run this script after accidentally
#       deleting a person or list and before recovering.  Don't want the
#       *.sqlite that I accidentally updated outside of this script to 
#       overwrite the old *.sqlite.  Give myself a chance to recover via:
#       - Accept that *.sqlite is already damaged
#       - Copy *.ldif to a temp location to capture state last time this
#         script was run
#       - Copy lists/* to a temp location too
#       - Copy *.sqlite.old or /Quick/.../*.sqlite to *.sqlite to restore 
#         most recent undamaged copy.
#       - Run this script to generate damaged *.ldif
#       - Note the diffs, good and bad, as it's generated
#       - Compare generated *.ldif with temp copy and reapply changes
#         interactively in TBird to fully restore *.sqlite
#       - Re-run this script to generate restored *.ldif and lists/*
#       - Compare *.ldif with temp *.ldif to confirm
#       - Compare lists/* with temp lists/* to confirm
cp -v ~/fred/Mozilla/TBird/Profile/abook.ldif.old \
      ~/fred/Mozilla/TBird/Profile/abook.ldif.older
cp -v ~/fred/Mozilla/TBird/Profile/abook.sqlite.old \
      ~/fred/Mozilla/TBird/Profile/abook.sqlite.older
cp -v ~/fred/Mozilla/TBird/Profile/abook.ldif \
      ~/fred/Mozilla/TBird/Profile/abook.ldif.old
cp -v ~/fred/Mozilla/TBird/Profile/abook.sqlite \
      ~/fred/Mozilla/TBird/Profile/abook.sqlite.old

# How to export addressbook automatically instead of having user do it?
# Netscape has a -export option, but Thunderbird does not.
tbw -addressbook

#windiffa ~/1.ldif ~/fred/Mozilla/TBird/Profile/abook.ldif
# -+S = Use default value (off) for -S option.  Allow long lines to wrap.
#       - This is needed because I typically do NOT want wrapping, so I
#         included -S in the value of the LESS environment variable.  In
#         this case long lines are common and I prefer wrapping.
diff -s ~/1.ldif ~/fred/Mozilla/TBird/Profile/abook.ldif |& grep -v -x -e "---" |& grep -v modifytimestamp: |& less -+S

while (1 == 1)
    echo -n "Accept these address book changes and generate lists (y/n/s)? "
    set accept=$<
    if ($accept == "y") then
        break
    else if ($accept == "s") then
        # Sort diffs to make it easier to ignore large-scale spurious
        # changes caused by TBird exporting people in a different order
        # than last time.
        diffsorted ~/1.ldif ~/fred/Mozilla/TBird/Profile/abook.ldif |& grep -v modifytimestamp: |& less -+S
        # Do not break or exit.  Instead, keep prompting the user for y or n.
        #break
    else if ($accept == "n") then
        exit 0
    endif
end

# Copy the latest dumped LDIF file (with short name used by interactive 
# user) to the TBird folder, for comparison with future dumps.
cp -v ~/1.ldif ~/fred/Mozilla/TBird/Profile/abook.ldif

#
# Generate mail list files from Mozilla Thunderbird address book.
#
echo ""
echo "Updating old lists for imminent comparison..."
cd ~/fred/Mozilla/TBird/Lists
cp -v ./*.* ./OLD
echo ""
echo "Generating and sorting (case-insensitive) new lists..."
maillist ~/fred/Mozilla/TBird/Profile/abook.ldif .
foreach file_to_sort (./*.lis)
   sort -f $file_to_sort:q > $file_to_sort:q.1
   mv $file_to_sort:q.1 $file_to_sort:q
end

#
# Change notgenesis to a negated list
#
sed -i .bak -e "s/BCC:/NOT:/g" notgenesis.lis
rm -v notgenesis.lis.bak

echo ""
echo "Comparing new and old lists..."
# |& = Pipe both stdout and stderr
# -v = Lines NOT containing the pattern
# -x = Only if the pattern matches the ENTIRE line
# -e = Treat pattern as pattern, NOT as an option, even if it starts with -
# "^[0-9]" = Lines starting with digits like:  2509a2511, 187c187, 348d347
# -I = Ignore case in searches
# -+F = Don't exit less if one screen or less
#?? "+/^[^<>].*" = Find and highlight all lines not starting with < or >
# "+/^(> |[^<>].*)" = Find and highlight all occurrences of "> " at the
#                     start of a line, and the entire line for all lines 
#                     not starting with < or >.  This makes it easier to 
#                     scan for additions to lists, while noting deletions.
#?? diff . ./OLD |& grep -v -x -e "---" | grep -v "^[0-9]" | less -I -+F "+/^[^<>].*"
diff . ./OLD |& grep -v -x -e "---" | grep -v "^[0-9]" | less -I -+F "+/^(> |[^<>].*)"
echo ""
echo "Comparing new and old mail filters..."
# Note: Double quotes outside the backticks cause the list of filenames, one
#       per line, generated by find, to be quoted so that embedded whitespace
#       and special chars are tolerated.  The foreach command operates on 
#       each line as a filename, not each word of each line.
foreach mail_filter_file ("`cd ~/fred/Mozilla/TBird/Profile/mail; find . -name msgFilterRules.dat -print | sort`")
    diff ~/fred/Mozilla/TBird/Profile/mail/$mail_filter_file:q ~/fred/Mozilla/TBird.old/Profile/mail/$mail_filter_file:q
    if ($status) then
        while (1 == 1)
            echo -n "Different: $mail_filter_file  Reconcile manually (y/n)? "
            set reconcile=$<
            if ($reconcile == "y") then
                windiff ~/fred/Mozilla/TBird/Profile/mail/"$mail_filter_file" ~/fred/Mozilla/TBird.old/Profile/mail/"$mail_filter_file"
                break
            else if ($reconcile == "n") then
                exit 0
            endif
        end
    else
        echo "Identical: $mail_filter_file"
    endif
end

if ($#argv > 0) then
   echo ""
   echo "Showing list membership for $*:q..."
   lists -q $*:q | less
endif



echo ""
echo Uncomment the rest soon!
exit 0
#beep Uncomment the rest soon!
#exit 1

echo ""
echo "Comparing new and old TBird prefs.js..."
diff ~/fred/Mozilla/TBird/profile/prefs.js ~/fred/Mozilla/TBird.old/profile/prefs.js > /dev/null
if ($status) then
    while (1 == 1)
        echo -n "Different: prefs.js  Reconcile manually (y/n)? "
        set reconcile=$<
        if ($reconcile == "y") then
            windiff ~/fred/Mozilla/TBird/profile/prefs.js ~/fred/Mozilla/TBird.old/profile/prefs.js
            break
        else if ($reconcile == "n") then
            exit 0
        endif
    end
else
    echo "Identical: prefs.js"
endif

#
# Review changes to other Mozilla Thunderbird files...
#
windiffa ~/fred/Mozilla/TBird ~/fred/Mozilla/TBird.old
while (1 == 1)
    echo -n "Copy all Thunderbird files (y/n)? "
    set accept=$<
    if ($accept == "y") then
        break
    else if ($accept == "n") then
        exit 0
    endif
end
cp -v -R ~/fred/Mozilla/TBird/Profile/Mail/mail.bristle.com/*      ~/fred/Mozilla/TBird.old/Profile/Mail/mail.bristle.com/
cp -v -R ~/fred/Mozilla/TBird/Profile/Mail/popmail.voicenet.com/*  ~/fred/Mozilla/TBird.old/Profile/Mail/popmail.voicenet.com/
cp -v -R ~/fred/Mozilla/TBird/Profile/Mail/pop.gmail.com/*.dat     ~/fred/Mozilla/TBird.old/Profile/Mail/pop.gmail.com/
cp -v -R ~/fred/Mozilla/TBird/Profile/Mail/mail.comcast.net/*.dat  ~/fred/Mozilla/TBird.old/Profile/Mail/mail.comcast.net/
cp -v -R ~/fred/Mozilla/TBird/Profile/Mail/mail.stluka.com/*.dat   ~/fred/Mozilla/TBird.old/Profile/Mail/mail.stluka.com/
cp -v    ~/fred/Mozilla/TBird/Profile/*                            ~/fred/Mozilla/TBird.old/Profile/