#!/usr/bin/env python

# This script copies maven release files that publish to the m2 repository to a directory
import fnmatch
import os
import sys
import shutil

def copy_released(src, dest, postfix):
    match = '*' + postfix

    for root, dirs, files in os.walk(src):
        files.sort();
        filename = 'NULL'
        dirname = '';
        for file in files:
            if not fnmatch.fnmatch(file, match):
                continue
            if fnmatch.fnmatch(file, '*-SNAPSHOT*'):
                continue
            filename = file
        # get the lastest xsd file
        if filename == 'NULL':
            continue
        srcfile= os.path.join(root,filename)
        destfile = os.path.join(dest,filename)
        shutil.copyfile(srcfile,destfile)
        # disable the output
        # print 'copy %s to %s' %(srcfile, destfile);


def main():
    if len(sys.argv) < 4:
        print 'copy_released.py SRC DES postfix'\
              'copy the last release file from SRC directory to DES directory which ends with the postfix'
        sys.exit();
    copy_released(sys.argv[1], sys.argv[2], sys.argv[3]);
    

if __name__ == '__main__':
  sys.exit(main())
