import os
import sys

def getParasha():
    if os.popen("which hebcal").read() != "":
        print "WARNING: please install hebcal on this system"
    print "Enter secular calender year of Rosh Hashanah, or q to quit",
    while 1:
        x = sys.stdin.readline()
        if len(x) > 1 and x[0] == "q":
            print "Goodbye!"
            sys.exit()
        try:
            year = int(x)
            if year < 1900:
                print "Did you mean something this century?  Use four digit year"
            else:
                break
        except ValueError:
            print "oops! try again"
    #Ok, have year
    x = os.popen("hebcal 10 1 %i"%year).readlines()[0]
    hebyear = x.split()[-1]
    print "Hebrew year is ",hebyear
    plist = os.popen("hebcal -s %i | grep Parasha"%year).readlines()
    plist += os.popen("hebcal -s %i | grep Parasha"%(year+1)).readlines()

    plist = [x.strip() for x in plist]
    plist =  [x.split(None,2) for x in plist]
    plist = [(x[0],x[2],x[2].lower().replace("-","").replace("'","").replace(" ","")) for x in plist]
    searchlist = [x[2][:4] for x in plist]
    i1  = searchlist.index("bere")
    searchlist = searchlist[i1:]
    plist = plist[i1:]   
    i2 = searchlist.index("haaz")+1
    return hebyear,plist[:i2]



WEBPAGE = """
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
  <title>D'vrei Torah page for the Adas Israel Traditional
Egalitarian Minyan %s</title>


</head>


<body>

<center>
<h2>Welcome to the D'var Torah resource page<br>

for the <br>

Adas Israel Traditional Egalitarian Minyan<br>

%s</h2>

</center>

<font size="+3">
</font>
<p><a href="http://grace.speakeasy.net/~nemeths/" target="_top">
Printable version of this page</a></p>

<p><a href="http://www.dcnemeth.com/resume/">Click
Here</a> for a copy of David's Resume</p>

<p>If you are interested in giving a D'var Torah in the Minyan,
or are looking for the text of a D'var Torah that
someone else has given, you've come to the right place!
&nbsp;&nbsp;

<p> NOTE: New email address!<p>
<p>To reserve your date email David Nemeth at <img src="http://grace.speakeasy.net/~nemeths/email.png"> </a>
or phone at home,&nbsp;202-244-9425. He's happy to talk after
services
too! &nbsp;<br>

<br>

</p>

<p>(Picture of David coming sometime!)<br>

<script type="text/javascript" language="JavaScript" src="http://www.hebcal.com/shabbat/?geo=zip;zip=20008;m=48;cfg=j">
</script>
<noscript><!-- this link seen by people who have JavaScript
turned off -->
<a
href="http://www.hebcal.com/shabbat/?geo=zip;zip=20008;m=48">Shabbat
Candle Lighting times for Washington, DC 20008</a>
courtesy of hebcal.com.
</noscript>

</p>

<p></p>

<p>If you have the text of a D'var you've given in the minyan,
either
this year or from a previous year, we'd like to include it
in the archive. Please send the text to him at <img src="email.png"> </a></p>

Texts of previous minyan D'vrei Torah can be found in the <a href="http://www.dcnemeth.com/dvar/oldtexts/">archive</a>.
<p></p>

<p>
<font size="+2">D'var Torah Resources:</font><br>

<a href="http://www.hebcal.com/">Hebcal.com</a> for
Holiday dates, and weekly parashot. It also has many other resources.<br>

<br>

The Torah in Hebrew and English side by side can be found at <a href="http://www.mechon-mamre.org/p/pt/pt0.htm">www.mechon-mamre.org</a>.
They also have mp3s of the bible being read in Hebrew.
<br>

<br>

Look up <a href="http://www.rashiyomi.com/query1.htm">Rashi</a>
for a given line of Torah at www.rashiyomi.com.<br>

<br>

Click on the name of the Parashat below to see the full text of the
reading in English according to the JPS translation.<br>

<br>

<table style="width: 80%;" border="1" bordercolor="#000000">

  <tbody>

    <tr valign="top">

      <td width="16%">
      <p>Date</p>

      </td>

      <td width="22%">
      <p>Parashat</p>

      </td>

      <td width="25%">
      <p>Speaker</p>

      </td>

      <td width="17%">
      <p>Text* </p>

      </td>

    </tr>
%s


  </tbody>
</table>

</p>


<p style="margin-bottom: 0in;"> </p>


<p>All Copyrights are retained by the authors of the D'vrei Torah.</p>

<p><br>

<br>

</p>

</body>
</html>
""".replace("%","%%").replace("%%s","%s")

TABLEROW = """
    <tr>

      <td sdval="38654" sdnum="9;0;MM/DD/YY" valign="bottom" width="16%">
      <p align="right">%s</p>

      </td>

      <td valign="top" width="27%%">
      <p><a href="http://www.hebcal.com/sedrot/%s.html">%s</a></p>

      </td>

      <td valign="top" width="40%">
      <p>Open</p>

      </td>

      <td valign="top" width="17%">
      <p>
	<!a href="http://www.dcnemeth.com/texts/Bereshit5768.pdf"> <br><!/a>
      </p>

      </td>

    </tr>
""".replace("%","%%").replace("%%s","%s")

hebyear,bits = getParasha()
rows = []
for b in bits:
    rows.append(TABLEROW%(b[0],b[2],b[1]))

output = WEBPAGE%(hebyear,hebyear,"\n".join(rows))
open("index%s.html"%hebyear,"w").write(output)
