#!/usr/bin/python
#
#NAME:
#  rename_tv_folders_for_kodi.py
#
#PURPOSE:
#  To go through all of the files in a Season# directory, find 
#  episodes, and rename them so that they have the proper naming 
#  convention to be found by Kodi. The convention is as follows
#
#  rootdir = "TV_SHOWS"
#  subdir1 = "SEINFELD"
#  subdir2 = "SEASON[N]"
#  subdir2 = "S[N]E[M] [Episode Name].ext"
#
#  where N and M should be a two digit number with 0 padding on 
#  left. 
#
#EXAMPLES:
#
import os
import re
import sys
import pdb
rootdir = './'
TVShowName = 'Curb_Your_Enthusiasm'

for root, directories, filenames in os.walk(rootdir):

  #Scan through the subdirectories of the rootdir 
  for directory in directories:

    # Check to see if the directory stucture is right to represent a season
    print '----------------------------------------------------------------'
    print 'DIRECTORY: ' + directory 
    SeasonNumberRe = re.search('\d\d', directory)
    # Ask to skip the directory
    print 'Press 1 to enter directory; any other number to skip...'
    Choice = int(raw_input())
  
    # YES: The season number is there
    if ((SeasonNumberRe != None) and (Choice == 1)):
      SeasonNumber = directory[SeasonNumberRe.start(): SeasonNumberRe.end()]
      print "Season Number : " + str(SeasonNumber) + '\n'

      # Now go through each directory and look for the jpg
      for root2, directories2, filenames2 in os.walk(os.path.join(root,directory)):
        for filename2 in filenames2:

          # First look for two digits, then look for one digit to start the filename  
          EpisodeNumberRe = re.search('\d\d', filename2)
          if (EpisodeNumberRe == None):
            EpisodeNumberRe = re.search('\d', filename2)

          # YES: we have a valid episode number
          if (EpisodeNumberRe != None):
            EpisodeNumber = filename2[EpisodeNumberRe.start():EpisodeNumberRe.end()]
            print "  " + filename2
            print "  Episode Number : " + str(EpisodeNumber) + '\n'
            # Create the new episode name
            newfilename2 = TVShowName + '_' + \
                'S' + ("%02d" % int(SeasonNumber))  + \
                'E' + ("%02d" % int(EpisodeNumber)) + \
                filename2[EpisodeNumberRe.end():]
            print '  Rename to : ' + newfilename2 + '?'
            print '  Press 1 to rename: any other number to skip...'
            Choice = int(raw_input())
            if (Choice == 1):
              oldpath = os.path.join(rootdir, directory, filename2)
              newpath = os.path.join(rootdir, directory, newfilename2)
              print ' Renaming ' + oldpath + ' to ' + newpath
              os.rename(oldpath, newpath)
            else :
              print '  Skipping...'

          # NO: this is probably an extra
          else:
            print "  " + filename2
            print "  " + "No episode number found\n"
            
    # NO: the season number is not there
    else:
      print 'No matching number for this directory...moving on'      
