<?php
/**
 * This file is part of sPof.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright 2003-2014 Forschungsgemeinschaft elektronische Medien e.V. (http://fem.tu-ilmenau.de)
 * @lincense  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 * @link      http://spof.fem-net.de
 *
 * @package FeM\sPof\bin
 * @author dangerground
 * @since 1.0
 */

echo "Are you sure you want to do this?  Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = trim(fgets($handle));
if ($line != 'yes') {
    echo "ABORTING!\n";
    exit;
}
echo "\n";
echo "Thank you, continuing...\n";

echo "Please specify the organisation name (alphanumeric characters only):\n";
$line = trim(fgets($handle));
if (empty($line)) {
    echo "Missing organisation name, aborting.";
    exit;
}
echo "\n";
$vendor = $line;

echo "Please specify the project name (alphanumeric characters only):\n";
$line = trim(fgets($handle));
if (empty($line)) {
    echo "Missing project name, aborting.";
    exit;
}
echo "\n";
$project = $line;

// project config
$basedir = getcwd();


// define directory structure
$folders = [
    'application',
    'application/www',
    'application/www/controller',
    'application/www/form',
    'application/www/public',
    'application/www/public/css',
    'application/www/public/js',
    'application/www/stylesheet',
    'application/www/template',
    'application/www/template/index',
    'application/www/view',
    'bin',
    'config',
    'model',
    'tmp',
];

// define default files
$files = [];

// project config
$files['config/default.yml']=<<<CONFIG
## required
#############

server:
  url: http://example.org   # without /
  path: /            # path below the url, with leading and trailing /

database:
  user: username    # username to connect to the database
  pass: "*******"   # password for the user, the quote marks are not part of the password
  name: database    # to which database to connect
  namespace: public # which namespace aka schema
  port: 5432        # where the database listens
  debug: Yes        # debug logging of DB operations

## optional
#############

email:
  subject_prefix:               # prefixes all mails send by the mail system
  support: support@example.org  # used to send mails, where you can reply to
  noreply: noreply@example.org  # used for mails where nobody is used to reply

## values:  auth, dump, debug, info, notice, warning, error, critical, alert, emergency
#log_level: [warning, error, critical, alert, emergency] # which levels to log

CONFIG;

// composer config
$files['composer.json']=<<<COMPOSER
{
    "name": "$vendor/$project",
    "type": "project",
    "license": "proprietary",
    "autoload": {
        "psr-4": {"$vendor\\\\$project\\\\": ""}
    },
    "require": {
        "FeM/sPof": "@dev"
    }
}

COMPOSER;

// dispatcher file
$files['application/www/public/index.php']=<<<INDEX
<?php

require_once dirname(dirname(dirname(__DIR__)))."/vendor/autoload.php";

// Run the application!
\$app = new FeM\sPof\Application('\\\\$vendor\\\\$project\\\\application\\\\www\\\\');
FeM\\sPof\\Application::\$WEB_ROOT = __DIR__.'/';

\$app->dispatch();

INDEX;


$files['application/www/routes.yml']=<<<ROUTES
index:
  pattern:
  module: index
  show: hello

ROUTES;

$files['application/www/view/IndexView.php']=<<<INDEX
<?php

namespace $vendor\\$project\\application\\www\\view;

class IndexView extends \\FeM\\sPof\\view\\AbstractHtmlView
{
    public function hello()
    {
        \$this->assign('hello', 'world');
    }
}

INDEX;

$files['application/www/template/index/hello.tpl']=<<<TPL
<h2>Hello</h2>
{\$hello|escape}

TPL;

$files['application/www/template/layout.tpl']=<<<TPL
<!DOCTYPE html>
<html>
<head>
<title>sPof demo page</title>
</head>
<body>
<h1>It work's</h1>
If you're seeing this page, you probably got your own sPof site to work.
<hr />
{\$content}
</body>
</html>

TPL;


$files['application/www/htaccess_template']=<<<HTACCESS
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]

RewriteRule (.*) index.php/$1 [L,QSA]

HTACCESS;



// create structure
$mask = umask(0022);
echo "\n\n***** Create folders *****\n";
foreach ($folders as $folder) {
    if (!file_exists($basedir.'/'.$folder)) {
        mkdir($basedir.'/'.$folder);
        echo " * ".$folder." created\n";
    }
}
// reset mask
umask($mask);

// create default files
echo "\n\n***** Create files *****\n";
foreach ($files as $file => $content) {
    file_put_contents($basedir.'/'.$file, $content);
    echo " * ".$file." created\n";
}

echo "\n\n***** Install and run composer *****\n";
echo shell_exec("curl -sS https://getcomposer.org/installer | php");
echo shell_exec("chmod +x composer.phar");
echo shell_exec("./composer.phar install");

echo "\n\n***** Fix folder permissions *****\n";
echo shell_exec("chmod g+w public");
echo " * public set to g+w\n";
echo shell_exec("chmod g+w tmp");
echo " * tmp set to g+w\n";

echo "\n\n***** Next steps *****\n";
echo " * adjust default.yml database settings\n";
