#!/usr/bin/php
<?php
/*
  vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  +----------------------------------------------------------------------+
  | Issabel version 5.0                                                  |
  | http://www.issabel.org                                               |
  +----------------------------------------------------------------------+
  | Copyright (c) 2023 Issabel Foundation                                |
  +----------------------------------------------------------------------+
  | 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.                                       |
  +----------------------------------------------------------------------+
  $Id: betaenable, Fri 14 Sep 2018 12:01:47 PM EDT, nicolas@issabel.com
*/
require_once 'Console/Getopt.php';

// Parse command-line options
$opt = Console_Getopt::getopt($argv, '', array(
    'enable',
    'disable',
    'status'
));

if (PEAR::isError($opt)) error_exit($opt->getMessage()."\n");

//validateOptions($opt);
foreach ($opt[0] as $option) switch ($option[0]) {
case '--enable':
    exit(action_enableBeta($opt, TRUE) ? 0 : 1);
case '--disable':
    exit(action_enableBeta($opt, FALSE) ? 0 : 1);
case '--status':
    exit(action_status($opt, FALSE) ? 0 : 1);
}

error_exit("No action specified (--enablem, --disable or --status)\n");

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

function action_enableBeta($opt, $bEnable) {
    if($bEnable) {
        exec("dnf config-manager --enable issabel-cinco-beta", $output, $ret);
    } else {
        exec("dnf config-manager --disable issabel-cinco-beta", $output, $ret);
    }
    if($ret) {
        error_exit("Could not perform action");
    }
    return 0;
}

function action_status() {
    exec("dnf repolist enabled | grep -i issabel-cinco-beta", $output, $ret);
    if($ret) {
        echo "disabled";
    } else {
        echo "enabled";
    }
    return 0;
}

?>
