#!/usr/bin/mksh
# Script by Ypnose - http://ywstd.fr

BAK_EXT="BAK"
BAK_LIM=9
N=1

function p_err {
	print -ru2 -- "$1"
	exit 1
}

if (( $# != 1 )) || [[ $1 = "-h" ]]; then
	p_err "usage: ${0##*/} [FILE]"
fi

[[ ! -f $1 ]] && exit 1

while true; do
	if (( N > BAK_LIM )); then
		p_err "You reached the limit (${BAK_LIM} .${BAK_EXT} files)"
	fi
	if [[ ! -f ${1}.${BAK_EXT}.${N} ]]; then
		cp -- "$1" "${1}.${BAK_EXT}.${N}"
		print -r -- "$1 copied to ${1}.${BAK_EXT}.${N}" 
		exit
	fi
	(( N++ ))
done

exit