/***************************************************************************
 *   Copyright (C) 2007 by Vaivaswatha   *
 *   vaivaswatha@puttu.net   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/


#include <stdio.h>
#include <stdlib.h>

int main(argc, argv)
int argc;
char **argv;
{
	FILE *fp; int result, time; 
	void exit();
	
	if (!(valid (argc, argv)))
	{
		syntax ();
		exit (0);
	}
	
	sscanf (*(argv + 1), "%d", &time);
		
	fp = fopen (*(argv + 2), "r+b");
	if (fp == NULL)
	{
		fprintf(stderr, "Error opening file...\n");
		exit (1);
	}
	
	result = update_srt (fp, time);
	switch (result)
	{
		case 100: /* successful */
			break;	
		case 2: printf("Requested sync request is illegal...\n");
			break;
	}
	
	fclose (fp);
	exit (100);
}

int syntax ()
{
	fprintf (stderr, "\nsyncsub: The syntax for syncsub is:\nsyncsub time filename\n");
	
	fprintf (stderr, "\ntime (positive or negetive) must be in seconds\n");
	fprintf (stderr, "As yet, only .srt files supported.\n\n");
}

int valid (argc, argv)  /* not complete yet.... */
		int argc; char ** argv;
{
	int result = 1;
	
	if (argc <= 2 || argc > 3)
		result = 0;
			
	else
	{
		if (!(**(argv + 1) == '+' || **(argv + 1) == '-' || (**(argv + 1) >= '0' && **(argv + 1) <= '9'))) /* first argument must be a number -- checks only first digit..*/
			result = 0;
	}
	
	return result;
}
