/* Helper class for XDG Base Directory Specification
 * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
 */

package xdg;

import java.io.File;
import java.io.IOException;

public final class xdg {

	private static final String filesep = System.getProperty("file.separator");

	private static final String getdir(String env, String def, String dir){
		String conf = System.getenv(env);
		if (conf == null || conf.length() == 0)
			conf = System.getProperty("user.home") + filesep + def;
		if (dir != null && dir.length() > 0)
			conf += filesep + dir;
                try {
                        return new File(conf).getCanonicalPath();
                } catch (IOException e) {
                        return conf;
                }
        }

	public static final String data_home() {
		return data_home(null);
	}

	public static final String config_home() {
		return config_home(null);
	}

	public static final String cache_home() {
		return cache_home(null);
	}

	public static final String data_home(String dir) {
		return getdir("XDG_DATA_HOME", ".local" + filesep + "share", dir);
	}

	public static final String config_home(String dir) {
		return getdir("XDG_CONFIG_HOME", ".config", dir);
	}

	public static final String cache_home(String dir) {
		return getdir("XDG_CACHE_HOME", ".cache", dir);
	}

	public static final boolean mkdirs_file(String filename) {
		return new File(filename).getParentFile().mkdirs();
	}

	public static final boolean mkdirs_path(String pathname) {
		return new File(pathname).mkdirs();
	}

}
