#!/bin/sh
#
# Copyright (c) 2004-2005 Daichi GOTO <daichi@ongs.co.jp>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer. 
# 2. Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
# THE POSSIBILITY OF SUCH DAMAGE.

# author: Daichi GOTO (daichi@ongs.co.jp)
# first edition: Thu May 23 11:30:33 2002
# last modified: $Date: 2005/03/22 11:42:52 $
# version: $Revision: 1.25 $

# mh2thunderbird
#    mh Mail to Thunderbird's Mail conversion
#
# usage
#    mh2thunderbird


# check_required_program
#     check that the required program exists
# usage
#     check_required_program program_name port_directory
check_required_program()
{
    type "$1" > /dev/null || {
        echo 'error:'
        echo "    you need $1."
        echo "    install it from $2."
        ! :
    } 1>&2
}

# error
#    print error message
# usage
#    error_msg='error message'
#    error
error()
{
    echo "$error_msg" 1>&2
}

# usage
#    print usage
# usage
#    usage_msg='usage message'
#    usage
usage()
{
    echo "$usage_msg" 1>&2
}

# version
#    print version
# usage
#    version '$Revision: 1.19 $'
version()
{
    ver=$1
    ver=${ver#* }
    echo ${ver% $}
}

usage_msg="usage:
    mh2thunderbird     mh Mail to Thunderbird's Mail conversion
     -i directory   set input mh directory
     -o directory   set output directory
     -t directory   set temporary directory
     -h             print help message
     -v             print version"

# default configuration
#
maildir=~/Mail/
tempdir=./mh2thunderbird_tempdir-$$
tempfile=$tempdir/tempfile
outdir=thunderbird-Mail-$$

# check the option arguments
#
while getopts i:o:t:hv option
do
    case "$option" in
    i)
        maildir=$OPTARG/
        ;;
    o)
        outdir=$OPTARG
        ;;
    t)
        tempdir=$(echo $OPTARG | sed -e 's,^,./,' -e 's,^.//,/,')
        tempfile=$tempdir/tempfile
        ;;
    h|\?)
        usage
        exit 0
        ;;
    v)
        version '$Revision: 1.25 $'
        exit 0
        ;;
    esac
done
shift $(($OPTIND - 1))

# packf version check
#
check_required_program packf /usr/ports/mail/nmh/

yes | packf -help | grep -- -mbox > /dev/null ||
    { error_msg='error: packf(1) supported -mbox is needed'; error; exit 1; }

# make temporary directory
mkdir -p "$tempdir"

# make output directory
mkdir -p "$outdir"

# mh Mail to thunderbird's Mail conversion
find "$maildir" -type d | while read target_dir
do
    [ queue = "${target_dir##*/}" -o -z "${target_dir##*/}" ] && continue 

    mbox_name=$(echo ${target_dir#$maildir} | sed s,/,.sbd/,g)
    conf_name=$mbox_name.msf
    dir_name=$(echo ${target_dir#$maildir} | sed s,/,.sbd/,g).sbd

    # make output directory of target_dir
    mkdir -p "$outdir/$dir_name"

    # copy mh style mail files to temporary directory
    cp "$target_dir"/[0-9]* "$tempdir" > /dev/null 2>&1

    # make UNIX mbox style file
    yes | packf -file "$tempfile" +"$tempdir" > /dev/null 2>&1

    # make Thunderbird mbox style file
    [ -f "$tempfile" ] &&
        LANG=C sed 's/From <.*> /From -/' \
            < "$tempfile" 2> /dev/null > "$outdir/$mbox_name" ||
        > "$outdir/$mbox_name"

    # make conf file ('>' or ': >' style is very faster than touch command)
    > "$outdir/$conf_name"

    # remove temporary files
    rm "$tempdir"/[0-9]* "$tempfile" > /dev/null 2>&1
done

# remove temporary directory
rm -r "$tempdir"

# rename draft directory
mv "$outdir/draft" "$outdir/Drafts" 
mv "$outdir/draft.msf" "$outdir/Drafts.msf" 
mv "$outdir/draft.sbd" "$outdir/Drafts.sbd" 

# rename indox directory
mv "$outdir/inbox" "$outdir/Inbox" 
mv "$outdir/inbox.msf" "$outdir/Inbox.msf" 
mv "$outdir/inbox.sbd" "$outdir/Inbox.sbd" 

# rename sent directory
mv "$outdir/sent" "$outdir/Sent" 
mv "$outdir/sent.msf" "$outdir/Sent.msf" 
mv "$outdir/sent.sbd" "$outdir/Sent.sbd" 

# rename trash directory
mv "$outdir/trash" "$outdir/Trash" 
mv "$outdir/trash.msf" "$outdir/Trash.msf" 
mv "$outdir/trash.sbd" "$outdir/Trash.sbd" 
