#!/usr/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/>.
*/

define('N',"\n");
require __DIR__.'/../lib/delinstbyid.php';
$levs=['Debug', 'Info', 'Warning', 'Error'];

$opts=[
	'url'=>'https://fedipact.online/',
	'ofpath'=>__DIR__.'/fedipact.list'
];

$help='mustool.php
 DESCRIPTION
  fedipact.php downloads the list of Anti-Meta Fedi Pact signers from
  «'.$opts['url'].'», parses it, creates a machine readable signers list and
  saves it to «'.$opts['ofpath'].'».
 SYNOPSIS
  fedipact.php
 OPTIONS
  -h, --help
  Shows this help text and exits.

  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]=='-h' || $argv[$i]=='--help') {
		echo($help);
		exit(0);
	} else {
		mexit('don’t know how to interpret «'.$argv[$i].'» (use «-h» to read help).'.N,1);
	}
}

$codoms=0;
$ocont=@file($opts['ofpath'],FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
if (is_array($ocont))
	$codoms=count($ocont);
$cont=@file($opts['url'],FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
if (is_array($cont)) {
	$doms=[];
	foreach ($cont as $line)
//		if (preg_match('#<li>\s*🖤\s*[a-z0-9\.\-\_]+@([a-z0-9\.\-\_]+)\s*</li>#iu',$line,$matches)===1)
		if (preg_match('#<li>\s*[a-z0-9\.\-\_]+@([a-z0-9\.\-\_]+)\s*</li>#iu',$line,$matches)===1)
			if (!in_array($matches[1],$doms))
				$doms[]=mb_strtolower($matches[1]);
	$cdoms=count($doms);
	if ($cdoms>0) {
		sort($doms);
		$doms=implode(N,$doms);
		if (@file_put_contents($opts['ofpath'],$doms.N)===false)
			mexit('couldn’t save domain list into «'.$opts['ofpath'].'» :-('.N,3);
		else
			eecho('done saving '.$cdoms.' entries into «'.$opts['ofpath'].'» :-)'.N,1);
	} else {
		mexit('couldn’t find any domain in «'.$opts['url'].'» :-('.N,2);
	}
} else {
	mexit('couldn’t open «'.$opts['url'].'».'.N,3);
}

exit(0);


// functions

function mexit($msg,$code) {
	global $link;
	if (isset($link) && $link!==false) mysqli_close($link);
	if ($code==0)
		eecho($msg,1);
	else
		eecho($msg,3);
	exit($code);
}

function eecho($msg,$lev=1) {
	global $levs;
	$time=microtime(false);
	$time=explode(' ',$time);
	$time=date('Y-m-d H:i:s',$time[1]).'.'.substr($time[0],2);
	$msg=$time.' '.$levs[$lev].': '.$msg;
	if ($lev<2)
		echo($msg);
	else
		fwrite(STDERR,$msg);
}

?>
