#!/usr/bin/python
#
# written by Nick Shin - nshin@estss.com
# created Oct 5 2010 and placed in the public domain
#
# this file can be found at www.estss.com/opensource/cheatsheet.php
#
#
# to build XULrunner skeleton files:
#     python python_xul_generator.py <application_name>
#
# to run XULrunner generated application:
#     xulrunner <application_name>/<application_name>.ini
#
#
#
#
# the following folder contains your application code:
#     <application_name>/chrome/content/
#
#
#
#
# NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
# 
# This is a reference implementation. You are free to copy,
# modify, or redistribute.


# XULRunner is basically a javascript/TK interpreter in XML'vish


# fill out the following:
VENDOR   = 'ESTSS'
APPNAME  = 'demo'               # no whitespace here
YOURNAME = 'Nick Shin'
EMAIL    = 'nshin@estss.com'

# 'defaultChromeURI'            # Specifies the default window to open when the application is launched. 
# 'defaultChromeFeatures'       # Specifies the features passed to window.open() when the main application window is opened. 
# 'singletonWindowType'         # Allows configuring the application to allow only one instance at a time. 
toolkit = 'defaultChromeURI'

GECKOMIN = '1.8'
GECKOMAX = '1.10.0.*'

# will assume app is version 1.0
# will use today's date as build ID
# and gecko versions min: 1.8 max: 1.10.0.*


# start generating the files we need
# COMMON {{{
# ========================================

import getopt, sys, os
from datetime import date

datenow = date.today()

def usage():
    print "Usage: " + sys.argv[0] + " <application>"
    print """
\t<application> is the XULRunner application name

\toptions: -h for help"

NOTE: this currently will stomp on existing files"
"""


def chk_options():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "version"])
    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(2)

    for o, a in opts:
        if o in ("-h"):
            usage()
            sys.exit()

    if len(args) < 1:
        print "*** missing application name to generate XULRunner files"
        usage()
        sys.exit(2)

    global APPNAME
    APPNAME = args[0]


# COMMON }}}
# FOLDERS {{{
# ========================================

def chk_folders():
    paths = [   APPNAME + '/chrome/content',
                APPNAME + '/chrome/skin',
                APPNAME + '/chrome/locale',
                APPNAME + '/defaults/preferences' ]

    for path in paths:
        if not os.path.exists(path):
            os.makedirs(path)


# FOLDERS }}}
# INI {{{
# ========================================

def gen_ini():
    app_ini = '[App]' + \
            '\nVendor=' + VENDOR + \
            '\nName=' + APPNAME + \
            '\nVersion=1.0' + \
            '\nBuildID=' + datenow.strftime('%Y%m%d') + \
            '\nCopyright=Copyright (c) ' + datenow.strftime('%Y ') + YOURNAME + \
            '\nID=' + EMAIL + \
            '\n\n[Gecko]' + \
            '\nMinVersion=' + GECKOMIN + \
            '\nMaxVersion=' + GECKOMAX + '\n'

    file_ini = APPNAME + '/' + APPNAME + '.ini'
    with open( file_ini, 'w' ) as f:
        f.write(app_ini)
        f.close()


# INI }}}
# MANIFEST {{{
# ========================================

def gen_manifest():
    app_manifest = 'content ' + APPNAME + ' file:content/' + \
                    '\nskin ' + APPNAME + ' classic/1.0 file:skin/' + \
                    '\nlocale ' + APPNAME + ' en-US file:locale/\n'

    file_manifest = APPNAME + '/chrome/chrome.manifest'
    with open( file_manifest, 'w' ) as f:
        f.write(app_manifest)
        f.close()


# MANIFEST }}}
# PREF {{{
# ========================================

def gen_pref():
    app_pref = 'pref("toolkit.' + toolkit + '", "chrome://' + APPNAME + '/content/' + APPNAME + '.xul");\n'

    file_pref = APPNAME + '/defaults/preferences/prefs.js'
    with open( file_pref, 'w' ) as f:
        f.write(app_pref)
        f.close()


# PREF }}}
# CSS {{{
# ========================================

def gen_css():
    app_css = '/* global skin --------------------------------------------------- */' + \
            '\n@import url(chrome://global/skin/);' + \
            '\n\n/* ' + APPNAME + ' skin ------------------------------------------------------- */' + \
            '\n#example1 {' + \
            '\n  list-style-image: url(chrome://' + APPNAME + '/skin/example1.png);' + \
            '\n  -moz-box-orient: vertical;' + \
            '\n}\n' + \
            '\n#example2 {' + \
            '\n  list-style-image: url(chrome://' + APPNAME + '/skin/example2.png);' + \
            '\n  -moz-box-orient: vertical;' + \
            '\n}\n\n'

    file_css = APPNAME + '/chrome/skin/' + APPNAME + '.css'
    with open( file_css, 'w' ) as f:
        f.write(app_css)
        f.close()


# CSS }}}
# DTD {{{
# ========================================

def gen_dtd():
    app_dtd = '<!ENTITY ' + APPNAME + '.title "' + APPNAME + '">\n'

    file_dtd = APPNAME + '/chrome/locale/' + APPNAME + '.dtd'
    with open( file_dtd, 'w' ) as f:
        f.write(app_dtd)
        f.close()


# DTD }}}
# XUL {{{
# ========================================

def gen_xul():
    app_xul = '<?xml version="1.0"?>' + \
            '\n<?xml-stylesheet href="chrome://' + APPNAME + '/skin/' + APPNAME + '.css" type="text/css"?>' + \
            '\n\n<!DOCTYPE window SYSTEM "chrome://' + APPNAME + '/locale/' + APPNAME + '.dtd">' + \
            '\n\n<window id="' + APPNAME + '"' + \
            '\n    title="&' + APPNAME + '.title;"' + \
            '\n    width="300" height="300"' + \
            '\n    xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">' + \
            '\n <caption label="Hello World"/>' + \
            '\n</window>\n'

    file_xul = APPNAME + '/chrome/content/' + APPNAME + '.xul'
    with open( file_xul, 'w' ) as f:
        f.write(app_xul)
        f.close()


# XUL }}}
# README {{{
# ========================================

def gen_readme():
    app_readme = """
This application was generated by the python_xul_generator.py script
which can be found at www.estss.com/opensource/cheatsheet.php
written by Nick Shin - nshin@estss.com


NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
You are free to copy, modify, or redistribute.


XULRunner is basically a javascript/TK interpreter in XML'vish
"""
    app_readme += '\nTo run the XULrunner application, type in the following command:' + \
            '\n\txulrunner ' + APPNAME + '/' + APPNAME + '.ini' + \
            '\n\nThe application code is located here:' + \
            '\n\t' + APPNAME + '/chrome/content/' + \
            '\n\nEnjoy!\n\n'

    file_readme = APPNAME + '/README'
    with open( file_readme, 'w' ) as f:
        f.write(app_readme)
        f.close()


# README }}}
# MAIN {{{
# ========================================

def main():
    chk_options()
    chk_folders()
    gen_ini()
    gen_manifest()
    gen_pref()
    gen_css()
    gen_dtd()
    gen_xul()
    gen_readme()


if __name__ == "__main__":
    main()


# MAIN }}}
# ========================================