#! /bin/sh
# bkp - create dated backup copies of files
# Copyright (C) 2001 Ed Cashin
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 

date=`date +%Y%m%d`
test=/usr/bin/test

findname () {
  name="$*.$date"
  $test ! -e "$name" && return
  i=1
  while $test -e "${name}_$i"; do
    i=`expr $i + 1`
  done
  name="${name}_$i"
}

dest=
if test "$1" = "-d"; then
  shift
  dest="$1"
  shift
fi

for f in "$@"; do
  if test "$dest"; then
    findname "$dest/`basename $f`"
  else
    findname "$f"
  fi
  printf "bkp \"%s\" --> \"%s\"\n" "$f" "$name"
  cp -i "$f" "$name"
done
