#!/bin/sh
#
# 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, 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 GNU CC; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Copyright (C) 2002-2003 Vino Fernando Crescini
#
# Vino Fernando Crescini  <jcrescin@cit.uws.edu.au>
# Started: 11 Jun 2001
# Updated: 04 Mar 2004

counter=1
charlen=5
tmpdir="./.tmp_$$_"
args="${*}"
progname="rename_files"
progver="1.0"

unset lowcase
unset prefix
unset recursive
unset verbose

while [ ${#} -ge 1 ]; do
  if [ "${1}" = "-h" ]; then
    echo "usage:       ${progname} [options]"
    echo "options:"
    echo "  -h         print this usage and exit"
    echo "  -r         recursively go into subdirectories"
    echo "  -l         force file extensions to lowercase"
    echo "  -p prefix  prepend \"prefix\" for each file"
    echo "  -v         verbose output"
    echo "  -V         print version and exit"
    echo ""
    exit 0
  elif [ "${1}" = "-l" ]; then
    lowcase="1"
  elif [ "${1}" = "-p" ]; then
    if [ -n "${2}" ]; then
      prefix="${2}"
      shift 1
    fi
  elif [ "${1}" = "-r" ]; then
    recursive="1"
  elif [ "${1}" = "-v" ]; then
    verbose="1"
  elif [ "${1}" = "-V" ]; then
    echo "${progname} ${progver}"
    exit 0;
  fi
  shift 1
done

mkdir "${tmpdir}"

for tmpfile in *
do
  if [ -n "${recursive}" ] && [ -d "${tmpfile}" ]; then
    cd "${tmpfile}"
    [ -n "${verbose}" ] && echo "entering `pwd`"
    "${0}" ${args}
    [ -n "${verbose}" ] && echo "exiting `pwd`"
    cd ..
  fi

  # get extension
  extension=".${tmpfile##*.}"

  if [ "${extension}" = ".${tmpfile}" ]; then
    unset extension
  fi

  # to lower case
  if [ -n "${lowcase}" ]; then
    extension="`echo ${extension} | tr [A-Z] [a-z]`"
  fi

  # pad the name with zeroes
  name="${counter}"
  while [ `echo -n "${name}" | wc -c` -lt "${charlen}" ]
  do
    name="0${name}"
  done

  # prepend prefix
  fullname="${prefix}${name}${extension}"

  # now rename
  [ -n "${verbose}" ] && echo "renaming ${tmpfile} to ${fullname}"
  mv -- "${tmpfile}" "${tmpdir}/${fullname}"

  counter=$[${counter}+1]
done

# now move them back
mv -- "${tmpdir}"/* .
rmdir "${tmpdir}"

exit 0