#!/bin/bash

#
# This script simply renames files or directories to <name>-<date>[<num>]
#
# Copyright (c) 1996-2002 SuSE Linux AG, Nuernberg, Germany.
#
# Contributor Bernard Lang
#
# please send bugfixes or comments to
# https://github.com/openSUSE/aaa_base/issues
#
# usage - tell user to use program
#
# Trick : put a nonexistant file name as first argument if you are worried
# about the possibility of files called -h or --help
#

usage() {
    echo usage: "$0" file\|dir [file\|dir ...]
}

if [ $# -eq 0 ] ; then
    usage
    exit
fi

if [ "$1" = "-h" -o "$1" = "--help" ] ; then
    echo 'Renames file or directory <name> given in argument list to <name>-<date>[<num>]'
    echo 'adding when necessary a suffix number <num> to avoid already used names.'
    usage
    exit
fi

DATESTRING=`date +"%Y%m%d"`

for i in "$@" ; do
    i=${i%%/}
    if [ -e "$i" ] ; then
        NEWNAME=$i-$DATESTRING
        NUMBER=0
        while [ -e "$NEWNAME" ] ; do
            NEWNAME=$i-$DATESTRING-$NUMBER
            let NUMBER=$NUMBER+1
        done
        echo moving "$i" to "$NEWNAME"
	if [ "${i:0:1}" = "-" ] ; then
	    i="./$i"
	    NEWNAME="./$NEWNAME"
	fi
        mv "$i" "$NEWNAME"
    else
        echo "$i" does not exist.
    fi
done

