#!/bin/bash
################################################################################
# pinemaildir - Dale Bewley <dale@bewley.net> Fri Apr 22 09:34:47 PDT 2005
#-------------------------------------------------------------------------------
# Configure pine to use IMAP to localhost for the purposes of supporting
# Maildir folders. The IMAPd (courier, dovecot, etc) will retrieve and
# store messages in maildir format since pine only understands mbox.
#
# Thanks to http://www.neverending.org/~ftobin/resources/pine-maildir/
################################################################################

################################################################################
# Settings we need to effect
# if you cahnge the default-fcc then the monthly folder prune will fail
SETTINGS="inbox-path
	folder-collections
	feature-list"
# where are the users' .pinerc's?
PINERCS=/home/*/.pinerc
################################################################################

# look through ~/.pinerc's and find users who have 
# changed these settings rather than the empty defaults
for pinerc in $PINERCS; do
	echo -n "Checking $pinerc..."
	if [ ! -f $pinerc ]; then
		echo " not a file. skipping"
		next;
	fi

	default=1
	for setting in $SETTINGS; do
		current=`grep "^$setting=" $pinerc | cut -d= -f2`
		if [ -n "$current" ]; then
			echo "	$setting is customized to: $current"
			default=0
		fi
		current=''
	done

	if [ "$default" -lt 1 ]; then
		echo "	settings customized"
		skip="$skip $pinerc"
	else
		TMPFILE=`mktemp` || exit 1
		sed     -e 's!^inbox-path=$!inbox-path={localhost/ssl/novalidate-cert}inbox!' \
			-e 's!^folder-collections=$!folder-collections="Local IMAP" {localhost/ssl/novalidate-cert}[]!' \
			-e 's!^feature-list=$!feature-list=enable-lame-list-mode,\n\tcombined-subdirectory-display,\n\tcombined-folder-display,\n\tquell-empty-directories!' \
		< $pinerc > $TMPFILE
		echo mv $TMPFILE $pinerc
	fi

	echo
done

echo "You should check these users by hand:"
for u in $skip; do echo $u; done


