#!/usr/bin/perl
#
# Copyright (c) 2009 Mathieu Roy <yeupou--gnu.org>
# http://yeupou.coleumes.org
#
#   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 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, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
#   USA

use strict;
use Getopt::Long;
use Subtitles;
use File::Copy;

# Get command line args
my $file;
my $time;
my $getopt;

eval {
    $getopt = GetOptions("file=s" => \$file,
			 "time=s" => \$time);
};

# Create object
my $sub = Subtitles->new();


# Always start fresh with the backup file if existing
copy($file.".backup", $file) if -e $file.".backup";

# Make a backup if no one yet, to avoid working on a messy basis
copy($file, $file.".backup") unless -e $file.".backup";

# Rename the current working copy
rename($file, $file.".orig");

# Open it
open F, $file.".orig" or die "Cannot read:$!";
die "Cannot load:$@\n" unless $sub-> load(\*F);
close F;

# Time shifting according to command line args
$sub-> shift($time);

# Save with regular filename
open F, "> $file" or die "Cannot write:$!\n";
$sub-> save( \*F);
close F;

# Bla bla
print "Sub is ", time2str( $sub-> length),"\n";

exit;
