4.4BSD/usr/src/contrib/mh-6.8/miscellany/sortf/sortf

#! /bin/csh -f

#	$Header: mail.sort,v 2.5 87/09/19 05:55:23 jerryp Exp $
#
#  ...USE csh BECAUSE OF msgs[1] AND shift msgs FEATURES...
#
#
# Revised Sept. 1989 Mark-Jason Dominus.
#
# New usage:  sorts [-[no]verbose] [-[no]fold] [+folder] [msgs]
#

umask 77	# PROTECT TEMPORARY FILE WITH -rw------- MODE
set mhdir = /usr/local/mh
set temp = /tmp/MSORT$$

#
#set theFolder = \
##	 '+'`grep '^Current-Folder' $HOME/mail/context | sed -e 's/.*: //'`
#
# Following code installed 13-14 Sept. 1989 M-J. Dominus
#
#Default arguments:
#  Which folder to sort?  Default = current folder.
set theFolder = `folder -fast`
#  Get help?  Default:  No help.
set helpflag = 0
#  Verbose sort?  Default:  Shut up.
set vflag = 0
#  Case-sensitive sort?  Default: Nope.
set foldflag = '-f'
#  Which messages?  Default: all
set theMessages = 'all'

# Add support for an arglist like:  sorts 4-6 8 12-23.
# The *FIRST* time you see a nonflag argument, _set_ theMessages = argument.
# *AFTER* the first time, _append_ the argument to theMessages.
set firstMsgArg = 1

#
# .mh_profile lines override default options.
#
#echo "argv before profile is $argv"
set profileLine = `grep -i '^sortf' $HOME/.mh_profile | sed -e 's/.*: //'` 
set argv = ($profileLine $argv)
#echo "argv after profile is $argv"

#
# Command-line arguments override .mh_profile and ordinary defaults.
#
while ("$argv" != '')
  if      ("$1" == '-verbose') then
	set vflag = 1
  else if ("$1" == '-noverbose') then
	set vflag = 0
  else if ("$1" == '-fold') then
	set foldflag = '-f'
  else if ("$1" == '-nofold') then
	set foldflag = ''
  else if ("$1" =~ +* ) then
# Measure twice, cut once. -D.
	set theFolder = `echo $1 | cut -c2-`
  else if ("$1" == '-help') then
	set helpflag = 1
  else
	if ("$firstMsgArg" == '1') then
		set theMessages = "$1"
		set firstMsgArg = 0
	else
		set theMessages = ($theMessages $1)
	endif
  endif
  shift
end

if ($helpflag) then
	cat <<END_OF_HELP
syntax: sortf [+folder] [msgs] [switches]
  switches are:
  -[no]verbose
  -[no]fold
  -(help)
END_OF_HELP
	exit
endif
# End of 14 Sept. 1989 argument-parsing additions.  M-J.D.

# REMOVE GAPS BEFORE SORTING (IGNORE folder'S OUTPUT):
# 
# Commented out 14 Sept. 1989 M-J.D.
#
#$mhdir/folder -pack '+'$theFolder >& /dev/null
##if ($vflag) echo "folder '`$mhdir/folder -fast`' packed for sorting"
#if ($vflag) echo "folder +$theFolder packed for sorting"

# GET MESSAGE NUMBERS AND SUBJECTS 
# THIS OLD STUFF WAS TO OVERRIDE USER'S PROFILE SWITCHES....
# NOW NEED TO REVISE THIS TO USE scan FORMAT FILE!!
# DELETE "Re:" AND STARTS OF BODIES (<<etc. etc.) FROM SUBJECT FIELDS:

if ($vflag) echo "scanning folder for message numbers and senders"

#$mhdir/scan -noheader -nonumdate -nosize -notime $argv | cut -c1-4,30- | \

#  The cut -c1-19 is because otherwise we would strip the
#  'To:' off of 'To:rissa@gargoyle' and then we would be left
#  with 'rissa@gargoyle' which compares less than
#  'rissa@gargoyle.uc' which is what we see when rissa wrote
#  to us instead of the other way around.  So we lop off the
#  last three characters to even things out.  16 Sept 1989 M-J. Dominus

$mhdir/scan -noheader '+'$theFolder $theMessages | cut -c1-4,12-29 | \
	sed -e 's/^\(.....\)[Tt][Oo]:[ 	]*/\1/' -e 's/<<.*//' | \
	cut -c1-19 > $temp


set msgs = "`colrm 5 < $temp`"

# cd TO CURRENT MAIL FOLDER, AND MAKE TEMPORARY DIRECTORY:
set foldir = `$mhdir/mhpath`
set tmpfoldir = 'MaIlSoRt$$'
chdir $foldir
mkdir $tmpfoldir

# GET LIST OF MESSAGES, IN ORDER, BY DATE:
if ($vflag) echo "sorting messages into temporary sub-folder:"
foreach sortmsg (`sort $foldflag +1 $temp | colrm 5`)
	# REMOVE LEADING BLANK ON $msgs[1]:
	if ($vflag) echo -n "$sortmsg "
	mv $sortmsg $tmpfoldir/`echo -n $msgs[1]`
	shift msgs
end
if ($vflag) echo ""

# MOVE SORTED MESSAGES INTO CURRENT FOLDER AND REMOVE TEMPORARY STUFF:
if ($vflag) echo "moving messages back to original folder"
cd $tmpfoldir
mv * $foldir
if ($status == 0) then
	cd $foldir
	rmdir $tmpfoldir
	rm $temp
endif