#!/usr/bin/python3
# -*- coding: utf-8 -*-

# Unity Mail, QuickList URL processor
# Author: Dmitry Shachnev <mitya57@gmail.com>
# License: GNU GPL 3 or higher; http://www.gnu.org/licenses/gpl.html

import os.path
import sys
import webbrowser
from configparser import RawConfigParser
from subprocess import Popen

CONFIG_DIR = os.path.expanduser('~/.config/')

if not os.path.exists(CONFIG_DIR+'autostart/unity-mail-autostart.desktop'):
	# First run of Unity Mail, open um-config
	Popen('um-config')
	sys.exit()

parser = RawConfigParser()
parser.read(CONFIG_DIR+'unity-mail.conf')

default = \
{  'Home': 'https://mail.google.com/mail/',
'Compose': 'https://mail.google.com/mail/#compose',
  'Inbox': 'https://mail.google.com/mail/#inbox',
   'Sent': 'https://mail.google.com/mail/#sent' }

def print_names():
	print('Possible URL names: "Home", "Compose", "Inbox", "Sent"')

if len(sys.argv) > 1:
	name = sys.argv[1]
	if parser.has_option('URLs', name):
		url = parser.get('URLs', name)
		if url.startswith('Exec:'):
			Popen(url[5:].split(' '))
		else:
			webbrowser.open(url)
	else:
		try:
			webbrowser.open(default[name])
		except KeyError:
			print('Error: unknown URL name!')
			print_names()
else:
	print('Error: please specify URL name.')
	print_names()
