#!/usr/bin/env php
<?php

require_once('vendor/autoload.php');
require_once('utils.inc.php');

use PhpDevCommunity\DotEnv;
use League\CLImate\CLImate;

$climate = new CLImate();
$climate->description('yeswiki-import, import YesWiki from another server 🌈🦄');

if (posix_getuid() != 0) {
    $climate->error('ERROR : this script needs root privilege to run.');
    exit;
}

$absolutePathToEnvFile = __DIR__ . '/.env';
if (file_exists($absolutePathToEnvFile)) {
    (new DotEnv($absolutePathToEnvFile))->load();
} else {
    $climate->error('ERROR : No .env file found.');
    exit;
}

$climate->arguments->add([
    'from' => [
        'prefix'       => 'f',
        'longPrefix'   => 'from',
        'description'  => 'Url of source wiki on another server',
        'required'     => true,
        'defaultValue' => 'https://example.com'
    ],
    'to' => [
        'prefix'       => 't',
        'longPrefix'   => 'to',
        'description'  => 'Url of destination wiki already created on server',
        'required'     => true,
        'defaultValue' => 'https://destination.com'
    ],
    'source' => [
        'prefix'       => 's',
        'longPrefix'   => 'source',
        'description'  => 'server name as indicated in ssh config',
        'required'     => false,
        'defaultValue' => 'cooptools'
    ],
]);
$climate->arguments->parse();
$from = $climate->arguments->get('from');
$to = $climate->arguments->get('to');
$source = $climate->arguments->get('source');

if (empty($from) || $from == 'https://example.com' || empty($to) || $to == 'https://destination.com') {
    $climate->error('ERROR : you need a --from (the source url) and a --to (the destination url).'."\n");
    $climate->usage();
    exit;
}

if (!filter_var($from, FILTER_VALIDATE_URL)) {
    $climate->error('ERROR : the source url given with --from '.$from.' is not a valid url.'."\n");
    exit;
}
$fromUrl = parse_url($from);

if (domainIsOnServer($fromUrl['host'])) {
    $climate->error('ERROR : Domain '.$fromUrl['host'].' should be external, not on the server.'."\n");
    exit;
}
#if (!$fromWiki = wikiIsOnServer($fromDomainWithoutWww.($fromUrl['path'] ?? ''))) {
#    $climate->error('ERROR : no Yeswiki found '.$fromDomainWithoutWww.$fromUrl['path'].' on the server.'."\n");
#    exit;
#}

if (!filter_var($to, FILTER_VALIDATE_URL)) {
    $climate->error('ERROR : the destination url given with --to '.$to.' is not a valid url.'."\n");
    exit;
}
$toUrl = parse_url($to);
$toDomainWithoutWww = preg_replace('/^www\./m', '', $toUrl['host']);

if (!domainIsOnServer($toDomainWithoutWww)) {
    $climate->error('ERROR : Domain '.$toDomainWithoutWww.' was not found on the server.'."\n");
    exit;
}
if (!$toWiki = wikiIsOnServer($toDomainWithoutWww.($toUrl['path'] ?? ''))) {
    $climate->error('ERROR : no Yeswiki found '.$toDomainWithoutWww.$toUrl['path'].' on the server.'."\n");
    exit;
}

if ($from === $to) {
    $climate->error('ERROR : --from and --to parameters must be different.'."\n");
    exit;
}

$climate->shout("Source: ".$source);

$climate->bold()->underline()->out('Move a YesWiki');
$climate->out('This will transfert YesWiki on <bold>'.$from.'</bold> to <bold>'.$to.'</bold>');
$climate->red('CAREFUL: the destination yeswiki will be replaced.'."\n");
$input = $climate->confirm('Is it all good ?');
if ($input->confirmed()) {
    $climate->shout(
        'The yeswiki <bold>'.$from.'</bold> was successfully moved to <bold>'.$to.'</bold>, congrats ! 🎉'."\n"
    );
} else {
    $climate->info('Ok, let\'s stop here...');
}

#rm -rf files custom themes private
#scp -r cooptools:/var/www/cocotier.info/web/qptce/files .
#scp -r cooptools:/var/www/cocotier.info/web/qptce/custom .
#scp -r cooptools:/var/www/cocotier.info/web/qptce/themes .
#scp -r cooptools:/var/www/cocotier.info/web/qptce/private .
#chown -R leclosionfr2:leclosionfr2 .
#cat /home/yeswikileclosionfr/yeswiki.l-eclosion.fr/wakka.config.php
#vi wakka.config.php
# config mail base_url et extra conf
#DROP TABLE `yeswiki_acls`, `yeswiki_links`, `yeswiki_nature`, `yeswiki_pages`, `yeswiki_referrers`, `yeswiki_triples`, `yeswiki_users`;
#ssh cooptools "mysqldump -uyeswikinsudb -py3sw1k1YES yeswikinsuferme yeswiki_symphose__acls yeswiki_symphose__links yeswiki_symphose__nature yeswiki_symphose__pages yeswiki_symphose__referrers yeswiki_symphose__triples yeswiki_symphose__users" > dump.sql
# on recherche/remplace l'url
# add good prefixes and YesWikiProAdmin