unrar(archive_file)
                        else:
                            # TODO: implement unzip
                            logger.info("trying to unzip")

            logger.info("Checking for video files...")
            # walk dir, find any video files
            for root, dirs, files in os.walk(file_path):
                for vfilename in filter(is_video_file, files):
                    logger.info("Found file: %s" % (vfilename))
                    files_to_copy.append(os.path.join(root, vfilename))

        #  use creation logic from backup_tv.py to mv and rename episode
        s = "0" + str(c[1]) if (int(c[1]) < 10) else str(c[1])
        season_dir = "season_" + s
        series_name = gomXBMCTools.normaliseTVShowName(str(c[0]))

        if not os.path.isdir(target + series_name):
            logger.info("\tDirectory: %s does not exist, creating..." % (series_name))
            os.mkdir(target + series_name)

        if not os.path.isdir(target + series_name + "/" + season_dir):
            logger.info("\tDirectory: %s does not exist, creating..." % (season_dir))
            os.mkdir(target + series_name + "/" + season_dir)

        rmdir = True
        for f in files_to_copy:
            e = "e0" + str(c[2]) if (int(c[2]) < 10) else "e" + str(c[2])
            # TODO: this is broken
            if c[2] == -1:
                e = gomXBMCTools.getEpisodeNumFromFilename(f, s)
def copyShow(show):
    """
        copyShow(show): copies single show from xbmc library to destination
    """
    print ""
    print "******************* COPYING SHOW: ",show," ******************* "
    prev_path = ''
    show_dirs = []
    for f in getShow(show):
        # get nice str repr for season/eps
        s = "0" + f['season'] if(int(f['season'])<10) else f['season']
        e = "e0" + f['episode'] if(int(f['episode'])<10) else "e" + f['episode']

        if(prev_path == f['path']):
            if args.verbose:
                print "\t\t\tduplicate: ", f['path']
            continue

        # need to handle joined episodes like show_s01e02e03.avi
        is_multi_episode = re.compile("e[0-9][0-9]e[0-9][0-9]*")
        eps = is_multi_episode.findall(f['path'])
        if(len(eps) > 0):
            e_count = re.compile("e[0-9][0-9]")
            eps = e_count.findall(f['path'])
            prev_path = f['path']
            for i in range(len(eps)-1):
                epno = int(f['episode']) + i + 1
                e += "e0" + str(epno) if(epno < 10) else "e" + str(epno)

        # build up the filename we are copying to
        season_dir = "season_" + s
        series_name = gomXBMCTools.normaliseTVShowName(f['showname'])

        fileName, fileExtension = os.path.splitext(f['path'])
        ftarget = args.target + series_name  + "/" + season_dir + "/" + series_name + "_s"+ s + e + fileExtension
        nfo = args.target + series_name  + "/" + season_dir + "/" + series_name + "_s"+ s + e + ".nfo"

        # create dirs if they don't exist 
        createDirIfNotExist(args.target + series_name)
        createDirIfNotExist(args.target + series_name + os.path.sep + season_dir)

        if args.test:
            print "\t\tCreating empty ", f['path'], " at ", ftarget
            makeFile(ftarget)
        else:
            if(os.path.exists(ftarget)):
               print "\t\tSkipping file ", ftarget, " as it already exists..."
            else:
                print "\t\tCopying ", f['path'], " to ", ftarget
                shutil.copy2(f['path'], ftarget)

        if args.metadata:
            # Copy episode.nfo
            if os.path.isfile(fileName+".nfo"):
                #print "\tCopying ", fileName+".nfo", " to ", nfo
                shutil.copy2(fileName+".nfo", nfo)

            # Copy any tbn/nfo/fanart etc
            show_root = getShowRootPath(f['path'])
            if ((show_root not in show_dirs) and show_root):
                print "\t\tDiscovered show root dir: ", show_root
                show_dirs.append(show_root)
                # copy any .nfo/tbn/etc files
                try:
                    tbns = [f for f in os.listdir(show_root) if re.match(r'season.*.tbn', f)]
                except:
                    tbns = []

                tbns += xbmc_metadata
                tbns.sort()
                for file in tbns:
                    if os.path.exists(show_root + os.path.sep + str(file)):
                        print "\t\t\tFound ", file, ", copying to ", args.target, series_name
                        shutil.copy2(show_root + os.path.sep + file, args.target + series_name)
                print ""
                            unrar(archive_file)
                        else:
                            # TODO: implement unzip
                            logger.info("trying to unzip")

            logger.info("Checking for video files...")
            # walk dir, find any video files
            for root, dirs, files in os.walk(file_path):
                for vfilename in filter(is_video_file, files):
                    logger.info("Found file: %s" % (vfilename))
                    files_to_copy.append(os.path.join(root, vfilename))

        #  use creation logic from backup_tv.py to mv and rename episode
        s = "0" + str(c[1]) if (int(c[1]) < 10) else str(c[1])
        season_dir = "season_" + s
        series_name = gomXBMCTools.normaliseTVShowName(str(c[0]))

        if not os.path.isdir(target + series_name):
            logger.info("\tDirectory: %s does not exist, creating..." %
                        (series_name))
            os.mkdir(target + series_name)

        if not os.path.isdir(target + series_name + "/" + season_dir):
            logger.info("\tDirectory: %s does not exist, creating..." %
                        (season_dir))
            os.mkdir(target + series_name + "/" + season_dir)

        rmdir = True
        for f in files_to_copy:
            e = "e0" + str(c[2]) if (int(c[2]) < 10) else "e" + str(c[2])
            # TODO: this is broken