"""Generate an Outlook add-in XML manifest after the hosting origin is known."""
import sys, uuid
from urllib.parse import urlparse
from xml.sax.saxutils import escape
from pathlib import Path
if len(sys.argv)!=2: raise SystemExit('Usage: python3 make_manifest.py https://your-host')
u=urlparse(sys.argv[1]); origin=sys.argv[1].rstrip('/')
if u.scheme!='https' or not u.hostname or u.path not in ('','/') or u.query or u.fragment or u.username: raise SystemExit('Supply an HTTPS origin without a path, query or credentials.')
o=escape(origin)
resources=f'''<Resources><bt:Images><bt:Image id="Icon16" DefaultValue="{o}/addin/icon-16.png"/><bt:Image id="Icon32" DefaultValue="{o}/addin/icon-32.png"/><bt:Image id="Icon80" DefaultValue="{o}/addin/icon-80.png"/></bt:Images><bt:Urls><bt:Url id="PaneUrl" DefaultValue="{o}/addin/taskpane.html"/></bt:Urls><bt:ShortStrings><bt:String id="GroupLabel" DefaultValue="Armina"/><bt:String id="ButtonLabel" DefaultValue="Armina Properties"/></bt:ShortStrings><bt:LongStrings><bt:String id="ButtonTip" DefaultValue="Manage property email folders and follow-up flags."/></bt:LongStrings></Resources>'''
def hosts(pin=False):
 return f'''<Hosts><Host xsi:type="MailHost"><DesktopFormFactor><FunctionFile resid="PaneUrl"/><ExtensionPoint xsi:type="MessageReadCommandSurface"><OfficeTab id="TabDefault"><Group id="ArminaGroup"><Label resid="GroupLabel"/><Control xsi:type="Button" id="ArminaPropertiesButton"><Label resid="ButtonLabel"/><Supertip><Title resid="ButtonLabel"/><Description resid="ButtonTip"/></Supertip><Icon><bt:Image size="16" resid="Icon16"/><bt:Image size="32" resid="Icon32"/><bt:Image size="80" resid="Icon80"/></Icon><Action xsi:type="ShowTaskpane"><SourceLocation resid="PaneUrl"/>{'<SupportsPinning>true</SupportsPinning>' if pin else ''}</Action></Control></Group></OfficeTab></ExtensionPoint></DesktopFormFactor></Host></Hosts>'''
text=f'''<?xml version="1.0" encoding="UTF-8"?>
<OfficeApp xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0" xsi:type="MailApp">
<Id>63ef5924-7527-4e64-a6b6-8bcdd48a31d4</Id><Version>1.0.0.0</Version><ProviderName>Armina Group</ProviderName><DefaultLocale>en-GB</DefaultLocale><DisplayName DefaultValue="Armina Properties"/><Description DefaultValue="File property emails and manage follow-up flags in Outlook."/><IconUrl DefaultValue="{o}/addin/icon-32.png"/><HighResolutionIconUrl DefaultValue="{o}/addin/icon-80.png"/><SupportUrl DefaultValue="{o}/addin/taskpane.html"/><AppDomains><AppDomain>{o}</AppDomain></AppDomains><Hosts><Host Name="Mailbox"/></Hosts><Requirements><Sets><Set Name="Mailbox" MinVersion="1.5"/></Sets></Requirements><FormSettings><Form xsi:type="ItemRead"><DesktopSettings><SourceLocation DefaultValue="{o}/addin/taskpane.html"/><RequestedHeight>450</RequestedHeight></DesktopSettings></Form></FormSettings><Permissions>ReadItem</Permissions><Rule xsi:type="ItemIs" ItemType="Message" FormType="Read"/><DisableEntityHighlighting>false</DisableEntityHighlighting>
<VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0"><Requirements><bt:Sets DefaultMinVersion="1.3"><bt:Set Name="Mailbox"/></bt:Sets></Requirements>{hosts()}{resources}<VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides/1.1" xsi:type="VersionOverridesV1_1"><Requirements><bt:Sets DefaultMinVersion="1.5"><bt:Set Name="Mailbox"/></bt:Sets></Requirements>{hosts(True)}{resources}</VersionOverrides></VersionOverrides></OfficeApp>'''
Path('manifest.xml').write_text(text)
print('Created manifest.xml for '+origin)
