#!/bin/sh # cpdate -- makes sure you have a backup copy of the file you are going # to edit # # This is a classical ASS: Arse Saving Script # # Par Leijonhufvud, 1999-11-25 # # Updated to check for the existance of LONGDATE and LONGNOTE files # PKL, 2000-07-13 # # # We have two options: if just one argument is given, # then we assume that is a filename and add a date # (and possibly time). If we get two arguments, # then we take the first one as the filename, and the # second one as the suffix (in case of conflict we add the # date+time after that suffix, analogous to the first case). # # Before doing anything we test to make sure that the source file # exists. If not, we get an error message and exits. # # In the extremely unlikley case that the LONGDATE (or LONGNOTE) # file already exists we get an an error message. Trying again will # (as suggested) almost certainly help unless you have a rather special # collection of files in that directory. # # TODO Allow for a specified destination directory, rather than the # same one as the source file lives in. # DATE_BINARY='/bin/date' CP='/bin/cp -p' # complain if we get no arguments if [ $# -eq 0 ]; then echo "Syntax : cpdate file [suffix]" echo "(You must give at least one argument)" exit 1 fi if [ $# -gt 2 ]; then echo "Syntax : cpdate file [suffix]" echo "(No more the two arguments)" exit 1 fi if [ ! -r $1 ] ; then echo "Syntax : cpdate file [suffix]" echo "(The source file must exist; I can't find \"$1\")" exit 1 fi # use todays date if we just get a filename if [ $# -eq 1 ] ; then SOURCE=$1 # set the two versions of the date-string DATE="`$DATE_BINARY +'%Y%m%d'`" LONGDATE="`$DATE_BINARY +'%Y%m%dT%T'`" # make sure we aren't overwriting something if [ ! -r $SOURCE.$DATE ] ; then # use DATE $CP $SOURCE $SOURCE.$DATE elif [ -r $SOURCE.$DATE ] ; then # use LONGDATE # the DATE file exists, test if the LONDATE file exists... if [ ! -r $SOURCE.$LONGDATE ] ; then $CP $SOURCE $SOURCE.$LONGDATE else # generate an error message and exit echo "$SOURCE.$LONGDATE exists!" echo "(Trying again will almost certainly help)" exit 1 fi else exit 1 fi exit 0 fi # if we get more than one argument, use the second one as a suffix if [ $# -eq 2 ] ; then SOURCE=$1 NOTE=$2 LONGNOTE=$2_`$DATE_BINARY +'%Y%m%dT%T'` # once more, make sure we aren't clobbering something (see the # DATE sequence above for details on how this works) if [ ! -r $SOURCE.$NOTE ] ; then $CP $SOURCE $SOURCE.$NOTE elif [ -r $SOURCE.$NOTE ] ; then if [ ! -r $SOURCE.$LONGNOTE ] ; then $CP $SOURCE $SOURCE.$LONGNOTE else echo "$SOURCE.$LONGNOTE exists!" echo "(Trying again will almost certainly help)" exit 1 fi else exit 1 fi exit 0 fi