#!/usr/bin/php -q
<?php
mb_internal_encoding("UTF-8");
set_time_limit(0);
ob_implicit_flush();
declare(ticks = 1);

function file_replace_line_number($file, $line_number, $text) {
	$handle = @fopen($file, "r");
	$thandle = @fopen($file."tmp", "w");
	if ($handle AND $thandle) {
		$a=0;
		while (($buffer = fgets($handle, 8192)) !== false) {
			$a++;
			if ($a==$line_number) {
				fwrite($thandle, $text."\n");
			}
			else {
				fwrite($thandle, $buffer);
			}
		}
		if (!feof($handle)) {
			echo "Erreur: fgets() a échoué\n";
		}
		fclose($handle);
		fclose($thandle);
		passthru("mv ".$file."tmp ".$file);
	}
}
function prepare_data($version) {
	passthru("chmod 755 /wprotect/DEBIAN/preinst");
	passthru("chmod +x /wprotect/DEBIAN/preinst");
	passthru("chmod 755 /wprotect/DEBIAN/postinst");
	passthru("chmod +x /wprotect/DEBIAN/postinst");
	passthru("chmod 755 /wprotect/DEBIAN/postrm");
	passthru("chmod +x /wprotect/DEBIAN/postrm");
	passthru("chmod 755 /wprotect/DEBIAN/prerm");
	passthru("chmod +x /wprotect/DEBIAN/prerm");
	passthru("mysqldump -u root -pphbmpd2wyh wprotect pages_default > /wprotect/etc/wprotect/default_pages.sql");
	passthru("rsync -vr --delete-after /var/www/ /wprotect/var/www/");
	passthru("rsync -vr --delete-after /opt/wprotect/ /wprotect/opt/wprotect/");
	passthru("rm /wprotect/var/www/admin/themes/default/card.tpl");
	passthru("rm /wprotect/var/www/admin/themes/default/card_en.tpl");
	passthru("rm -R /wprotect/var/www/portal1/firmware");
	passthru("rm -R /wprotect/opt/wprotect/firmware");
	passthru("rm /wprotect/opt/wprotect/poststart");
	passthru("rm /wprotect/var/www/portal1/speedtest.jpg");
	passthru("rm /wprotect/var/www/portal1/themes/default/index.tpl");
	passthru("rm /wprotect/var/www/portal1/themes/default/index_en.tpl");
	passthru("rm /wprotect/var/www/portal1/themes/default/already_connect.tpl");
	passthru("rm /wprotect/var/www/portal1/themes/default/already_connect_en.tpl");
	passthru("rm /wprotect/var/www/portal1/themes/default/conditions_en.tpl");
	passthru("rm /wprotect/var/www/portal1/themes/default/conditions.tpl");
	passthru("rm /wprotect/var/www/portal2/themes/default/index.tpl");
	passthru("rm /wprotect/var/www/portal2/themes/default/index_en.tpl");
	passthru("rm /wprotect/var/www/portal2/themes/default/already_connect.tpl");
	passthru("rm /wprotect/var/www/portal2/themes/default/already_connect_en.tpl");
	passthru("rm /wprotect/var/www/portal2/themes/default/conditions_en.tpl");
	passthru("rm /wprotect/var/www/portal2/themes/default/conditions.tpl");
	passthru("rm /wprotect/var/www/portal3/themes/default/index.tpl");
	passthru("rm /wprotect/var/www/portal3/themes/default/index_en.tpl");
	passthru("rm /wprotect/var/www/portal3/themes/default/already_connect.tpl");
	passthru("rm /wprotect/var/www/portal3/themes/default/already_connect_en.tpl");
	passthru("rm /wprotect/var/www/portal3/themes/default/conditions_en.tpl");
	passthru("rm /wprotect/var/www/portal3/themes/default/conditions.tpl");
	passthru("rm /wprotect/var/www/portal4/themes/default/index.tpl");
	passthru("rm /wprotect/var/www/portal4/themes/default/index_en.tpl");
	passthru("rm /wprotect/var/www/portal4/themes/default/already_connect.tpl");
	passthru("rm /wprotect/var/www/portal4/themes/default/already_connect_en.tpl");
	passthru("rm /wprotect/var/www/portal4/themes/default/conditions_en.tpl");
	passthru("rm /wprotect/var/www/portal4/themes/default/conditions.tpl");
	if ($version) {
		file_replace_line_number("/wprotect/DEBIAN/postinst", 3, "\$wprotect_version=\"".$version."\";");
		file_replace_line_number("/wprotect/DEBIAN/control", 2, "Version: ".$version);
	}
}

function push_git($description) {
	passthru("cd /wprotect; git add .; git commit -a -m \"".$description."\"; git push -u origin 2.0; ");
}

function compil_and_push_to_repo() {
	passthru("chmod 755 /wprotect/DEBIAN/postinst");
	passthru("cd /; mv /wprotect/.git /tmp/tmpgit; dpkg-deb -z8 -Z gzip -b /wprotect /wprotect.deb; mv /tmp/tmpgit /wprotect/.git");
	passthru("rsync -arv /wprotect.deb root@connectoo.fr:/var/www/virtual/maj.connectoo.fr/htdocs/debian/incoming/");
	passthru("ssh root@connectoo.fr \"cd /var/www/virtual/maj.connectoo.fr/htdocs/debian && reprepro remove stretch wprotect && reprepro includedeb stretch incoming/wprotect.deb\"");
}

foreach ($argv AS $k=>$v) {
	if ($v=="-d" AND isset($argv[($k+1)])) {
		$description=$argv[($k+1)];
	}
	if ($v=="-c" AND isset($argv[($k+1)])) {
		$version_to_compil=$argv[($k+1)];
	}
}

if (isset($version_to_compil)) {
	prepare_data($version_to_compil);
}
else {
	prepare_data(false);
}

if (isset($description)) {
	echo "PUSH to GIT...\n";
	push_git($description);
	echo "PUSH to GIT terminated !\n";
}
if (isset($description) AND isset($version_to_compil)) {
	echo "Compil and push to repo\n";
	compil_and_push_to_repo();
	echo "End !\n";
}

?>



