#!/usr/bin/python

# domaint_new_dir_check.py
#       --copyright--                   Copyright 2007 (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--
#       November 11, 2007       bar
#       November 18, 2007       bar     turn on doxygen
#       November 27, 2007       bar     insert boilerplate copyright
#       May 17, 2008            bar     email adr
#       May 27, 2012            bar     doxygen namespace
#       --eodstamps--
##      \file
#       \namespace              tzpython.domaint_new_dir_check
#
#
#       See the help string.
#
#
#

import  os
import  re
import  sys


def fix_arg(arg) :
    arg = '"\'"' + arg + '"\'"'


    while True :
        ok          = True
        narg        = re.sub(r'^"(.*)"$', r"\1", arg)
        if  narg   != arg :
            ok      = False
            arg     = narg

        narg        = re.sub(r"^'(.*)'$", r"\1", arg)
        if  narg   != arg :
            ok      = False
            arg     = narg

        if  ok :
            break
        pass

    if  re.search(r"\s", arg) :
        arg = '"' + arg + '"'

    return(arg)



if __name__ == '__main__' :
    help_str    = """
domaint_new_dir.py      directory (-s)

Using 4NT's DIR command, check that the files in a directory seem to be the same as the last time we ran this script on the directory.
    """

    import  replace_file
    import  tzlib
    import  TZCommandLineAtFile


    del(sys.argv[0])

    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)

    do_sub      = ""
    dir_to_do   = "."
    quiet       = False

    while True :
        oi  = tzlib.array_find(sys.argv, [ "--do_sub", "--do_subdirectories", "-s"] )
        if  oi < 0 :    break
        del sys.argv[oi]
        do_sub  = "/s"

    while True :
        oi  = tzlib.array_find(sys.argv, [ "--quiet", "-q"] )
        if  oi < 0 :    break
        del sys.argv[oi]
        quiet   = True


    if  len(sys.argv) > 1 :
        print help_str
        sys.exit(101)

    if  sys.argv :
        dir_to_do   = fix_arg(sys.argv[0])

    fi  = os.popen('dir  /m /k /h %s %s' % ( do_sub, dir_to_do ) )
    dl  = fi.read()
    if  fi.close() != None :
        raise "Cannot run dir on " + dir_to_do

    dl  = re.sub(r"\r", "", dl)
    dre = re.compile(r"^.*\bdomaint_new_dir_check\b.*$", re.MULTILINE)
    dl  = dre.sub("", dl)
    dl  = re.sub(r"\n+", "\n", dl)

    fn  = os.path.join(dir_to_do, "domaint_new_dir_check.cmp")

    fd  = ""
    if  os.path.isfile(fn) :
        fi  = open(fn, "r")
        fd  = fi.read()
        fi.close()


    if  fd != dl :
        tfn = fn + ".tmp"
        fo  = open(tfn, "w")
        fo.write(dl)
        fo.close()

        replace_file.replace_file(fn, tfn, fn + ".bak")

        if  not quiet :
            print fn, "changed!"

        sys.exit(1)

    if  not quiet :
        print "No change to", fn

    sys.exit(0)

#
#
#
# eof
