#!/usr/bin/perl
#
# It's my first script in Perl, so be lenient.
#
# This script works only for TV shows and their subtitles !
# When you want to see your favorite tv show, with its sub, you launch the video
# with your favorite video player, and you add the path of the subtitles.
# Many video players, like mplayer, import automatically the subtitles if they 
# have the same name. This script renames the subtitles files (.srt) of a directory
# exactly like the corresponding videos (.avi).
# You won't need to specify the path of the subtitles to your video player anymore.
# 
# Example:
# Video: my_video.s04e38.something.dot.something_else.avi
# Subtitle file: video.438.srt
# After running the script, the subtitle will be named:
# my_video.s04e38.something.dot.something_else.srt
#
# The formats supported by the script:
# - sAAeBB or SAAEBB with AA the season and BB the episode.
# - ABB with A the season and BB the episode.
# - AxBB with A the season and BB the episode.
#
# If the script is launched without any arguments, then the videos and subtitles 
# directories will be set to the current directory.
#
# Options:
#       
#       -h print command list
#
# -d (-dir) <videos directory> 
# Specify the directory where the videos are located.
# If this option is not set, the video directory will be the current directory.
#
# -s (-sub) <subtitles files directory>
# Specify the directory of the subtitles files.
# If this option is not set, the subtitles directory will be the same directory as
# the videos directory. Which means that if the videos directory is not set, it will be
# the current directory.
# 
# -p (--preview)  <without arguments>
# Preview mode. If this options is set, the subtiles new names will be display on the
# screen without being renamed.
#
# I hope this script will be as useful as it's to me.
# 
# Enjoy your tv shows ;-)
#

use Cwd;
#use Getopt::Std;
use Getopt::Long;

my $preview = 0;
my $help = 0;
GetOptions( "dir=s" => \$dir, "sub=s" => \$sub, "preview!" => \$preview, "help!" =>\$help ) or printOptions();

if ($help == 1){
  print "Command list\n";
  printOptions();
}


if ($preview == 1) {print "****** PREVIEW MODE ******\n";}
else {print "****** RENAME MODE *******\n";}

chomp ($currentDir = getcwd());
my $videoDir = $currentDir;
my $subDir = $currentDir;

if (defined $dir){
  $videoDir = $dir;
}
print "dir: $videoDir\n";

if (defined $sub) {
        $subDir = $sub;
}
else {
        $subDir = $videoDir;
}

opendir(DIR, $videoDir) || die "can't opendir $videoDir: $!";
while (my $path = readdir(DIR)) { 
        push(@videosList, $path) if $path =~ m/\.avi$/;
} 
closedir(DIR);

if (!@videosList){
        print"\n No videos found ! check your path !\n";
        exit();
}

$temp = getcwd();
opendir(DIR, $subDir) || die "can't opendir $subDir: $!";
while (my $path = readdir(DIR)) {
        push(@subsList, $path) if $path =~ m/\.srt$/;
}
closedir(DIR);

if (!@subsList){
        print "\n No subtitles found ! check your path !\n";
        exit();
}

if ($videoDir ne $currentDir){
        chdir ($videoDir) || die "Can't cd to spool: $!\n";
}

foreach $f (@videosList) {
                
        ################
        # VIDEOS FILES #
        ################
        
        my @result = getSeasonEpisod($f);
        $season = @result[0];
        $episod = @result[1];

        foreach $s (@subsList) {
                
                if ($videoDir ne $subDir) {
                        chdir ($currentDir) || die "Can't cd to spool: $!\n";
                        chdir ($subDir) || die "Can't cd to spool: $!\n";
                }

                ####################
                # SUBTITLES FILES  #
                ####################
        
                my @result = getSeasonEpisod($s);
                $seasonSub = @result[0];
                $episodSub = @result[1];        

                if ($season == $seasonSub and $episod == $episodSub){
                        my ($vidName) = $f =~ m/(.*)\.avi$/;
                        print "Rename: $s --> $vidName.srt";
                        print "\n";
                        if ($preview != 1){
                                rename($s,"$vidName.srt") || die "Cannot rename $s: $!";
                        }
                }
        }
}

# get season and episode
sub getSeasonEpisod {
        my $season = 0;
        my $episod = 0;

        # format ABB
        if(@_[0] =~ /(\d)(\d\d)/){
                $season = $1;
                $episod = $2;
        }

        # format sAAeBB
        elsif (@_[0] =~ /[sS](\d\d)[eE](\d\d)/){
                $season = $1;
                $episod = $2;
        }

        # format AxBB
        elsif (@_[0] =~ /(\d)[xX](\d\d)/){
                $season = $1;
                $episod = $2;
        }
        
        else {  
                print "Video format Not Match ! \n";
        }
        
        return ($season,$episod);
}

# print options
sub printOptions(){
  print "-h print command list\n";
  print "-d (-dir) <videos directory>\n";
  print "-s (-sub) <subtitles files directory>\n";
  print "-p (--preview)  <without arguments>\n";
  exit();
}