X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=archive_patch.sh;h=aebe780b74c86a5ad31a6c5522a9836d3083c4db;hb=fd8a5026b904792be8a383e10819121be0d08a76;hp=8c7a236c04704ca86ae5c120f447ca0505aaf69e;hpb=3516b08d7dfc4fe05f132e6fa29b9d53cffc0e9b;p=cmccabe-bin diff --git a/archive_patch.sh b/archive_patch.sh index 8c7a236..aebe780 100755 --- a/archive_patch.sh +++ b/archive_patch.sh @@ -1,5 +1,68 @@ #!/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 directory where the file should end up (default: .)" + echo "-h This help message" + echo "-m Use the file's modification time, not the current time" + echo +} + +copy=0 +destdir=. +use_mtime=0 +while getopts "cd:hm" flag +do + case $flag in + c) copy=1;; + + d) destdir=$OPTARG;; + + h) usage + exit 1 + ;; + + m) use_mtime=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 + if [ $use_mtime -eq 1 ]; then + new_date=`stat -c '%y' "${file}" | sed 's/ .*//'` + new_file_name="${destdir}/${new_date}_`basename "${file}"`" + else + new_file_name="$destdir/`date +'%Y-%m-%d'`_`basename "${file}"`" + fi + $cmd "${file}" "${new_file_name}" || die "failed on $file" +done