#!/usr/bin/php
<?php
/*
  vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  Codificación: UTF-8
  +----------------------------------------------------------------------+
  | Elastix version 2.0                                                  |
  | http://www.elastix.org                                               |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006 Palosanto Solutions S. A.                         |
  +----------------------------------------------------------------------+
  | Cdla. Nueva Kennedy Calle E 222 y 9na. Este                          |
  | Telfs. 2283-268, 2294-440, 2284-356                                  |
  | Guayaquil - Ecuador                                                  |
  | http://www.palosanto.com                                             |
  +----------------------------------------------------------------------+
  | The contents of this file are subject to the General Public License  |
  | (GPL) Version 2 (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.opensource.org/licenses/gpl-license.php                   |
  |                                                                      |
  | Software distributed under the License is distributed on an "AS IS"  |
  | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See  |
  | the License for the specific language governing rights and           |
  | limitations under the License.                                       |
  +----------------------------------------------------------------------+
  | The Original Code is: Elastix Open Source.                           |
  | The Initial Developer of the Original Code is PaloSanto Solutions    |
  +----------------------------------------------------------------------+
  $Id: repoconfig.php,v 1.1 2007/01/09 23:49:36 alex Exp $
*/
require_once 'Console/Getopt.php';

define('REPO_DIR', '/etc/yum.repos.d/');
/*
 * This script expects a list of all repos that should become enabled. Any other
 * repo that is not mentioned in the list will get disabled.
 */

$repolist = $argv;
array_shift($repolist);
exit(action_repoConfiguration($repolist) ? 0 : 1);

function error_exit($sMsg, $errorcode = 1)
{
    fwrite(STDERR, $sMsg);
    exit($errorcode);
}

function action_repoConfiguration($repolist)
{
	$bExito = TRUE;
    
    foreach (scandir(REPO_DIR) as $sRepoFile)
    if (is_file(REPO_DIR.$sRepoFile) && preg_match('/\.repo$/', $sRepoFile)) {
        $sCurrentRepo = NULL;
        $bEnabledSet = FALSE;
        $content = array();
        foreach (file(REPO_DIR.$sRepoFile) as $s) {
        	// Switch into next repository
            $regs = NULL;
            if (preg_match('/^\[(\S+)\]/', $s, $regs)) {
            	if (!$bEnabledSet && !is_null($sCurrentRepo)) {
            		// Previous repository never set the enabled flag
                    $content[] = 'enabled='.(in_array($sCurrentRepo, $repolist) ? '1' : '0')."\n\n"; 
            	}
                $sCurrentRepo = $regs[1];
                $bEnabledSet = FALSE;
                $content[] = $s;
            } elseif (preg_match('/^enabled\s*=/', $s)) {
            	// Found explicit enabled directive
                $content[] = 'enabled='.(in_array($sCurrentRepo, $repolist) ? '1' : '0')."\n";
                $bEnabledSet = TRUE;
            } else {
                // Other content
            	$content[] = $s;
            }
        }
        if (!$bEnabledSet && !is_null($sCurrentRepo)) {
            // Last repository never set the enabled flag
            $content[] = 'enabled='.(in_array($sCurrentRepo, $repolist) ? '1' : '0')."\n"; 
        }
        file_put_contents(REPO_DIR.$sRepoFile, $content);
    }
    
    return $bExito;
}
?>