kateb

 view release on metacpan or  search on metacpan

lib/kateb/LocalData.pm  view on Meta::CPAN

package kateb::LocalData;
$kateb::LocalData::VERSION = '1.1.0';

use strict;
use warnings;
use 5.012;

use kateb::FontInfo;
use JSON::PP;
use File::Temp;
use File::Path qw(make_path rmtree);
use File::Spec;
use File::Spec::Functions qw(catdir catfile tmpdir);

sub new {
	my $class  = shift;
	my $self = _prepare();
	bless $self, $class;

	return $self;
}

sub _prepare {
	my $execname = "kateb";

	#----------------------- GLOBAL VARIABLES -----------------------#
	# create empty envs
	my $json_file;

	# Home directory
	my $home_dir = $ENV{HOME} || $ENV{LOGDIR} || (getpwuid $<)[7] || `echo -n ~`;

	# Configuration directory
	my $config_dir = catdir($ENV{XDG_CONFIG_HOME} || catdir($home_dir, '.config'), 	$execname);

	# download fonts directory
	my $cache_font_dir = catdir($config_dir, 'fonts');

	# user font directory [target directory]
	my $root_font_dir = $^O eq 'darwin'
		? catdir('/', 'Library', 'Fonts')
		: catdir('/', 'usr', 'share', 'fonts', 'truetype', 'farsifreefont');
	my $user_font_dir = $^O eq 'darwin'
		? catdir($home_dir, 'Library', 'Fonts')
		: catdir($home_dir, '.local', 'share', 'fonts', 'farsifreefont');
	my $target_dir = $> == 0 ? $root_font_dir : $user_font_dir;

	# temp dir for downloads
	my $temp_dir = File::Temp->newdir();
	$temp_dir = catdir($temp_dir, 'kateb');
	make_path $temp_dir;

	# Configuration file
	# my $config_file = catfile($config_dir, "$execname.conf");

	# json Database file
	my $json_data_file = catfile($config_dir, "$execname.json");

	# check for exist config dir
	if (not -d $config_dir)
	{
		make_path($config_dir)
		  or note(":: Unable to create directory <<$config_dir>>: {!$!!}");
	}

	# check for exist taget directory
	if (not -d $target_dir)
	{
		make_path($target_dir)
		  or note(":: Unable to create directory <<$target_dir>>: {!$!!}");
	}

	# check json data base file
	my $installed_versions;

	if (not -e $json_data_file)
	{
		_reset_json_file($json_data_file);
		$json_file = _read_file($json_data_file);
		$installed_versions = decode_json($json_file);
	} else
	{
		$json_file = _read_file($json_data_file);
		eval
		{
			$installed_versions = decode_json($json_file)
		}; if ($@)
		{
			_reset_json_file($json_data_file);
			$json_file = _read_file($json_file);
			$installed_versions = decode_json($json_file);



( run in 1.992 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )