#!/usr/bin/python

# copyfiles.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--
#       July 11, 2006           bar
#       July 28, 2006           bar     fix commondir/ logic
#                                       set file date/time to original
#       August 8, 2006          bar     generisize this script
#       November 18, 2007       bar     turn on doxygen
#       November 27, 2007       bar     insert boilerplate copyright
#       May 17, 2008            bar     email adr
#       November 29, 2011       bar     pyflake cleanup
#       May 27, 2012            bar     doxygen namespace
#       --eodstamps--
##      \file
#       \namespace              tzpython.copyfiles
#
#
#       Copy files listed one per line to equivalent directories under the current directory.
#
#

#
#
#

import  os
import  shutil
import  string
import  sys


def nop(s) :
    return(s)


def do_deslash(s) :
    return(s.replace('\\', '/'))



if __name__ == '__main__' :
    """
copyfiles.py            Copy files listed in a text file to equivalent places in made directories below the target.

copyfiles.py            common_directory    .   <card.m3u

Copy files listed one per line in 'card.m3u' to equivalent directories (ignoring leading 'common_directory') under '.'.

    """

    import  TZCommandLineAtFile


    del(sys.argv[0])

    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)

    if  len(sys.argv) != 2 :
        print "Please tell me the common directory and the target directory. (e.g. I:\music and . )"
        sys.exit(101)

    lower   = nop

    lower   = string.lower

    deslash = nop
    deslash = do_deslash

    common  = sys.argv[0]
    to_dir  = sys.argv[1]

    if  common.endswith('\\') or common.endswith('/') :
        common  = common[:-1]

    lcommon = deslash(lower(common))

    err     = False
    fns     = []
    while   True :

        fn  = sys.stdin.readline()
        if  not fn :
            break

        fn  = fn.strip()
        if  len(fn) > 0 :
            lfn = deslash(lower(fn))

            if  not lfn.startswith(lcommon) :
                print "Does not start with common_directory:", lfn, lcommon
                err = True

            fns.append(fn)
        pass

    if  err :
        sys.exit(102)

    err_files   = []

    for fn in fns :

        cfn = fn[len(common):]
        if  cfn.startswith('\\') or cfn.startswith('/') :
            cfn  = cfn[1:]

        ofn = os.path.join(to_dir, cfn)

        od  = os.path.split(ofn)[0]
        if  not os.path.isdir(od) :
            print "making", od
            os.makedirs(od)
            fd  = os.path.split(fn)[0]
            # python or xp cannot: os.utime(od, ( os.path.getatime(fd), os.path.getmtime(fd) ) )

        if  not os.path.isfile(ofn) :
            print "copying", fn, "to", ofn
            try :
                shutil.copyfile(fn, ofn)
                os.utime(ofn, ( os.path.getatime(fn), os.path.getmtime(fn) ) )
            except  IOError :
                err_files.append(ofn)
            pass
        pass

    if  len(err_files) != 0 :
        print "I had trouble with these files!"
        for fn in err_files :
            print fn
        sys.exit(99)

    pass

#
#
#
# eof
