#!/bin/bash

# Move/copy one or more files/dirs to date-tagged names,
# e.g., foo.txt --> foo.txt-2012.03.29
# the date part is the date of the last file modification.
# ... online at http://www.jjj.de/
# Author: Joerg Arndt ( arndt (AT) jjj.de )

# version: 2023-February-06 (15:39)

#THIS=$(basename $0);
THIS=${0##*/} # basename
function usage
{
cat <<EOF
  usage: $THIS  [OPTION(s)] FILE1 [ FILE2 FILE3 ... ]
  where files may also be directories

   move/copy one or more files/dirs to date-tagged names:
    e.g. foo.txt --> foo.txt-2012.03.29
    where the date part is the date of the last file modification

  OPTION(s) must precede any other arguments.
   "-q" Quiet: supress output (option is unset if "-n" is also given).
   "-n" sets dry-run mode (do Nothing)
   "-t" append date and Time,
        e.g. foo.txt --> foo.txt-2012.03.29-12:39:32
   "-w" noW: use current date/time
   "-c" Copy file (instead of moving it)
   "-e <.ext>" rename file keeping the Extension .ext at end,
        e.g.,  foo.txt --> foo-2012.03.29.txt

   BUG: last character of (directory) name must not be a '/'

$THIS is online: at http://www.jjj.de/
EOF
#
#   "-d <dir>" move/copy file to Directory
#   "-z" compress file after renaming (using gZip)
#        NOTE: -z presently doesn't loop until a uniq name is found
}



if [ "$1" = "--help" ]; then  usage; exit 0;   fi
if [ "$1" = "-help" ];  then  usage; exit 0;   fi

if [ -z "$*" ] ; then  usage; exit 1;  fi

DRYRUN=
COPY=
#TODIR=
EXT=
TIME=
QUIET=
#COMPRESS=
NOW=
#while getopts :ctnwqhz\?de: OPT; do
while getopts :ctnwqh\?e: OPT; do
    case $OPT in
        h|+h|\?|+\?)
            usage
            exit 0;
            ;;
        n|+n)
            DRYRUN=1;
            ;;
        c|+c)
            COPY=1;
            ;;
#        d|+d)
#            TODIR=$OPTARG;
#            ;;
        e|+e)
            EXT=$OPTARG;
            ;;
        t|+t)
            TIME=1;
            ;;
        q|+q)
            QUIET=1;
            ;;
#        z|+z)
#            COMPRESS=1;
#            ;;
        w|+w)
            NOW=1;
            ;;
        *)
#            echo "usage: ${0##*/} [+- ARG] [+-ct] [--] ARGS..."
            usage;
            exit 1;
    esac
done
shift $[ OPTIND - 1 ]

#echo "DRYRUN=$DRYRUN  COPY=$COPY  TIME=$TIME"
#exit;

if [ -n "DRYRUN" ]; then  QUIET=""; fi

#if [ -n "$TODIR" ]; then
#    test -d $TODIR -a -w $TODIR || \
#        { echo "$TODIR must be a writable directory."; exit 1; };
#    TODIR=$TODIR'/';
#fi


for f in $* ; do

#    f=${f%%/} #

    if [ ! -e $f ]; then
        echo "$f: no such file or directory"
        continue; ## we might want a nonzero return value
    fi


    if [ -n "$TIME" ]; then
        FORMAT='%Y.%m.%d-%H:%M:%S'; ## date and time
    else
        FORMAT='%Y.%m.%d';  ## just date
    fi

    if [ -n "$NOW" ]; then
        DATE=$(date +$FORMAT); ## date = now
    else
        DATE=$(date -r $f +$FORMAT); ## date = file modification time
    fi


    if [ -n "$EXT" ]; then
        NEW=${f%${EXT}}-$DATE$EXT;  ## foo.txt --> foo-2012.03.29.txt
    else
        NEW=$f-$DATE;  ## foo.txt --> foo.txt-2012.03.29
    fi

    ## $NEW already there, skip it if option -t was given
    if [ -e $NEW ]; then
        if [ -n "$TIME" ]; then   continue;  fi

        ## loop until a unused filename is found:
        NUM=1;
        while [ -e $NEW ] ; do
            if [ -n "$EXT" ]; then
                if [ -z "$QUIET" ]; then  echo "$NEW already exists";  fi
                NEW=${f%${EXT}}-$DATE--$NUM$EXT;
            else
                NEW=$f-$DATE--$NUM;
            fi
            let NUM=$NUM+1
        done
    fi

    if [ -z "$QUIET" ]; then  echo "$f  -->  $NEW";  fi

    if [ -n "$DRYRUN" ]; then  continue;  fi  ## dry-run: just pretend actions

    if [ -n "$COPY" ]; then
        cp -a $f $NEW
    else
        mv $f $NEW
    fi

#    ## FIXME: need to find unused name.gz / name.tar.gz
#    if [ -n "$COMPRESS" ]; then
#    	if [ -d "$NEW" ]; then
#                ZNEW=$DNEW.tar.gz
#		tar cfz $ZNEW $DNEW
#                rm -rf $DNEW
#	else
#                ZNEW=$DNEW.gz
#		gzip -9 $DNEW
#	fi
#    	if [ -z "$QUIET" ]; then  echo "  -->  $ZNEW";  fi
#    fi

done


exit 0;
#######################################
