use IO::Handle;
STDOUT->autoflush(1);

our $dir = $0;

$dir =~ s/\\/\//g;
if($dir =~ /\//)
{
	$dir =~ s/^(.*)\/(.*?)$/$1/;
}
else
{
	$dir = cwd();
}

do "$dir/subs.pm";

do "$dir/config.pl" or die "ERROR: !! couldnt load $dir/config.pl";

my $version = "0.6";

my $minsize = 700 *1024; # bytes

my $pos = 0;
my $count = 1;
my $filesize = 0;


# ===============================================================
# main code
# ===============================================================

print ">>> encode.pl $version starting\n\n";


# ===============================================================
# remove any existing flv's from our directory;

opendir(DIR, "$flv_local_dir") or die "!!! ERROR: couldnt open dir: \"$flv_local_dir\" to read\n";
@dir = readdir(DIR);
close(DIR);

for(@dir)
{
	if($_ !~ /\.flv$/i)
	{
		next;
	}
	unlink("$flv_local_dir/$_");
}

# check if file exists

$ARGV[0] =~ s/\/\//\//ig;

if(! -f "$ARGV[0]")
{
	print "ERROR: encode.pl cannot see the file \"$ARGV[0]\"\nThis may be due to strange characters in the current path.";
	exit 0;
}

# ===============================================================
# start encoding

while()
{
	print "\n\n\nDBG POS = $pos\n";
	$out_file = "$flv_local_dir/"."$count"."default.flv";

	if($enc_exe =~ /mencoder(\.exe|)\s*$/i)
	{
		if($duration == 1)
		{
			# mencoder - no segments
			$cmd = " \"$enc_exe\" \"$ARGV[0]\" $enc_opts -o \"$out_file\" ";
		}
		else
		{
			# mencoder - segments
			$cmd = " \"$enc_exe\" \"$ARGV[0]\" -ss $pos -endpos $duration $enc_opts -o \"$out_file\" ";
		}
	}
	else
	{
		if($duration == 1)
		{
			# ffmpeg - no segments
			$cmd = "\"$enc_exe\" -i \"$ARGV[0]\" $enc_opts \"$out_file\"";
		}
		else
		{
			# ffmpeg
			$cmd = "\"$enc_exe\" -i \"$ARGV[0]\" $enc_opts -ss $pos -t $duration \"$out_file\"";
		}
	}
	
	print "DBG infile = $ARGV[0]\n";
	print "DBG outfile = $out_file\n";
	print "DBG command = $cmd\n";

	print "\n\n";
	system("$cmd");
	
	&check_jobs;
	$filesize = -s "$out_file";
	if($filesize < $minsize)
	{
		unlink("$out_file");
		print "\n\n>>> Out file \"$countdefault.flv\" is smaller than min vid size ausuming file ended, halting encoding and removing file.\n";
		exit 1;
	}
	$count++;

	$pos += $duration;
	
	if($use_flvtool eq "on")
	{
		print ">>> Adding meta info to segment with flvtool2\n";
		$cmd = "\"$flvtool_exe\" -U \"$out_file\"";
		system("$cmd");
		print ">>> Meta Info Added\n";
	}
	if($duration == 1)
	{
		exit 1;
	}
}

exit 1;

# ===============================================================
# subs
# ===============================================================

# check_jobs - if another job is waiting, exit this one.

sub check_jobs
{
	# check to see if another job is waiting
	
	opendir(DIR, "$work_dir") or die "ERROR: goto_next - couldnt open $work_dir\n";
	my @dir = readdir(DIR);
	closedir(DIR);
	
	my $count = 0;
	for(@dir)
	{
		if(/\.(bat|sh)$/i)
		{
			$count++;
		}
		
		if($count >= 2)
		{
			print "\n\n>>> found new job abandoning old one.\n\n";
			exit 1;
		}
	}
	
	if($count == 0)
	{
			print "\n\n>>> Jobs cleared, aborting current encode.\n\n";
			exit 1;
	}
	return 1;	
}