#!/usr/bin/python3
# encoding: utf-8
'''
lunagen -- "Lunatics!" Project Release Site Generator

LunaGen generates a small static HTML website based on YAML
data documents containing the key release information. This
generates an index.html file containing the episode lists
for all series, individual release pages for each episode,
and additional pages for character list and story background.

This is meant to be a fan-focused site.

It also contains affiliate link, sponsor, advertising, and
other fundraising elements.

@author:     Terry Hancock

@copyright:  2019 Anansi Spaceworks. 

@license:    GNU General Public License, version 2.0 or later. (Python code)
             Creative Commons Attribution-ShareAlike, version 3.0 or later. (Website Templates).

@contact:    digitante@gmail.com    
'''

import os, sys
from optparse import OptionParser

import lunagen            

def main(argv=None):
    '''Command line options.'''

    program_name = os.path.basename(sys.argv[0])
    program_version = "v0.1"
    program_build_date = "%s" % lunagen.Config.__updated__

    program_version_string = '%%prog %s (%s)' % (program_version, program_build_date)
    program_longdesc = '''\
LunaGen is a static HTML website generator designed for releasing
a series of episodes (technically: a series of series of episodes).

Data is authored using the YAML structured data language, which allows
for episode metadata and descriptions to be written in a human-friendly
format, which is then formatted into HTML using Jinja2 templates.

Once generated, the site is static and can simply be uploaded to a
standard web server with minimal or no configuration (like a static web host).

It was originally created to generate the release pages for
Anansi Spaceworks' "Lunatics!" series.

For details, please see the 'examples' and 'doc' directories.
    '''
    program_license = "Copyright 2019 Terry Hancock (Anansi Spaceworks)                                            \
                Licensed under the GNU General Public License, version 2.0\n"

    if argv is None:
        argv = sys.argv[1:]
    #try:
    # setup option parser
    parser = OptionParser(version=program_version_string, epilog=program_longdesc, description=program_license)
    parser.add_option("-i", "--in", dest="src", help="set input path [default: %default]", metavar="FILE")
    parser.add_option("-o", "--out", dest="tgt", help="set output path [default: %default]", metavar="FILE")
    parser.add_option("-v", "--verbose", dest="verbose", action="count", help="set verbosity level [default: %default]")
    parser.add_option("-S", "--seed", dest="seed", help="set random number seed [default: None]", type="int")


    # process options
    (opts, args) = parser.parse_args(argv)

    if opts.verbose:
        verbose = opts.verbose
    else:
        verbose = 0
    
    if verbose:
        print("Opts = ", opts)
        print("Args = ", args)
    
    if opts.src:
        src = opts.src
    elif args:
        src = args.pop(0)
    else:
        src = './site'
        
    if opts.tgt:
        tgt = opts.tgt
    elif args:
        tgt = args.pop(0)
    else:
        tgt = './build'
        
    if verbose: print("Source = ", src)
    if verbose: print("Target = ", tgt)
    
    lunagen.LunaGen(src, tgt, verbose, opts.seed).gensite()

if __name__ == "__main__":
    # if lunagen.Config.DEBUG:
    #     #sys.argv.append("-h")
    #     sys.argv.append("-v")
    # if lunagen.Config.TESTRUN:
    #     import doctest
    #     doctest.testmod()
    # if lunagen.Config.PROFILE:
    #     import cProfile
    #     import pstats
    #     profile_filename = 'lunagen_profile.txt'
    #     cProfile.run('main()', profile_filename)
    #     statsfile = open("profile_stats.txt", "wb")
    #     p = pstats.Stats(profile_filename, stream=statsfile)
    #     stats = p.strip_dirs().sort_stats('cumulative')
    #     stats.print_stats()
    #     statsfile.close()
    #     sys.exit(0)
    
    
    sys.exit(main())