#!/bin/sh -e
# Add the modification date in front of the filename
test $# -eq 0 && echo "predate [-s] [newname] <files...>" && exit 2
IFS='\n'
test "$1" = -s && short=true && shift
# Whether to change the filename suffix after the date
test ! -f "$1" && rename=true && name=$1 && shift || rename=false

for file
do mv --verbose --interactive "$file" \
	"$(latest "$file" | head -2 | tail -1 |
			cut -c$(test "$short" && echo "3,4,6,7,9,10" || echo "-10"))$(
		if $rename
		then echo "$(test "$name" && echo "_$name").${file##*.}"
		else echo "_$file" | sed 's/^_2\?\([0-9]\{2,3\}\)-\([0-9]\{2\}\)\(-[0-9]\{2\}\)\?_\?/_/'
		fi)"
done
# stat --format %y "$file" | cut -d' ' -f1