<?php

namespace Aps\App\Joomla;


class Uninstaller
{
    public function execute()
    {
        $root = getenv('WEB___DIR');

        $this->removeFile($root . '/configuration.php');

        // In server document root remove only default files,
        // in subfolder remove always
        if (getenv('BASE_URL_PATH') === '/') {
            $this->removeDefault($root . '/robots.txt', $root . '/robots.txt.dist');
            $this->removeDefault($root . '/.htaccess', $root . '/htaccess.txt');
            $this->removeDefault($root . '/web.config', $root . '/web.config.txt');
        } else {
            $this->removeFile($root . '/robots.txt');
            $this->removeFile($root . '/.htaccess');
            $this->removeFile($root . '/web.config');
        }
    }

    private function removeFile($path)
    {
        if (file_exists($path)) {
            unlink($path);
        }
    }

    private function removeDefault($path, $src)
    {
        if (file_exists($path) && file_exists($src)) {
            if (md5_file($path) == md5_file($src)) {
                unlink($path);
            }
        }
    }
}
