#! /usr/bin/awk -f

# Prepare an mpsub file out of multiple .idx vobsubs.

# Copyright 2007 Alexandre Oliva <lxoliva@fsfla.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 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, a copy can be downloaded from
# http://www.gnu.org/copyleft/gpl.html, or by writing to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.


# Usage: for f in *.idx; do echo partlen $(len-of $f); cat $f; done |
#        idxlist2mpsub

# I couldn't quite figure out what the first timestamp of each .idx
# stands for (it appears to be somewhat random), so it's simply
# ignored.  Also, there's no indication of the extent of time in which
# a subtitle must be displayed, so it's assumed to extend up to the
# next subtitle, or to the end of the part.  len-of must indicate the
# exact length of the part.


# partlen 5
# timestamp: 01:50:41:123, filepos: ?????????
# timestamp: 00:00:01:500, filepos: ?????????
# timestamp: 00:00:04:000, filepos: ?????????
# partlen 3
# timestamp: 00:05:14:567, filepos: ?????????
# partlen 1
# partlen 2
# timestamp: 00:08:14:111, filepos: ?????????

# =>

# FORMAT=TIME
#
# 0 1.500
# ?
# 
# 0 2.500
# ?
#
# 0 1
# ?
#
# 0 3
# ?
#
# 1 2
# ?
#
BEGIN {
    print "FORMAT=TIME";
    print "";
    firstinpart = 1;
    leadtime = 0;
}
/^timestamp: ..:..:..:..., / && !firstinpart {
    sub (/,/, "");
    split ($2, times, ":");
    nexttime = times[1] * 3600 + times[2] * 60 + times[3] + times[4] / 1000;
    print "not first in part, leadtime " leadtime " => 0, prevtime " prevtime " nexttime " nexttime > "/dev/stderr";
    print leadtime, nexttime - prevtime - 0.1;
    print "?";
    print "";
    prevtime = nexttime;
    leadtime = 0.1;
}
/^timestamp: ..:..:..:..., / && firstinpart {
    print "first in part, leadtime " leadtime " parttime " parttime > "/dev/stderr";
    firstinpart = 0;
    prevtime = 0;
}
/^parttime / {
    endofpart = 1;
}
END {
    endofpart = 1;
}
endofpart && !firstinpart {
    print "end of non-empty part, leadtime " leadtime " >= 0, prevtime " prevtime " nexttime " parttime > "/dev/stderr";
    nexttime = parttime;
    print leadtime, nexttime - prevtime;
    print "?";
    print "";
    prevtime = nexttime;
    leadtime = 0;
    endofpart = 0;
}
endofpart && firstinpart {
    print "end of empty part, parttime " parttime ", leadtime " leadtime " => " leadtime + parttime > "/dev/stderr";
    leadtime += parttime;
    endofpart = 0;
}
/^parttime / {
    print "start of part, parttime " $2 > "/dev/stderr";
    firstinpart = 1;
    parttime = $2;
}
