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

# Copyright (c) 2010 Xavid Pretzer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#     The above copyright notice and this permission notice shall be
#     included in all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#     OTHER DEALINGS IN THE SOFTWARE.

# Not necessary on scripts, but it is on Linerva
import site
site.addsitedir('/mit/xavid/lib/python2.6/site-packages')

from lunardate import LunarDate

import sys,os
cgip = os.environ.get('GATEWAY_INTERFACE',False) and len(sys.argv) == 1

url= 'http://fireflies.xavid.us/2010/01/09/o-auspicious-day/'

if cgip:
    import cgi
    fs = cgi.FieldStorage()

    def format(t,en,ja):
        s = t.toSolarDate()
        return u'''Content-Type: text/html; charset=utf-8

<html>
<head>
<title>Rokuyō: %s (%s)</title>
</head>
<body>
<p>
Gregorian date %s (lunar date %s) is:<h1><a href="%s#%s">%s (%s)</a></h1>
</p>

<form>
Year: <input name="year" value="%s" size="4" /><br />
Month: <input name="month" value="%s" size="2" /><br />
Day: <input name="day" value="%s" size="2" /><br />
<input type="submit" value="Calculate" />
</form>

<p>
Written by Kihou (at xavid dot us). Powered by <a href="http://scripts.mit.edu/">scripts.mit.edu</a>. <a href="rokuyou.py">Source</a>
</p>
</body>
</html>''' % (en,ja,s.isoformat(),"%s%s-%s"%(t.month,"'"if t.isLeapMonth else '',t.day),url,ja,en,ja,s.year,s.month,s.day)
    day = fs.getfirst('day',None)
    month = fs.getfirst('month',None)
    year = fs.getfirst('year',None)
else:
    day = month = year = None
    for a in sys.argv:
        if not a.startswith('-') and a.count('-') == 2:
            year,month,day = a.split('-')
            break
    if len(sys.argv) > 1 and '--link' in sys.argv:
        def format(t,en,ja):
            return u'<a href="%s#%s">%s (%s)</a>' % (url,ja,en,ja)
    else:
        def format(t,en,ja):
            return u"%s (%s)" % (en,ja)

rokuyou = {0 : (u'Daian',u'大安'),
           1 : (u'Shakkō',u'赤口'),
           2 : (u'Senshō',u'先勝'),
           3 : (u'Tomobiki',u'友引'),
           4 : (u'Sembu',u'先負'),
           5 : (u'Butsumetsu',u'仏滅')}

if day is not None and month is not None and year is not None:
    t = LunarDate.fromSolarDate(int(year),int(month),int(day))
else:
    t=LunarDate.today()

print format(t,*rokuyou[(t.month+t.day)% 6]).encode('utf-8')
