#!/usr/bin/python
#coding: utf-8

import sys
import os
import json
from optparse import OptionParser

import licant.libs

usage = "usage: %prog [options] lib path"
parser = OptionParser(usage = usage)
parser.add_option("-u", "--user", action = "store_true", default = False)
parser.add_option("-r", "--remove", action = "store_true", default = False)
parser.add_option("-l", "--list", action = "store_true", default = False)
opts, args = parser.parse_args(sys.argv[1:])

paths_file = licant.libs.lpath if opts.user else licant.libs.gpath 
#if opts.local:
#	paths_file = licant.libs.lpath 
#else:
#	paths_file = licant.libs.gpath 

if (os.path.exists(paths_file)):
	try:
		paths = json.load(open(paths_file))
	except:
		print("Load error:")
		print("Wrong format", paths_file)
		exit(-1)
else:
	paths = {}

if opts.list:
	print("Libraries:")
	for k, v in paths.items():
		print(k, v)
	exit(0)

if not ((len(args) == 2) or (opts.remove and len(args) == 1)):
	#parser.print_help()
	parser.error("incorrect numbers of argument")
	exit(0)

name = args[0]

if opts.remove:
	try:
		del paths[name]
	except:
		print("Unregistred library {}".format(name))

else:
	path = args[1]
	abspath = os.path.abspath(path)
	exists = os.path.exists(abspath)

	if not exists:
		print("File {} is not exists".format(path))
		exit(-1)

	paths[name] = abspath

try:
	json.dump(paths, open(paths_file, "w"))
except IOError as e:
	print("Dump error:")
	print(e)
	exit(-1)