/**
 * This page export the game to javame for mobile phones
 * 
 * @author  bdaloukas
 * @version $Id: exportjavame.php,v 1.17 2011/08/03 20:04:32 bdaloukas Exp $
 * @package game
 **/
function game_OnExportJavaME($game, $javame)
{
    global $CFG, $DB;
    $courseid = $game->course;
    $course = $DB->get_record('course', array('id' => $courseid));
    $destdir = game_export_createtempdir();
    if ($javame->type == 'hangmanp') {
        $destmobiledir = 'hangmanp';
    } else {
        $destmobiledir = 'hangman';
    }
    $src = $CFG->dirroot . '/mod/game/export/javame/' . $destmobiledir . '/simple';
    if ($javame->filename == '') {
        $javame->filename = 'moodle' . $destmobiledir;
    }
    $handle = opendir($src);
    while (false !== ($item = readdir($handle))) {
        if ($item != '.' && $item != '..') {
            if (!is_dir($src . '/' . $item)) {
                $itemdest = $item;
                if (substr($item, -5) == '.java') {
                    continue;
                    //don't copy the java source code files
                }
                if (substr($itemdest, -8) == '-1.class') {
                    $itemdest = substr($itemdest, 0, -8) . '$1.class';
                }
                copy($src . '/' . $item, $destdir . '/' . $itemdest);
            }
        }
    }
    mkdir($destdir . '/META-INF');
    game_exportjavame_exportdata($src, $destmobiledir, $destdir, $game, $javame->maxpicturewidth, $javame->maxpictureheight);
    game_create_manifest_mf($destdir . '/META-INF', $javame, $destmobiledir);
    $filejar = game_create_jar($destdir, $course, $javame);
    if ($filejar == '') {
        $filezip = game_create_zip($destdir, $course->id, $javame->filename . '.zip');
    } else {
        $filezip = '';
    }
    if ($destdir != '') {
        remove_dir($destdir);
    }
    if ($filezip != '') {
        echo "unzip the {$filezip} in a directory and when you are in this directory use the command <br><b>jar cvfm {$javame->filename}.jar META-INF/MANIFEST.MF<br></b> to produce the jar files<br><br>";
    }
    $file = $filejar != '' ? $filejar : $filezip;
    $fullfile = "{$CFG->dataroot}/{$courseid}/export/{$file}";
    game_send_stored_file($fullfile);
}
/**
 * This page export the game to javame for mobile phones
 * 
 * @author  bdaloukas
 * @version $Id: exportjavame.php,v 1.9 2009/07/31 17:30:53 bdaloukas Exp $
 * @package game
 **/
function game_OnExportJavaME($gameid, $javame)
{
    global $CFG;
    $game = get_record_select('game', "id={$gameid}");
    $courseid = $game->course;
    $course = get_record_select('course', "id={$courseid}");
    $destdir = game_export_createtempdir();
    if ($javame->type == 'hangmanp') {
        $destmobiledir = 'hangmanp';
    } else {
        $destmobiledir = 'hangman';
    }
    $src = $CFG->dirroot . '/mod/game/export/javame/' . $destmobiledir . '/simple';
    if ($javame->filename == '') {
        $javame->filename = 'moodle' . $destmobiledir;
    }
    $handle = opendir($src);
    while (false !== ($item = readdir($handle))) {
        if ($item != '.' && $item != '..') {
            if (!is_dir($src . '/' . $item)) {
                $itemdest = $item;
                if (substr($item, -5) == '.java') {
                    continue;
                    //don't copy the java source code files
                }
                if (substr($itemdest, -8) == '-1.class') {
                    $itemdest = substr($itemdest, 0, -8) . '$1.class';
                }
                copy($src . '/' . $item, $destdir . '/' . $itemdest);
            }
        }
    }
    mkdir($destdir . '/META-INF');
    game_exportjavame_exportdata($src, $destmobiledir, $destdir, $game, $javame->maxpicturewidth, $javame->maxpictureheight);
    game_create_manifest_mf($destdir . '/META-INF', $javame, $destmobiledir);
    $filejar = game_create_jar($destdir, $course, $javame);
    if ($filejar == '') {
        $filezip = game_create_zip($destdir, $course->id, $javame->filename . '.zip');
    } else {
        $filezip = '';
    }
    if ($destdir != '') {
        remove_dir($destdir);
    }
    if ($filezip != '') {
        echo "unzip the {$filezip} in a directory and when you are in this directory use the command <br><b>jar cvfm {$javame->filename}.jar META-INF/MANIFEST.MF<br></b> to produce the jar files<br><br>";
    }
    $file = $filejar != '' ? $filejar : $filezip;
    ?>
        
<HTML><HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</HEAD><BODY>
<?php 
    echo "<a href=\"{$CFG->wwwroot}/file.php/{$courseid}/export/{$file}\">{$javame->name}</a>";
    ?>
</BODY>
</HTML>
<?php 
}