#!/bin/sh
-new_file_name=`date +'%Y-%m-%d'`_`basename $1`
+#
+# archive_patch.sh
+#
+# Archives old files
+#
+# copyright Colin Patrick McCabe, 2011
+#
-mv -i "${1}" "${new_file_name}"
+die() {
+ echo $@
+ exit 1
+}
+
+usage() {
+ echo
+ echo "$0: archives old files"
+ echo
+ echo "-c copy the file rather than moving it"
+ echo "-d <dir> directory where the file should end up (default: .)"
+ echo "-h This help message"
+ echo
+}
+
+copy=0
+destdir=.
+while getopts "cd:h" flag
+do
+ case $flag in
+ c) copy=1;;
+
+ d) destdir=$OPTARG;;
+
+ h) usage
+ exit 1
+ ;;
+
+ *) usage
+ exit 1;;
+ esac
+done
+shift $((OPTIND-1))
+
+if [ $copy -eq 1 ]; then
+ cmd="cp -f"
+else
+ cmd="mv -f"
+fi
+
+[ -d "$destdir" ] || die "destdir '$destdir' is not a directory"
+
+for file in "$@"; do
+ [ -e "$file" ] || die "'$file' does not exist"
+done
+for file in "$@"; do
+ new_file_name=$destdir/`date +'%Y-%m-%d'`_`basename $file`
+ $cmd "${file}" "${new_file_name}" || die "failed on $file"
+done