#!/bin/sh
#(Author: Dan Jacobson  [jacobson@eecs.nwu.edu])

#Takes VMS filenames and backups and converts to a more UNIX style of
#filenames and GNU Emacs style backups.  For example:
#POO.TXT;13	POO.TXT;14	POO.TXT;15 become
#poo.txt.~13~	poo.txt.~14~	poo.txt

#usage example: thisfile  * (or quoted filenames to avoid ";"'s)

#Converts VMS style filenames to lower case, ";" is dropped, all but
#highest version number are converted to GNU Emacs backup style
#numbering.  You might ftp the files to UNIX to their own directory,
#then run this file on them.  was running a version of ftp that made
#this conversion file necessary.  Non VMS style files shouldn't get
#disturbed by this program.

#comment this out (or put a switch around it to test) for non Berkeley Unixes:
move_flag=-i

PATH=/usr/ucb:/bin:/usr/bin:/usr/local/bin #a standard safe path on my machine

case $# in 0)
	echo "$0: usage: `basename $0` * (or quoted filenames)" >&2; exit 1
	;;
esac

#doesn't convert null (".;22") VMS filenames (they would map to ".")

set dummy `ls -d $*|	#-d: don't expand subdirectories
	sort +t\;nr1|	#would like to sort on the last field
			#but only expect 1 ";" at most per path anyway
	sed -n '
	h
	#basename
	s/.*\///
	/^\.[^a-z ;.][^a-z ;.]*;[0-9][0-9]*$/b accept
	/^[^a-z ;.][^a-z ;.]*\.[^a-z/ ;.]*;[0-9][0-9]*$/b accept
	d
	:accept
	#it is a vms filename
	#print the FROM filename
	x; p
	#get rid of its basename
	s/[^/]*$//
	#make the TO filename basename lower case
	x; y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
	#make the full TO filename, print it
	H; x; s/\n//; h; s/;[0-9][0-9]*$//; p
	#make the version number, print it
	x; s/.*;\([0-9][0-9]*\)$/\1/; p
	'`
shift #dummy
while :
do
	case $# in 0) break;; esac
	oldname=$1 newname=$2 number=$3
	if test -r $newname
		then #make it into an GNU emacs style numbered backup
			mv $move_flag $oldname $newname.~$number~
		else #was the highest numbered VMS version
			mv $move_flag $oldname $newname
	fi
	shift; shift; shift
done
