#!/bin/sh
# Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
# 
# Permission to use, copy, modify, and distribute this material 
# for any purpose and without fee is hereby granted, provided 
# that the above copyright notice and this permission notice 
# appear in all copies, and that the name of Bellcore not be 
# used in advertising or publicity pertaining to this 
# material without the specific, prior written permission 
# of an authorized representative of Bellcore.  BELLCORE 
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.

# Conversion from C shell to Bourne shell by Z-Code Software Corp.
# Conversion Copyright (c) 1992 Z-Code Software Corp.
# Permission to use, copy, modify, and distribute this material
# for any purpose and without fee is hereby granted, provided
# that the above copyright notice and this permission notice
# appear in all copies, and that the name of Z-Code Software not
# be used in advertising or publicity pertaining to this
# material without the specific, prior written permission
# of an authorized representative of Z-Code.  Z-CODE SOFTWARE
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.

if [ -z "$METAMAIL_TMPDIR" ]
then
	METAMAIL_TMPDIR=/tmp
fi

TREEROOT=$METAMAIL_TMPDIR/msg-parts-`whoami`

if test -z "$3" -o ! -z "$5"
then
	echo "Usage: showpartial file id partnum totalnum"
	exit 1
fi

file="$1"

# This next line is because message-id can contain weird chars
id=`echo "$2" | tr -d \!\$\&\*\(\)\|\'\"\;\/\<\>\\ `

partnum="$3"

if test -z "$4"
then
	totalnum=-1
else
	totalnum="$4"
fi

if test ! -d $TREEROOT
then
	mkdir $TREEROOT
	if test $? -ne 0
	then
		echo mkdir $TREEROOT failed
		exit 1
	fi
fi

if test ! -d ${TREEROOT}/$id
then
	mkdir ${TREEROOT}/$id
	if test $? -ne 0
	then
	        echo mkdir ${TREEROOT}/$id failed
		exit 1
	fi
fi

cp $file ${TREEROOT}/$id/$partnum
if test $? -ne 0
then
	echo cp $file ${TREEROOT}/$id/$partnum failed
	exit 1
fi

if test $totalnum -eq -1
then
	if test -r ${TREEROOT}/$id/CT
	then
		totalnum=`cat ${TREEROOT}/$id/CT`
	else
    		totalnum=-1
	fi
else
	echo $totalnum > ${TREEROOT}/$id/CT
fi

# Slightly bogus here -- the shell messes up the newlines in the headers
# if ($partnum == 1) then
#     echo $MM_HEADERS > ${TREEROOT}/$id/HDRS
# endif
found=0
ix=1
list=""
limit=$totalnum
if test $limit -eq -1
then
	limit=25
fi

while test "$ix" -le "$limit"
do
	if test -f ${TREEROOT}/$id/$ix
	then
		list="$list $ix"
		found=`expr $found + 1`
	fi
	ix=`expr $ix + 1`
done

if test "$found" = "$totalnum"
then
	cd ${TREEROOT}/$id
	cat $list > ${TREEROOT}/$id/FULL
	rm $list
	echo All parts of this ${totalnum}-part message have now been read.
	metamail -d  ${TREEROOT}/$id/FULL
	echo WARNING:  To save space, the full file is now being deleted.  
	echo You will have to read all $totalnum parts again to see the full message again.
	rm ${TREEROOT}/$id/FULL
	rm ${TREEROOT}/$id/CT
	cd ${METAMAIL_TMPDIR}
	rmdir ${TREEROOT}/$id
	rmdir ${TREEROOT} > /dev/null 2>&1
else
	if test "$totalnum" -eq -1
	then
		echo So far you have only read $found of the several parts of this message.
	else
		echo So far you have only read $found of the $totalnum parts of this message.
	fi
	echo When you have read them all, then you will see the message in full.
fi
