#!/bin/bash
#
# Script to export Mail RSS subscriptions to an OPML file.
# Written by VividVisions.com
#

path=`echo ~/Library/Mail/RSS`

if [ ! -d $path ] 
then
   echo "Error: ~/Library/Mail/RSS not found."
   exit 1
fi

export IFS=$'\n'
filename="Mail Export.opml"
echo '<?xml version="1.0" encoding="utf-8"?>
<opml version="1.0">
<head>
<title>Apple Mail Subscriptions</title>
<dateCreated>'`date +%Y-%m-%d' '%H:%M:%S' '%z`'</dateCreated>
</head>
<body>
<outline text="Apple Mail Import">' > $filename

for file in $(find $path -name Info.plist);
do
   name=${file%.rss*}
   name=${name##*/}
   name=${name//\"/\'}
   name=${name/&/&amp;}
   url=`grep '<string>http' "$file" | grep -o 'http[^<]*'` 
   echo "<outline type=\"rss\" xmlUrl=\"$url\" text=\"$name\" description=\"$name\" />" >> $filename
done

echo '</outline>
</body>
</opml>' >> $filename

echo "Done"
exit 0
