#!/usr/bin/php
<?php
/*
  vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  Codificación: UTF-8
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2003 Palosanto Solutions S. A.                    |
  +----------------------------------------------------------------------+
  | Cdla. Nueva Kennedy Calle E 222 y 9na. Este                          |
  | Telfs. 2283-268, 2294-440, 2284-356                                  |
  | Guayaquil - Ecuador                                                  |
  +----------------------------------------------------------------------+
  | Este archivo fuente está sujeto a las políticas de licenciamiento    |
  | de Palosanto Solutions S. A. y no está disponible públicamente.      |
  | El acceso a este documento está restringido según lo estipulado      |
  | en los acuerdos de confidencialidad los cuales son parte de las      |
  | políticas internas de Palosanto Solutions S. A.                      |
  | Si Ud. está viendo este archivo y no tiene autorización explícita    |
  | de hacerlo, comuníquese con nosotros, podría estar infringiendo      |
  | la ley sin saberlo.                                                  |
  +----------------------------------------------------------------------+
  | Autores: Alex Villacís Lasso <a_villacis@palosanto.com>              |
  +----------------------------------------------------------------------+
  $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;
}
?>