#!/usr/bin/php
<?php

define('N',"\n");

use function mysqli_real_escape_string as myesc;

function mexit($msg,$code) {
	echo($msg);
	exit($code);
}

$iniarr=@parse_ini_file(__DIR__.'/../../conf/mustard.ini')
	or mexit('Impossibile aprire il file di configurazione.'.N,1);
$link=mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket'])
	or mexit('Impossibile connettersi al database: '.mysqli_connect_error().' ['.mysqli_connect_errno().']'.N,1);
mysqli_set_charset($link,'utf8mb4');

$res=mysqli_query($link,'SELECT * FROM Languages')
	or mexit(mysqli_error($link),2);
while ($row=mysqli_fetch_assoc($res)) {
	$NameIt=ucfirst(locale_get_display_name($row['Code'],'it'));
	$NameEn=ucfirst(locale_get_display_name($row['Code'],'en'));
	$NameFr=ucfirst(locale_get_display_name($row['Code'],'fr'));
	$NameEs=ucfirst(locale_get_display_name($row['Code'],'es'));
	$NameOrig=ucfirst(locale_get_display_name($row['Code'],$row['Code']));
	$query='UPDATE Languages SET NameIT=\''.myesc($link,$NameIt).'\', NameEN=\''.myesc($link,$NameEn).'\', NameFR=\''.myesc($link,$NameFr).'\', NameES=\''.myesc($link,$NameEs).'\', NameOrig=\''.myesc($link,$NameOrig).'\' WHERE ID='.$row['ID'];
	echo($query.N);
	mysqli_query($link,$query)
		or mexit(mysqli_error($link).N,2);
}

mysqli_close($link);

exit(0);

?>
