/*
 *  NekoML Standard Library
 *  Copyright (c)2005 Motion-Twin
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License or the LICENSE file for more details.
 */

neko("
	@sys_exit = $loader.loadprim('std@sys_exit',1);
	@get_env = $loader.loadprim('std@get_env',1);
	@get_cwd = $loader.loadprim('std@get_cwd',0);
	@exe_path = $loader.loadprim('std@sys_exe_path',0);
	@sys_file_type = $loader.loadprim('std@sys_file_type',1);
	@sys_read_dir = $loader.loadprim('std@sys_read_dir',1);
");

type version {
	maj : int;
	min : int;
	build : int;
}

function without_extension(s) {
	match List.rev (String.split s ".") {
	| [] -> ""
	| [x] -> x
	| ext :: l -> String.concat "." (List.rev l)
	}
}

function extension(s) {
	match List.rev (String.split s ".") {
	| [] | [_] -> ""
	| ext :: _ -> ext
	}
}

function without_dir(s) {
	var s = String.concat "/" (String.split s "\\");
	match List.rev (String.split s "/") {
	| [] -> assert()
	| file :: _ -> file
	}
}

var array_dependency = Array.make

function args() : string array {
	neko("
		@Array.@make($loader.args)
	");
}

function exit(code : int) : 'a {
	neko("
		@sys_exit(code);
	");
}

var exists : string -> bool = neko "$loader.loadprim('std@sys_exists',1)"

var version = {
	var v : int = neko "$version()";
	{
		maj = v / 100;
		min = (v / 10) % 10;
		build = v % 10;
	}
}

function get_env( s : string ) : string option {
	var s = neko "@get_env(s)";
	if s == neko "null" then None else Some s;
}

var put_env : string -> string -> void = neko "$loader.loadprim('std@put_env',2)"

function get_cwd() : string {
	neko "@get_cwd()"
}

var set_cwd : string -> void = neko "$loader.loadprim('std@set_cwd',1)"

function executable_path() : string {
	neko "@exe_path()"
}

function is_directory(str:string) : bool {
	neko "@sys_file_type(str) == 'dir'";
}

function read_directory(str:string) : string list {
	neko "@List.@make(@sys_read_dir(str))"
}
