#!/bin/bash
###########################################################################
#
#	Shell program to share common mail folders between pine and mutt
#
#	Copyright 2001, USM Bish, <bish@nde.vsnl.net.in>.
#
#	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 2 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.
#
#	Description: Mail being processed through fetchmail and proc-
#       mail are downloaded into /var/spool/mail/[user], and procmail 
#       thereafter  distributes the mail in mbox  format into desired
#       subdirs under the  home dir.  This program makes such in mail 
#       to be commonly shared by mutt and pine.
#
#	Usage:
#
#		MuttMail
#
#	Revisions:
#
#	Jun 10 2001	 File created           .......... ver 0.0
#       Jun 10 2001      Local mail for one user  ........ ver 0.1
#       Jun 11 2001      General purpose for all users ... ver 0.2 
#       Jul 20 2001      Amended to "0" for quit ......... ver 0.3
#       Oct 02 2001      Triple column menu     .......... ver 0.4
#       Oct 21 2001      Support for madbk      .......... ver 0.5 
#       Nov 03 2001      .procmail.log trimming .......... ver 0.6
#
###########################################################################

##########################################################################
# User defined variables
##########################################################################

### Where is your mailbox under $HOME ?
### Usually it is ~/mail or ~/Mail
### Setting this is MANDATORY (skip ~/ prefix)

MAILDIR=mail            

### EDIT this for loaction of madbk program on your system
### Usual recommended path /usr/local/bin/madbk
### Keep these 2 lines commented if madbk is not installed.

#MADBK=~/myprogs/madbk/madbk
MADBK=/usr/local/bin/madbk

### Where is the procmail log file for your box ?
### Also give the max log file size of log file permissible in kb
### Keep these 2 lines commented if procmail.log is not generated

PROCMAIL_LOG=/home/bish/mail/.procmail.log
LOGFILE_SIZE=20

#########################################################################
# DO NOT CHANGE BELOW THIS LINE
#########################################################################

###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.6"

TOT_CNT=0               # Initialise to an impossible figure

#########################################################################
# Functions
#########################################################################

function which_box
{
        CNT=0
        for box_name in `ls $HOME/$MAILDIR`; do
            CNT=$((CNT+1)) 
            if [ $CNT = $ANSWER ]; then
                 MY_BOX=$box_name
            fi   
        done
}       # end of which_box 

function check_maildir
{       
        
        CNT=0
        PASS=0

        for box_name in `ls $HOME/$MAILDIR`; do
            CNT=$((CNT + 1))

            # Triple column using ANSI sequence
            case "$PASS" in
            
            0) echo
               ALIGN="\033[09G"
               PASS=1
               ;;
            
            1) ALIGN="\033[30G"
               PASS=2
               ;;
               
            2) ALIGN="\033[51G"
               PASS=0

            esac
            
            OUT_STR=$CNT"]  "$box_name
            echo -en $ALIGN$OUT_STR 
        done

        TOT_CNT=$CNT

        echo -en "\n\n\t0]  Quit this program\n\n"

        if [ $MB = "Y" ]; then
           echo -en "\tM]  Mutt Address Book (madbk)\n"
        fi
 
        if ! [ "$PROCMAIL_LOG" = "" ]; then       
            if [ $PROCMAIL = "Y" ]; then
               SIZE=`du -k $PROCMAIL_LOG | awk '{print $1}'`
               if [ "$SIZE" -gt "$LOGFILE_SIZE" ]; then
                  TRUNCATE="Y"
                  echo -en "\tT]  Truncate procmail log file [ size : $SIZE kb ]\n"
               else
                  TRUNCATE="N"   
               fi
            fi      
        fi
                       
}       # End of check_maildir

function do_it
{

        mutt -f $MAILDIR/$MY_BOX
        
}       # End of do_it

function menu
{

        clear
        
        #############################################
        # First print the top portion of the menu
        #############################################
	cat <<- -EOF2-


        ${PROGNAME}   [ver. ${VERSION}]
	
        Mailboxes available for $USER:
-EOF2-
        ############################################
        # Now input the variable portion of the menu
        ############################################

        check_maildir
        
        ############################################
        # last section of menu
        ############################################
        
        echo ""                
	echo -en "        Enter Option : "
	echo -en "[0]-["$TOT_CNT"] -->> "
	read ANSWER
        #echo ""	
       
        
}	# end of menu
        
###########################################################################
#	Program starts here
###########################################################################

## Check if procmail log file exists

if [ -s $PROCMAIL_LOG ]; then
   PROCMAIL="Y"
else
   PROCMAIL="N"
fi


## Check if madbk is installed

if ! [ "$MADBK" = "" ]; then
   if [ -s "$MADBK" ]; then
      MB="Y"
   else
      MB="N"
   fi
else
   MB="N"
fi
         
## Just loop forever
until [ "$ANSWER" = "999" ];
do  
   # Present menu and get user input
   menu 
   
   case $ANSWER in

   # madbook is found so execute   
   M|m ) if [ -s "$MADBK" ]; then
            $MADBK
         else
            echo
         fi   
         ;;

   # Hidden option -a to add to madbk database
   A|a ) if [ -s "$MADBK" ]; then
            $MADBK -a
         else
            echo
         fi   
         ;;

   # Hidden option -d to delete from madbk database
   D|d ) if [ -s "$MADBK" ]; then
            $MADBK -d
         else
            echo
         fi   
         ;;

   # procmail log too large so truncate
   T|t ) if [ "$TRUNCATE" = "Y" ]; then
            tail -30 $PROCMAIL_LOG > $HOME/procmail.log
            EXTN=$(date +%d%m%y)
            rm -f $HOME/procmail.log.$EXTN.gz
            mv $PROCMAIL_LOG $HOME/procmail.log.$EXTN
            gzip $HOME/procmail.log.$EXTN
            mv $HOME/procmail.log $PROCMAIL_LOG
            echo
            echo -en "\tS U C C E S S ....\n"
            echo -en "\t"$PROCMAIL_LOG" has been truncated and \n"
            echo -en "\tarchived at : $HOME/procmail.log.$EXTN.gz\n\n"
            echo -en "\tPress [Enter] to continue"
            read
            $PROGNAME
         fi
         ;;                  
   
   0|q|Q )   # If exit chosen then exit loop
         break
         ;;
         
   * )   # Proceed further as per number chosen
         PD=`pwd`  
         cd $HOME
         which_box
         do_it
         cd $PD
   esac
         
done

echo ""
echo "        Bye ..."
echo ""
exit 0
   