#
# usage:	oldname.sh
#
# abstract:	This Bourne Shell script given oldname returns the
#		unique name in old/ so that oldname can be placed there
#		without overwriting any other file. The original oldname
#               and old/ are in the directory specified by dir_oldname.
#		The format of a file in old/ is:
#				oldname.date.#
#		date is taken from the file. If old/ does not exist
#		it is created. We assume the file exists.
#
# note(s):      1. This routine must be called using a . (period)
#		2. The variable 'oldname' holds the filename.
#		3. The variable 'dir_oldname' holds the directory where
#		   oldname and old/ exist.
#		4. date = ddmmmyy (mmm in lowercase)
#
# Copyright (c) 1995-2004 by The MathWorks, Inc.
# $Revision: 1.3.14.1 $  $Date: 2004/07/24 23:21:06 $
#------------------------------------------------------------------------
#
        if [ ! -d $dir_oldname/old ]; then
	    mkdir $dir_oldname/old
	    chmod 755 $dir_oldname/old
        fi
#
        daymonth=`(cd $dir_oldname; ls -l $oldname | awk '{ print $(NF-2) $(NF-3) }' | tr '[:upper:]' '[:lower:]')`
	year=`date | awk '{ print $NF }'`
	date=$daymonth$year
#
	nfiles=`(cd $dir_oldname/old; ls $oldname.$date.* 2>/dev/null) | awk '
#----------------------------------------------------------------------------
    BEGIN { maxn = 0; digits = "0123456789" }
          { n = split($0,a,".")
	    if (n >= 3) {
	        m = a[n]
		for (i = 1; i <= length(m); i = i + 1)
		    if (index(digits,substr(m,i,1)) == 0) 
			break
	        if (i == length(m) + 1) 
		    if (m > maxn)
			 maxn = m 
	    }
	  }
    END { print maxn }'`
#----------------------------------------------------------------------------
        nfiles=`expr $nfiles + 1`
        oldname=$oldname.$date.$nfiles
