<?php

// ----------------------------------------------------------------------
// fetch status infos von den einzelnen Phasen
// ----------------------------------------------------------------------

$aConfig = array(
    // Basispfad:
    'workDir' => '/var/imldeployment',
    'tmpDir' => '/var/tmp/imldeployment',
    'versionsToKeep' => 5, // for cleanup: keep n unused versions
    'builtsToKeep' => 3,
    'build' => array(
        'env' => 'export RVMSCRIPT="/usr/local/rvm/scripts/rvm";',
        'hooks' => array(
            'build-postclone' => 'hooks/onbuild-postclone',
            'build-precompress' => 'hooks/onbuild',
        ),
    ),
    'lang' => 'de', // for available languages see ./config/lang/*.json
    // rsync of archives
    'mirrorPackages' => array(),
    // ssh install - if a host is given
    'installPackages' => array(
        'user' => 'imldeployment',
        
        // command to update ssh hostkey in known_hosts file
        // %s is name of the server (2x)
        'addkeycommand' => '/usr/bin/ssh-keygen -R %s; /usr/bin/ssh-keyscan -t rsa %s >> /home/www-data/.ssh/known_hosts',
        
        // command to verify if puppet host is correct
        'testcommand' => 'sudo puppet --version',
        
        // puppet agent liefert 0 oder 2 zurueck, wenn OK
        // http://docs.puppetlabs.com/references/3.4.0/man/apply.html
        // 'command' => 'sudo puppet agent -t --detailed-exitcodes ; rc=$?; if [ $rc -eq 2 ]; then rc=0; fi ; exit $rc',
        // task#38890 replace direct puppet call with a shell script
        'command' => '/usr/local/bin/puppetrun.sh',
    ),
    'phases' => array(
        "preview" => array(
            'css' => array(
                'bgdark' => 'background:#393E50;  color:#f8f8f8;',
                'bglight' => 'background:#eee; color:#333; background:rgba(210,210,210,0.3); ',
                'bgbutton' => 'background:#393E50; color:#fcfcfc; border: 1px solid rgba(0,0,0,0.15);',
            ),
        ),
        "stage" => array(
            'css' => array(
                'bgdark' => 'background:#3F88C5; color:#f8f8f8;',
                'bglight' => 'background:#f0f4f8; color:#333; background:rgba(200,210,220,0.3); ',
                'bgbutton' => 'background:#3F88C5; color:#fcfcfc; border: 1px solid rgba(0,0,0,0.15);',
            ),
        ),
        "live" => array(
            'css' => array(
                'bgdark' => 'background:#44BBA4; color:#f8f8f8;',
                'bglight' => 'background:#f4f8f0; color:#333; background:rgba(210,220,200,0.3); ',
                'bgbutton' => 'background:#44BBA4; color:#fcfcfc; border: 1px solid rgba(0,0,0,0.15);',
            ),
            // wenn deploytimes existiert, dann wird nach dem Deploy das Paket 
            // in einer Queue zurueckgehalten
            "deploytimes" => array('/(Mon|Tue|Wed|Thu)\ 14\:/'),
        ),
    ),
    'showdebug' => array(
        'ip'=>array(),
    ),
    
    // generate template in hook/templates
    'auth' => array(
        'ldap' => array(
            'server'       => '<%= @replace["ldap-url"] %>',
            'port'         => 636,
            'DnLdapUser' => '<%= @replace["ldap-user"] %>',
            'PwLdapUser' => '<%= @replace["ldap-password"] %>',
            'DnUserNode'   => '<%= @replace["ldap-dn-user"] %>',
            'DnAppNode'    => '<%= @replace["ldap-cn-apps"] %><%= @replace["ldap-dn-apps"] %>',
            'debugLevel'   => 0,
        )
    ),
    'foreman' => array(
        'api'=>'<%= @replace["foreman-url"] %>', // with ending "/"
        'user'=>'<%= @replace["foreman-user"] %>',
        'password'=>'<%= @replace["foreman-password"] %>',
        'ignore-ssl-error'=><%= @replace["ignore-ssl-error"] %>,
        // 'varname-replace'=>'ci-replacement',
    ),
    // where to store project data
    'projects' => array(
        'json' => array(
            'active'       => true,
        ),
        'ldap' => array(
            'active'       => false,
        ),
    ),
    // notifications to messengers ...
    'messenger'=>array(<%= @replace["messenger"] %>),    
);

// ----------------------------------------------------------------------
// override for local development
// ----------------------------------------------------------------------

// unsafe ... 
// make a check that fits your environemnt
// php_uname("n") can send a short hostname (without domain)
$bProd=!!strpos(__DIR__, '/ci.iml.unibe.ch/');

switch (php_uname("n")) {
    case "USER":
    case "AAE49":
    case "dev.ci.iml.unibe.ch":
        $aConfig['workDir'] = "D:\imldeployment";
        break;

    case "ci.iml.unibe.ch":
    case "ci":

        if ($bProd){
            // synch der Pakete nur auf dem Livesystem
            $aConfig['mirrorPackages'] = array(
                'puppet' => array(
                    'type' => 'rsync',
                    'runas' => '', // www-data', // nur fuer commandline
                    'target' => 'ladmin@calcium.iml.unibe.ch:/share/imldeployment',
                ),
                'puppet.one' => array(
                    'type' => 'rsync',
                    'runas' => '', // www-data', // nur fuer commandline
                    'target' => 'copy-deployment@puppet.one.iml.unibe.ch:/var/shared/imldeployment',
                ),
                'pkg-server' => array(
                    'type' => 'rsync',
                    'runas' => '', // www-data, // nur fuer commandline
                    'target' => 'copy-deployment@software.shared.se.iml.unibe.ch:/var/shared/imldeployment',
                ),
            );
        }
        break;

    default:
        break;
}
if (!array_key_exists('tmpDir', $aConfig) || !$aConfig["tmpDir"]){
    $aConfig["tmpDir"] = (getenv("temp") ? getenv("temp") : "/var/tmp") . '/imldeployment';
}

// ----------------------------------------------------------------------
// TODO: include custom settings that were saved in the GUI
// ----------------------------------------------------------------------


// ----------------------------------------------------------------------
// generate some vars
// ----------------------------------------------------------------------

$aConfig = array_merge($aConfig, array(
    'appRootDir' => dirname(dirname(__FILE__)),
    'configDir' => dirname(__FILE__),
    'dataDir' => $aConfig['workDir'] . '/data',    // to write data: ssh keys, projects, database
    'buildDir' => $aConfig['workDir'] . '/build',
    'buildDefaultsDir' => $aConfig['workDir'] . '/defaults',
    'packageDir' => $aConfig['workDir'] . '/packages',
    'archiveDir' => $aConfig['workDir'] . '/packages/_files',
        ));