#! /usr/bin/python
#  -*- coding: utf-8 -*-
# Requires zenity
# tip - run: $ sudo apt-get install zenity

"""
Copyright (C) 2009-2010 Thiago Bellini<thibellini@gmail.com>

MassFileRename 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 3 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.

"""

import os
import re
import sys

pattern = ''
files = []

def makeNewName(oldname, newname, number):
	thename = ''
	suffix = ''
	# This will maintain the extension ex. ".jpg" and turn it lowercase
	if oldname.find('.') != -1:
		oldnameList = oldname.split('.')
		suffix = oldnameList[len(oldnameList)-1].lower()
	
	if suffix != '':
		thename = newname + '_' + number + '.' + suffix
	else:
		thename = newname + '_' + number
	
	return thename


pattern = os.popen('zenity --title "fileRenamer" --entry --text "Enter the Pattern:" --width=320').read().split('\n')[0]
if pattern == '':
	sys.exit(1)

if pattern.find('/') != -1:
	sys.exit(1)

for i in sys.argv[1:]:
	files.append(i)

if files == []:
	sys.exit(1)

number = 1
lenFiles = len(str(len(files)))
if lenFiles == 1:
	lenFiles = 2
for filee in files:
	if os.path.isdir(filee):
		pass
	elif os.path.islink(filee):
		pass
	elif os.path.ismount(filee):
		pass
	else:
		oldname = os.path.split(filee)[1]
		newname = makeNewName(oldname, pattern, str(number).zfill(lenFiles))
		newfile = os.path.join(os.path.split(filee)[0],newname)
		if filee != newfile:
			while os.path.isfile(newfile):
				number = number + 1
				newname = makeNewName(oldname, pattern, str(number).zfill(lenFiles))
				newfile = os.path.join(os.path.split(filee)[0],newname)
				if filee == newfile:
					break
		os.rename(filee,newfile)
		number = number + 1

