#!/bin/sh

# Copyright (C) 2009 Sergio García-Cuevas González.
# 
# 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 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/>.



# fixsrt
#
# Adjusts the times of SRT subtitles applying the following transformation:
#
#  t -> SCALE * (t + OFFSET)




OFFSET="$1"
SCALE="$2"


# Default values
[ "x$OFFSET" = x ] && OFFSET=0
[ "x$SCALE" = x ] && SCALE=1


awk 'function time_to_seconds(timespec)
     {
       negative = 0
       if (timespec ~ /^-/)
       {
         timespec = substr (timespec, 2)
         negative = 1
       }

       split (timespec, timevec, ":|,")
       hours = timevec[1]
       minutes = timevec[2]
       seconds = timevec[3]
       milliseconds = timevec[4]

       seconds = hours * 3600 + minutes * 60 + seconds + milliseconds * 0.001
       if (negative) seconds = -seconds

       return seconds
     }



     function seconds_to_time(seconds)
     {
       negative = 0
       if (seconds < 0)
       {
         negative = 1
         seconds = -seconds
       }

       milliseconds = 1000 * seconds % 1000
       hours = int(seconds / 3600)
       seconds = seconds % 3600
       minutes = int(seconds / 60)
       seconds = seconds % 60
       seconds = int(seconds)

       time = sprintf("%02d:%02d:%02d,%03d",
                      hours, minutes, seconds, milliseconds)
       if (negative) time = "-" time

       return time
     }



     BEGIN { OFFSET = "'"$OFFSET"'"
             SCALE  = "'"$SCALE"'"

             # We also accept OFFSET as a time specification code.
             if (OFFSET ~ /:/)
               OFFSET = time_to_seconds(OFFSET)
             subtitle_number_line = 1
             time_spec_line = 0 }

     # The first line of a subtitle.
     subtitle_number_line && /^[0-9]/ { print
                                        # The next line will be the
                                        # time spec line.
                                        time_spec_line = 1
                                        subtitle_number_line = 0
                                        next }

     # The second line of a subtitle.  Here we fix the times.
     time_spec_line { start = time_to_seconds($1)
                      end = time_to_seconds($3) 

                      start = SCALE * (start + OFFSET)
                      end = SCALE * (end + OFFSET)

                      start = seconds_to_time(start)
                      end = seconds_to_time(end)

                      printf("%s --> %s\r\n", start, end)

                      # The next line will be a regular line.
                      time_spec_line = 0
                      next }

    # The empty line separates different subtitles.  The next line
    # will be a subtitle number line.
    /^\r?$/ { subtitle_number_line = 1 }

    # We just print the regular lines.
    { print }'
