<?php
/*
 * © Copyright 2010 IntraHealth International, Inc.
 * 
 * This File is part of iHRIS
 * 
 * iHRIS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Translate Templates
 *
 * @package I2CE
 * @subpackage Core
 * @author Carl Leitner <litlfred@ibiblio.org>
 * @copyright Copyright &copy; 2008, 2008 IntraHealth International, Inc. 
 * @version 1.0
 */


$i2ce_dir = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR;
$tmp_dir_base = "/home/" . trim(`whoami`) . '/ihris-release';

$def_locale_file = 'modules/Pages/modules/LocaleSelector/modules/DefaultLocales/DefaultLocales.xml';
$locale_cmd = 'grep "/value" ' . $i2ce_dir .  $def_locale_file . '  | awk -F\> \'{print $2}\' | awk -F: \'{print $1}\'';
$locales = array();
exec($locale_cmd,$locales);
foreach ($locales as &$loc) {
    $loc = trim($loc);
    if (!$loc) {
        unset($locales[$i]);
    }
}
unset($loc);
$locales = implode(",",$locales);

$base_dir = getcwd();
$launchpad_login = false;
$full_tar_balls = array(
    'I2CE'=>'I2CE textlayout',
    'ihris-manage'=>'I2CE textlayout ihris-common ihris-manage',
    'ihris-qualify'=>'I2CE textlayout ihris-common ihris-qualify',
    'ihris-retain'=>'I2CE textlayout ihris-common ihris-retain',
    'ihris-common'=>'I2CE textlayout ihris-common'
    );

$usage[] = 
    "[--launchpad-login=T/F] set to login name/team for PPA\n";


$search_dirs = array();
require_once( $base_dir .  DIRECTORY_SEPARATOR . 'I2CE' . DIRECTORY_SEPARATOR . 'tools' . DIRECTORY_SEPARATOR . 'base.php');
require_once( $base_dir .  DIRECTORY_SEPARATOR . 'I2CE' . DIRECTORY_SEPARATOR . 'tools' . DIRECTORY_SEPARATOR . 'launchpad_base.php');
require_once( $i2ce_dir. DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'I2CE_Validate.php');


foreach ($args as $key=>$val) {
    switch($key) {
    case 'launchpad-login':
        $launchpad_login = $val;
        break;
    }
}


$created_tarballs = array(
    'I2CE'=>array('I2CE'=>"/home/litlfred/ihris-release/I2CE-4.0.19.tar.bz2"),
    'ihris-manage'=>array('ihris-manage'=>"/home/litlfred/ihris-release/ihris-manage-4.0.19.tar.bz2")
    );

$last_release = array(
    'I2CE'=>array('next_release'=>"4.0.0.test2"),
    'ihris-manage'=>array('next_release'=>"4.0.0.test2"),
    );


// //fooling around with uploads
// foreach ($created_tarballs as $top_mod=>$tar_balls) {
//     $project = strtolower($top_mod);
//     $version = $last_release[$top_mod]['next_release'];
//     $series = implode(".",array_slice(explode(".",$version),0,2));
//     echo "CR $version for $project\n";
//     createRelease($project,$series,$version);    
//     foreach ($tar_balls as $base=>$tar_ball) {
//         echo "UL $version for $project -> $tar_ball\n";
//         $desc = str_replace('Ihris','iHRIS',ucwords(str_replace('textlayout','Text Layout Tools',str_replace('-',' ',$base)))) . ' ' .$version;
//         uploadReleaseTarBall($tar_ball,$project,$series,$version,$desc);
//     }
// }

$do_upload = null;

foreach ($created_tarballs as $top_mod=>$tar_balls) {
    $project = strtolower($top_mod);
    $version = $last_release[$top_mod]['next_release'];
    $series = implode(".",array_slice(explode(".",$version),0,2));
    if (!createRelease($project,$series,$version)) {
        continue;
    }
    foreach ($tar_balls as $base=>$tar_ball) {
        if (!prompt("Upload tarball to launchpad ($tar_ball)?",$do_upload)) { 
            continue;
        }
        $desc = str_replace('Ihris','iHRIS',ucwords(str_replace('textlayout','Text Layout Tools',str_replace('-',' ',$base)))) . ' ' .$version;
        uploadReleaseTarBall($tar_ball,$project,$series,$version,$desc);
    }
}
