#!/bin/sh

arg0=`basename $0`

USAGE="Usage: $arg0 [-h] [-f date_format] [-o chown_args] [-m chmod_args] full_path_to_file ..."

usage()
{
  echo "$USAGE" >&2
  exit 1
}

help()
{
  echo "$USAGE"
  echo "Options:"
  echo "	-h	Display this help"
  echo "	-f	Date format (as per date command) (default:$date_fmt)"
  echo "	-o	Args to chown (default: dont chown)"
  echo "	-m	Args to chmod (default: dont chmod)"
  echo "	full_path_to_file	location of original file"
  exit
}

date_fmt="%Y%m%d"
chown_arg=""
chmod_arg=""
DEBUG=""

while getopts "f:o:m:hD" opt
do
  case "$opt" in
    f)	date_fmt=$OPTARG
	;;
    o)	chown_arg=$OPTARG
	;;
    m)	chmod_arg=$OPTARG
	;;
    D)	DEBUG="echo"
	;;
    h)	help
	;;
    *)	usage
	;;
  esac
done

let OPTIND=OPTIND-1
shift $OPTIND

for file
 do
  test -f "$file" || continue
  if tag=`date -r "$file" +"$date_fmt"`
  then
    : Good
  else
    continue
  fi
  copy="$file-$tag"
  test -f "$copy" && continue	# Already been done
  $DEBUG cp -p "$file" "$copy"
  test -n "$chmod_arg" && $DEBUG chmod "$chmod_arg" "$copy"
  test -n "$chown_arg" && $DEBUG chown "$chown_arg" "$copy"
 done
  
  
