#!/bin/sh
# archive this, or these
# written by duus on Nov 11, 2005
# see PM/UnixShellScripts/backup/
#
# shell script that makes a file backup in archive/
#
__thisdate=`date +%y_%m_%d`
__error=0
__silent=0
__numfiles=0
_SCRIPT_LOCATION=~/bin/
_SCRIPT_NAME=archthis
#h archthis v 0.1 by duus
#h  Copies an arbitrary list of files into a subdirectory ./archive,
#h   after appending the date and a unique numerical suffix to each.
#h  Also creates new folder ./archive if none exists.
#h   (Date is YY_MM_DD so alphabetical sorting is chronological.)
#h  
#h __Example__
#h $ ls                                         
#h file.txt        file2.txt                        
#h $ archthis *.txt                             
#h                                              
#h creating directory ./archive                 
#h cp ./file.txt ./archive/file.txt.06_5_17.1   
#h cp ./file2.txt ./archive/file2.txt.06_5_17.1 
#h                                              
#h   archplus archived 2 file(s), had 0 error(s). 
#h $ ls archive/                             
#h file.txt.06_5_17.1   file2.txt.06_5_17.1
if [ "$1" = "version" ]
	then
	echo "archthis v 0.1 by duus"
	exit 1;
elif [ "$1" = "help" -o "$1" = "h" -o -z "$1" ]
	then 
		grep "^#h" ${_SCRIPT_LOCATION}/${_SCRIPT_NAME} | more
	exit 1;
fi
#
# this else embeds the bulk of the program
if [ ! -d ./archive ]
		then
			echo "creating directory ./archive"
			mkdir archive
fi

for i in $*; do
__file=$i
__suffix=1
__done=0
if [ -e $i ]
	then
	#
	#  find the next version to copy up.
	#
	while [ $__done -le 0 ]
	do
		if [ -r ./archive/$__file.$__thisdate.$__suffix ]
		then
	
		__suffix=`expr $__suffix + 1`
	
		else
	
		__done=1
	
		fi
	done
	#
	# now copy it up.
	#
	echo "cp ./$__file ./archive/$__file.$__thisdate.$__suffix"
	cp ./$__file ./archive/$__file.$__thisdate.$__suffix
	__numfiles=`expr $__numfiles + 1`
	
	else
		echo " According to my calculations, file $i does not exist."
		__error=`expr $__error + 1`
	fi
done

# finish
echo " "
echo " archplus archived $__numfiles file(s), had $__error error(s)."
##
