#!/usr/bin/env python
# Copyright (C) 2010, Thomas Leonard
# See the README file for details, or visit http://0install.net.

from __future__ import print_function

import locale
import logging
from logging import warn
try:
	locale.setlocale(locale.LC_ALL, '')
except locale.Error:
	warn('Error setting locale (eg. Invalid locale)')

import os, sys

## PATH ##

from optparse import OptionParser

from zeroinstall.injector import reader, model
from zeroinstall.injector.config import load_config
from zeroinstall import support, alias, helpers, apps, _
from zeroinstall.support import basedir

config = load_config()

def export(name, value):
	"""Try to guess the command to set an environment variable."""
	shell = os.environ.get('SHELL', '?')
	if 'csh' in shell:
		return "setenv %s %s" % (name, value)
	return "export %s=%s" % (name, value)

# Do this here so we can include it in the help message.
# But, don't abort if there isn't one because we might
# be doing something else (e.g. --manpage)
first_path = apps.find_bin_dir()
in_path = first_path is not None
if not in_path:
	first_path = os.path.expanduser('~/bin/')

parser = OptionParser(usage="usage: %%prog [options] alias [interface [main]]\n\n"
		"Creates a script to run 'interface' (will be created in\n"
		"%s unless overridden by --dir).\n\n"
		"If no interface is given, edits the policy for an existing alias.\n"
		"For interfaces providing more than one executable, the desired\n"
		"'main' binary or command may also be given." % first_path)
parser.add_option("-c", "--command", help="the command the alias will run (default 'run')", metavar='COMMNAD')
parser.add_option("-m", "--manpage", help="show the manual page for an existing alias", action='store_true')
parser.add_option("-r", "--resolve", help="show the URI for an alias", action='store_true')
parser.add_option("-v", "--verbose", help="more verbose output", action='count')
parser.add_option("-V", "--version", help="display version information", action='store_true')
#parser.add_option("-d", "--dir", help="install in DIR", dest="user_path", metavar="DIR")

(options, args) = parser.parse_args()

if options.verbose:
	logger = logging.getLogger()
	if options.verbose == 1:
		logger.setLevel(logging.INFO)
	else:
		logger.setLevel(logging.DEBUG)
	hdlr = logging.StreamHandler()
	fmt = logging.Formatter("%(levelname)s:%(message)s")
	hdlr.setFormatter(fmt)
	logger.addHandler(hdlr)

if options.version:
	import zeroinstall
	print("0alias (zero-install) " + zeroinstall.version)
	print("Copyright (C) 2010 Thomas Leonard")
	print("This program comes with ABSOLUTELY NO WARRANTY,")
	print("to the extent permitted by law.")
	print("You may redistribute copies of this program")
	print("under the terms of the GNU Lesser General Public License.")
	print("For more information about these matters, see the file named COPYING.")
	sys.exit(0)

if options.manpage:
	print('"0alias --manpage" is deprecated; use "0install man" instead', file = sys.stderr)
	if len(args) != 1:
		os.execlp('man', 'man', *args)
		sys.exit(1)

if len(args) < 1 or len(args) > 3:
	parser.print_help()
	sys.exit(1)
alias_prog, interface_uri, main = (list(args) + [None, None])[:3]
command = options.command
if command is None:
	command = 'run'
elif command is '':
	command = None

if options.resolve or options.manpage:
	if interface_uri is not None:
		parser.print_help()
		sys.exit(1)

#if options.user_path:
#	first_path = options.user_path

if interface_uri is None:
	if options.command:
		print("Can't use --command when editing an existing alias", file=sys.stderr)
		sys.exit(1)
	try:
		if not os.path.isabs(alias_prog):
			full_path = support.find_in_path(alias_prog)
			if not full_path:
				raise alias.NotAnAliasScript("Not found in $PATH: " + alias_prog)
		else:
			full_path = alias_prog

		alias_info = alias.parse_script(full_path)
		interface_uri = alias_info.uri
		main = alias_info.main
		command = alias_info.command
	except (alias.NotAnAliasScript, IOError) as ex:
		# (we get IOError if e.g. the script isn't readable)
		if options.manpage:
			logging.debug("not a 0alias script '%s': %s", alias_prog, ex)
			os.execlp('man', 'man', *args)
		print(str(ex), file=sys.stderr)
		sys.exit(1)

interface_uri = model.canonical_iface_uri(interface_uri)

if options.resolve:
	print(interface_uri)
	sys.exit(0)

if options.manpage:
	sels = helpers.ensure_cached(interface_uri, command, config = config)
	if not sels:
		# Cancelled by user
		sys.exit(1)
	alias_name = os.path.basename(args[0])
	helpers.exec_man(config.stores, sels, main = main, fallback_name = alias_name)
	assert 0

if len(args) == 1:
	os.execlp('0launch', '0launch', '-gd', '--', interface_uri)
	sys.exit(1)

print(_('("0alias" is deprecated; using "0install add" instead)'))

from zeroinstall import cmd
args = ['add']
if options.command:
	args.append('--command=' + options.command)
if main is not None:
	print("Sorry, using a custom main is no longer supported.", file = sys.stderr)
	sys.exit(1)

print('0install {options} {alias} {uri}'.format(
	options = ' '.join(args),
	alias = alias_prog,
	uri = interface_uri))

args += ['--', alias_prog, interface_uri]

cmd.main(args, config = config)
