------------------------------------------------------------
--- cc-get version 0.1.1
--- Created by DJRanger
---
--- This is very experimental, use at your own risk
--- Install:
---   Put this script and the cc-get-bin folder into your
---   .minecraft/mods/ComputerCraft/lua/rom/programs/ folder
---
------------------------------------------------------------

VERSION = '0.1.1'

if http == nil then
	print('Please enable the http api.')
	print('Look for enableAPI_http=0 in .minecraft/config/ and change the zero to a one and then restart Minecraft')
	return false
end
json = (loadfile("/rom/programs/cc-get-bin/dkjson.lua")())
shell.run('cc-get-bin/params.lua')
shell.run('cc-get-bin/package.lua')

params = parse_params({...}, {version = '-v *([^ ]+)'})

urlBase = 'http://cc-get.djranger.com/cc/' .. VERSION .. '/'
local action = params[1]

if not fs.exists('/bin') then
	fs.makeDir('/bin')
	fs.makeDir('/bin/lib')
end

if fs.exists('/bin/packages.json') then
	fs.makeDir('/bin/lib');
	fs.move('/bin/packages.json', '/bin/lib/packages.json')
end

if not fs.exists('/bin/lib/packages.json') then
	file = fs.open('/bin/lib/packages.json', 'w')
	file.write('{"packages": {}, "installed": 0}')
	file.close()
end

local file = fs.open('/bin/lib/packages.json', 'r')
data = json.decode(file:readAll())
file:close()

function save_data()
	file = fs.open('/bin/lib/packages.json', 'w')
	file.write(json.encode(data))
	file.close()
end

if not action then
	-- No params, show some info about the program
	shell.run('help', 'cc-get')
elseif action == 'install' or action == 'remove' then
	name = params[2]
	if not name then
		print('Usage: cc-get ' .. action .. ' <program>')
	
	elseif action == 'install' then
		local status, package = Package.get(name, params.version)
		if status then
			local version = ''
			if params.version then
				version = ' version ' .. package.version
			end
			term.write('Really install ' .. name .. version .. '? y/n: ')
			answer = read()
			
			if answer:sub(0,1) == 'y' then
				status = package:install()
				if status == true then
					package.source = nil
					data.packages[ package.name ] = package
					print( package.name ..' version ' .. package.version .. ' installed' )
				else
					pritn('Something went wrong installing ' .. package.name)
				end
			end
		else
			print(package)
		end
	elseif action == 'remove' then
		local status, package = Package.load( name )
		if status then
			term.write('Really remove the package ' .. name .. '? y/n: ')
			answer = read()
			if answer:sub(0,1) == 'y' then
				package:uninstall()
				print('The ' .. package.name .. ' package has been removed')
			end
		else
			print(package)
		end
	end
	
elseif action == 'update' then
	local qs = ''
	for name, info in pairs(data.packages) do
		qs = qs .. name .. ','
	end
	
	qs = qs:sub(0, -2)
	
	result = json.decode( http.get(urlBase .. 'info/' .. qs).readAll() )
	local updates = 0
	
	for name, info in pairs(result) do
		if data.packages[ name ].timestamp < info.timestamp then
			-- Need to update
			print('Updating ' .. name .. ' from version ' .. data.packages[ name ].version .. ' to version ' .. info.version .. '...')
			status, package = Package.load(name)
			package:update()
			print( package.name .. ' updated ')
			updates = updates + 1
		end
	end
	
	if updates == 0 then
		print('Nothing needs to be updated')
	end
elseif action == 'list' then
	local i = 0
	for name, info in pairs(data.packages) do
		print( name .. ' version ' .. info.version )
		i = i + 1
	end
	
	if i == 0 then
		print('No packages installed')
	end

else
	print('Unknown command "' .. action .. '"')
end

save_data()