#!/bin/bash

###########################################################################
#
#	Shell program to convert mails in mh style  mail folders  into
#       a single mbox file
#
#	Copyright 2000, 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: Program to convert any mail in mh-style mail dirs 
#       into a single mbox file. It tries to  guess all  possible "mh"
#       style mail dirs under home directory, and presens a menu
#       
#	Usage: be used from command line for interactive mode OR
#              with the mh mail dir as the first parameter.
#
#       mh2mbox             .... interactive menu
#       mh2mbox  mh-mail    .... where mh-mail is the dir under which
#                                your mh mails are stored under $HOME
#                                [Note: No "/" is to be entered)
#
#	Revisions:
#
#	Sep/10/2000	File created            ..... ver 0.1
#       Sep/11/2000     Comand line added	..... ver 0.2
#
###########################################################################

PROGNAME=`basename $0`
VERSION="0.2"

# EDIT This default output file (if needed)
DEST=$HOME/mail/mh-mbox   # New mbox created

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

function common_rtn
{
   MAIL_CNT=0

   for i in `ls $ORIG/$FOLDER 2> /dev/null` 
     do
       echo -en "."
       FNAME=$ORIG/$FOLDER/$i
       if ! [ -d $FNAME ]; then
           MAIL_CNT=$(($MAIL_CNT+1))
           get_date
           cat /tmp/mh-mbox.01 2> /dev/null >> $DEST
           cat $FNAME 2> /dev/null >> $DEST 
           echo "" >> $DEST
       fi     
     done
   echo -en "\n\t\tTotal mails converted = "$MAIL_CNT
   echo -en "\n\n\t\tDone ... saved in mbox format"
   echo -en "\n\t\tSee : $DEST \n\n"
   rm -f /tmp/mh-mbox.*

}
function write_top
{
   clear
   echo -en "\t\t###################################\n"
   echo -en "\t\t$PROGNAME                     ver $VERSION\n"
   echo -en "\t\t(c)2000        bish@nde.vsnl.net.in\n"
   echo -en "\t\t###################################\n\n"
}

function set_orig
{
   PD=`pwd`
   cd $HOME
   ls -d */ | grep "mail" > /tmp/mh-mbox.00
   ls -d */ | grep "Mail" >> /tmp/mh-mbox.00
 
   for i in `cat /tmp/mh-mbox.00` 
      do
         CNT=$(($CNT+1))
         echo -en "\t\t"$CNT"] \t"$i
         echo ""
      done
   echo -en "\n\t\t0]\tAbort without choosing .."   
   
   echo -en "\n\n\t\tWhich is your MH-Maildir ? [ 0 - "$CNT" ] ... "
   read NUMBER

   if [ "$NUMBER" = "0" ]; then
      echo -en "\n\t\tAborting ...\n"
      exit 1
   fi

   CNT=0   
   ### Get the Maildir name
   for dir_name in `cat /tmp/mh-mbox.00`
     do
       CNT=$((CNT+1))
       if [ $CNT = $NUMBER ]; then
          CHOMP_DIR=`echo $dir_name | tr -d '/'`
          ORIG=$HOME/$CHOMP_DIR
       fi
     done
   cd $PD    

}

function choose_maildir
{
   PD=`pwd`
   CNT=0
   cd $ORIG
   for i in `ls -d */` 
      do
         CNT=$(($CNT+1))
         echo -en "\t\t"$CNT"] \t"$i
         echo ""
      done
   echo -en "\n\t\t0]\tAbort"   
   echo -en "\n\n\t\tWhich Folder [ 0 - "$CNT" ] ... "
   read NUMBER

   if [ "$NUMBER" = "0" ]; then
      echo -en "\n\t\tAborting ...\n"
      exit 1
   fi
   
   CNT=0   
   
   ### Get the folder name
   for box_name in `ls -d */`
     do
       CNT=$((CNT+1))
       if [ $CNT = $NUMBER ]; then
          FOLDER=$box_name
       fi
     done
   cd $PD          
 }
         
function get_date
{
  #############################################
  # The first line of mbox format must read:
  # "From user  Sun Jun  3 23:01:09 2000"
  # with 2 spaces after username and no commas
  #############################################
  
  ## Get date from the mail itself
  cat $FNAME | grep "Date:" > /tmp/mh-mbox.01
  
  ## Add the user after From with 2 spaces
  echo -en "From "$USER"  " > /tmp/mh-mbox.02
  
  ## Put the date in the order expected
  cat /tmp/mh-mbox.01 | awk '{print $2" "$4" "$3" "$6" "$5}' >> /tmp/mh-mbox.02 2> /dev/null
  
  ## Remove any commas from the string
  cat /tmp/mh-mbox.02 | tr -d ',' > /tmp/mh-mbox.01
  
}

############## Begin ############

write_top

if ! [ "$1" ]; then
   echo -en "\t\tPossible MH-Maildir(s) are :\n\n"
   set_orig
   write_top
   echo -en "\t\tAvailable boxes in $ORIG\n\n"
   choose_maildir
   echo -en "\n\t\tProcessing --> "
   common_rtn
   exit 0
else
   MH_DIR=$HOME/$1
   if [ -d $MH_DIR ]; then
      ORIG=$MH_DIR
      write_top
      echo -en "\t\tAvailable boxes in $ORIG\n\n"
      choose_maildir
      echo -en "\n\t\tProcessing --> "
      common_rtn
      exit 0
   else 
      echo -en "\n\t\t$HOME/$1 dir does not exist\n\n"
      exit 1
   fi         
fi    
exit
  
