#!/usr/bin/php
<?php

function Usage()
{
	echo basename(__FILE__)." --from from_encoding --to to_encoding --output output_path file \n";
}

function main($fromEncoding, $toEncoding, $outputPath, $inputPath)
{
	$output = iconv($fromEncoding, "{$toEncoding}//IGNORE", file_get_contents($inputPath));
	if ($output == '') {
		file_put_contents($outputPath, mb_convert_encoding(file_get_contents($inputPath), $toEncoding));
	} else {
		file_put_contents($outputPath, $output );
	}
}

$opts = '';
$longOpts = array('to:', 'from:', 'output:');
$inputOptions = getopt($opts, $longOpts);

if (empty($inputOptions['to']) || empty($inputOptions['from']) || empty($inputOptions['output'])) {
	Usage();
	exit(1);
}

$leftArray = array_slice($argv, 7);
if (count($leftArray) === 0) {
	Usage();
	exit(1);
}

main($inputOptions['from'], $inputOptions['to'], $inputOptions['output'], $leftArray[0]);

?>

