#   PreProcessor 0.1
#   by Csaba Peterfalvi
#   peterfalvi.web.elte.hu
#   08-08-2014

#   Enables Inverse Search from YAP to WinEdt or TeXnicCenter with the General File Preprocessor 'preprocessor.pl'


@parts = split(/\\/,$0);
$temp=$0 . 'preprocessor_subs.pl';
$temp=~s/$parts[-2]\\$parts[-1]//;
require $temp;

$Editor = shift @ARGV;
$JobFile = pop @ARGV;
$CommentChar = pop @ARGV;
$Source = join(' ',@ARGV);
$JobFile=~s/\//\\/g;

@parts = split(/\\/,$JobFile);
$Path=$JobFile;
$Path=~s/\\$parts[-1]//;
$JobFile=$Path . '\preprocessor.pp';

open($in, '<', $JobFile) or goto NOPP;

@jobs=();
while(<$in>){
	chomp($_);
	if(m/^$CommentChar#WRITEPP: *"(.+)"/){
		$temp=subs($1,$Path);
		push(@jobs,"$temp");};}
close($in);

foreach $job(@jobs){
	@parts = split(/\\/,$job);
	$job=$parts[-1];
	$job=~s/(\.\w+)$//;
	$jobpp=$job . '_pp';
	$Source=~s/$jobpp/$job/;}

NOPP:
system($Editor,$Source);
