#!/usr/bin/ksh
#+++++++++++++++++++++++++++++++
#
#	Korn shell script which will copy a file
#	to another file, maintaining backup versions.
#	Format:
#		copy [from] [to]
#
#	This script will detect whether the target
#	file exists and make a copy of the target
#	file in a file called "target_file".old.n where
#	'n' defines the version number.  A version limit is
#	determined using the environment variable $version_limit.
#	If the $version_limit does not exist, then this script defaults 
#	to the 3 most recent versions.  To set a version limit
#	add the following commands to your .profile:
#	
#		version_limit=n
#		export version_limit
#
#	exit status:
#		1 = attempt to copy file to itself
#		2 = attempt to copy a directory (Not supported)
#		3 = Too many arguments
#		7 = Interrupt
#
#+++++++++++++++++++++++++++++++++++++
readonly	CPY_ERROR_CSELF=1
readonly	CPY_ERROR_DIRCOPY=2
readonly	CPY_ERROR_TOOMANYARGS=3
readonly	CPY_ERROR_INT=7
integer		MAX_VERSIONS_DEFAULT=3
readonly	MAX_VERSIONS_DEFAULT
#+++++++++++++++++++++++++++++++++++++
#	Interrupt handler
#	remove scratch file if interrupted
#+++++++++++++++++++++++++++++++++++++
function cleanup
{
	if [[ -f $temp_file ]]
	then
		rm $temp_file
	fi
	exit $CPY_ERROR_INT
}
#++++++++++++++++++++++++++++++++++++++
#	Establish a ^c handler
#++++++++++++++++++++++++++++++++++++++
trap 'cleanup' INT
temp_file="/tmp/${0##*/}${$}"
export temp_file;	# Needed for AIX

#++++++++++++++++++++++++++++++++++++++
#	If the user did not enter all arguments prompt
#	for files names.
#++++++++++++++++++++++++++++++++++++++
case ${#} in
	0)	read from?"From: "
		read to?"To: "
		;;
	1)	read to?"To: "
		from=${1}
		;;
	2)	to=${2}
		from=${1}
		;;
	*)
		print -u2"Too many arguments!  RTFM!"
		print -u2 "Format:  copy [from] [to]"
		exit $CPY_ERROR_TOOMANYARGS
		;;
esac

# Don't copy to self
if [[ ${to} = ${from} ]]
then
	print -u2 "Cannot copy to self!"
	exit	$CPY_ERROR_CSELF
fi
#Cannot currently handle directories
if [[ -d $to ]]; then
	print -u2 "$to is a directory.\nDirectory copies not supported."
	exit	$CPY_ERROR_DIRCOPY
fi

#Default to 3 versions or use user specified version limit
version_limit=${version_limit:-$MAX_VERSIONS_DEFAULT}	

# Determine whether other versions of the file exist
(ls ${to}.old.* | egrep "${to}"'\.old\.[0-9]+$'> $temp_file) 2>/dev/null

#Determine current number of versions
versions=$(wc -l < $temp_file)

# Determine highest and lowest versions
if (( versions != 0 ))
then
	max_version=$(sed '1,$s/.*\.//g' $temp_file | sort -nr|head -1)
	min_version=$(sed '1,$s/.*\.//g' $temp_file | sort -n|head -1)
fi

#Remove temporary storage
rm $temp_file
#Block interrupts until completion
trap '' INT

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Now make any necessary backup copies
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

if [[ -f ${to} && -f ${from} ]]; then
# if no versions exist make first '.old'
	if (( versions == 0 ))
	then 
		cp ${to} ${to}.old.1
#if not at version limit make new version
	elif (( versions < version_limit ))
	then
		(( max_version = max_version +1 ))
		cp ${to} ${to}.old.${max_version}
# otherwise purge min version
	else
		(( max_version = max_version +1 ))
		cp ${to} ${to}.old.${max_version}
		rm ${to}.old.${min_version}
	fi
fi
# finally do the copy
cp ${from} ${to}

# Reenable traps
trap -
#bye bye
exit 0
