#!/usr/bin/python

# set_version_htm_sha1.py
#       --copyright--                   Copyright 2009 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       October 30, 2007        bar     spin to \tzpython
#       November 18, 2007       bar     turn on doxygen
#       November 21, 2007       bar     import-able
#       November 27, 2007       bar     insert boilerplate copyright
#       April 14, 2008          bar     fix an if statement so non-version.htm file works
#       May 17, 2008            bar     email adr
#       June 4, 2009            bar     able to run under python 2.6.2, with hashlib
#       October 1, 2010         bar     use tzlib
#                                       expose sha1 subroutine
#       November 11, 2010       bar     fix cmd line param
#       April 19, 2011          bar     able to spec the directory the sha1 files are in (default: .)
#       November 12, 2011       bar     take the s out of the url
#       --eodstamps--
##      \file
#
#
#       Put the SHA1 in version.htm
#       version.htm is used by phone_homer.py
#
#


import  os
import  re
import  sys

try :
    import  hashlib
except      ImportError       :
    import  md5
    import  sha
    class   a_hashlib(object) :
        def md5(me, s  = "")  :
            return(md5.new(s))
        def sha1(me, s = "")  :
            return(sha.new(s))
        pass
    hashlib = a_hashlib()


import  phone_homer
import  replace_file
import  tzlib




def fix_file(fname, regx, to) :
    """ Use the given regx to sub() hits to 'to' in the given, named file (replacing the file, creating a .bak of the file). """

    fd  = tzlib.read_whole_binary_file(fname)

    if  not regx.search(fd) :
        raise "Version not found in " + fname

    nd  = regx.sub(to, fd)

    tfname  = fname + ".tmp"
    tzlib.write_whole_binary_file(tfname, nd)
    replace_file.replace_file(fname, tfname, fname + ".bak")



def string_sha1(str) :
    """ Return the lower case, hex, SHA1 of the given string. """

    return(hashlib.sha1(str).hexdigest().lower())



def file_sha1(fname) :
    """ Return the lower case, hex, SHA1 of the given file. """

    return(string_sha1(tzlib.read_whole_binary_file(fname)))



if  __name__ == '__main__' :

    program_name    = sys.argv.pop(0)

    vhtm_fn         = "version.htm"
    if  len(sys.argv) :
        vhtm_fn     = sys.argv.pop(0)

    idir            = "."
    if  len(sys.argv) :
        idir        = sys.argv.pop(0)

    vhtm            = tzlib.read_whole_text_file(vhtm_fn)       # read the version.htm file

    gfiles          = phone_homer.files_re.findall(vhtm)
    if  len(gfiles) == 0 :
        raise("Did not find files in " + vhtm_fn)

    for sha_fn in gfiles :

        ( cs, fn )  = sha_fn

        sha1        = file_sha1(os.path.join(idir, os.path.basename(fn)))

        rss         = phone_homer.sha1_sub_re_str % ( re.escape(fn) )
        tos         = phone_homer.sha1_sub_str    % ( sha1, fn      )
        regx        = re.compile(rss, re.IGNORECASE)
        vhtm        = regx.sub(tos, vhtm)


    tfn = vhtm_fn + ".tmp"
    tzlib.write_whole_binary_file(tfn, vhtm)
    replace_file.replace_file(vhtm_fn, tfn, vhtm_fn + ".bak")


#
#
#
# eof

