#!/bin/php
<?php

/*
  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/>.
*/

const N="\n";
const SNAME='vertrad.php';
const SVERS='0.0.1';

const RED="\33[1;31m";
const NORM="\033[0m";

$opts=[
	'files'=>[],
	'prompt'=>false
];

$help=
'SYNOPSIS

'.SNAME.' [options] <file[s]>

DESCRIPTION

This is '.SNAME.' v'.SVERS.', a CLI php script that ...

OPTIONS

-p, --prompt
 Prompt for editing on each suspect pair of msgid-msgstr.

-h, --help
 Show this help text and exit.

...

NOTES

...

EXIT VALUES

0: regular run
1: fatal error

DISCLAIMER AND LICENSE

This program comes with ABSOLUTELY NO WARRANTY; for details see the source.
This is free software, and you are welcome to redistribute it under certain
conditions; see <http://www.gnu.org/licenses/> for details.'.N;

for ($i=1; $i<$argc; $i++) {
	if ($argv[$i][0]=='-') {
		switch ($argv[$i]) {
			case '--makereadme':
				file_put_contents(__DIR__.'/README.md','```text'.N.$help.'```'.N);
				exit(0);
				break;
			case '-p':
			case '--prompt':
				$opts['prompt']=true;
				break;
			case '-h':
			case '--help':
				echo($help);
				exit(0);
				break;
			default:
				fwrite(STDERR,'Error: «'.$argv[$i].'» is not a valid option.'.N);
				exit(1);
		}
	} elseif (is_file($argv[$i]) && is_readable($argv[$i])) {
		$opts['files'][]=$argv[$i];
	} else {
		fwrite(STDERR,'Error: «'.$argv[$i].'» is not a readable file.'.N);
		exit(1);
	}
}
//print_r($opts); exit(0);

if (count($opts['files'])==0) {
	fwrite(STDERR,'Error: you have to specify at least one file.'.N);
	exit(1);
}

$ts=0;
foreach ($opts['files'] as $file) {
	$cont=@file($file,FILE_IGNORE_NEW_LINES);
	if ($cont!==false) {
		$s=0;
		$i=0;
		$c=count($cont);
		while ($i<$c) {
			if (preg_match('/^msgid "/u',$cont[$i])===1) {
				$msgid=$cont[$i];
				$i++;
				$msgstr=$cont[$i];
				if ($msgstr!='msgstr ""') {
					$show=false;
					if (preg_match_all('/\\\"/u',$msgid)!=preg_match_all('/\\\"/u',$msgstr)) $show=true;
					if (preg_match_all('/</u',$msgid)!=preg_match_all('/</u',$msgstr)) $show=true;
					if (preg_match_all('/>/u',$msgid)!=preg_match_all('/>/u',$msgstr)) $show=true;
					if (preg_match_all('/\'/u',$msgid)!=preg_match_all('/\'/u',$msgstr)) $show=true;
					if ($show) {
						$s++;
						echo('«'.$file.'»:'.$i.N.hil($msgid).N.hil($msgstr).N);
						if ($opts['prompt']) {
							$inp='+';
							while (!in_array($inp,['y','n','e','']))
								$inp=strtolower(prompt('Do you want to edit it? [(y)es/(N)o/(e)xit] '));
							if ($inp=='y')
								//system('nano +'.($i+1).' '.escapeshellarg($file).' > `tty`');
								system('kate -l '.($i+1).' '.escapeshellarg($file));
							elseif ($inp=='e')
								exit(0);
						}
						echo(N);
					}
				}
			}
			$i++;
		}
		//echo('«'.$file.'» total suspects: '.$s.N);
		$summ[]='«'.$file.'» total suspects: '.$s;
		$ts+=$s;
	} else {
		fwrite(STDERR,'Error: could not open «'.$file.'».'.N);
		exit(1);
	}
}

foreach ($summ as $row)
	echo($row.N);
echo('Grand total of suspects: '.$ts.N);

exit(0);

function hil($msg) {
	$msg=preg_replace('/\\\"/u',RED.'\\\"'.NORM,$msg);
	$msg=preg_replace('/</u',RED.'<'.NORM,$msg);
	$msg=preg_replace('/>/u',RED.'>'.NORM,$msg);
	$msg=preg_replace('/\'/u',RED.'\''.NORM,$msg);
	return($msg);
}

function prompt($prompt) {
	echo($prompt);
	$line=stream_get_line(STDIN, 1024, PHP_EOL);
	return(trim($line));
}

?>
