#!/bin/bash
#
# $Id: dlgsplit,v 1.10 2004/03/28 19:11:13 psy Exp $
#
# This script splits a text file containing n DLG messages into n files --
# named xx1, xx2, etc. -- each containing one message.
#
# Copyright (C) 2004 Tristan Miller <psychonaut@nothingisreal.com>
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Set up some variables
PROGRAMNAME="dlgsplit"
PROGRAMVERSION="v1.0"
PROGRAMCOPYRIGHT="2004 Tristan Miller"
WELCOME="$PROGRAMNAME $PROGRAMVERSION
Copyright (C) $PROGRAMCOPYRIGHT
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
DIGITS=6
PREFIX="xx"
NUM_FILES=0
QUIETMODE=0

# Usage instructions
ME=`basename $0`
USAGE="Usage: $ME [options] message_file

General options:
    -p PREFIX       use PREFIX instead of \`xx'
    -q              quiet mode (do not output progress)
    -?, --help      output this message and exit
    -v, --version   output version information and exit
"

# Error function
function error() # errormessage
{
    echo "$ME: $1" 1>&2
}

# Parse command line
while [ $# -gt 0 ]
do
    case $1 in
    -p)        shift
	       if [ $# -eq 0 ]
	       then
		    error "no prefix specified"
		    exit 1
	       fi
	       PREFIX="$1"
	       ;;
    -q)        shift
	       QUIETMODE=1
               ;;
    -\?)       echo "$USAGE"; exit 0    ;;
    --\?)      echo "$USAGE"; exit 0    ;;
    --help)    echo "$USAGE"; exit 0    ;;
    -v)        echo "$WELCOME"; exit 0    ;;
    --version) echo "$WELCOME"; exit 0    ;;
    -*)        error "invalid argument \`$1'"
	       exit 1 ;;
    *)         if [ $NUM_FILES -eq 0 ]
	       then
		    FILENAME=$1
		    let "NUM_FILES+=1"
	       else
                    error "only one message file can be specified"
                    exit 1
	       fi
	       shift
	       ;;
    esac
    shift
done
if [ $NUM_FILES -eq 0 ]
then
    error "no message file specified"
    exit 1
fi

# Create temp directory for dirty work
CWD=`pwd`
mkdir /tmp/dlgsplit.$$
cd /tmp/dlgsplit.$$

# Split file
csplit -z --digits=$DIGITS $CWD/$FILENAME '/^\[From    \]/' '{*}' \
    1>&2 >/dev/null

# Rename files according to their message numbers
for F in *
do
    G=`head -1 $F | sed -e 's/^.*MSG //;s/ OF.*//'`
    if [ $QUIETMODE -eq 0 ]
    then
	echo "Found message $G..."
    fi
    mv "$F" "$CWD/$PREFIX$G"
done

# Clean up
rmdir /tmp/dlgsplit.$$
