#!/usr/bin/env python

# This script copys the camel snapshot xsd files that publish to the m2 repository to the
# camel web site
import fnmatch
import os
import sys
import shutil

def copy_snapshot_xsds(src, dest):
    for root, dirs, files in os.walk(src):
        files.sort();
        filename = 'NULL'
        dirname = '';
        for file in files:
            if not fnmatch.fnmatch(file, '*.xsd'):
                continue
            filename = file
        # get the lastest xsd file
        if filename == 'NULL':
            continue
        # skip the time stamp, take of the two last - get the target file name
        list = filename.split('-');
        # check if the file's name is end with SNAPSHOT
        # or removing the last two time stamps
        if list.pop()!= 'SNAPSHOT.xsd':
            list.pop();
        list.append('SNAPSHOT.xsd')
        srcfile= os.path.join(root,filename)
        destfile = os.path.join(dest,'-'.join(list))
        shutil.copyfile(srcfile,destfile)


def main():
    if len(sys.argv) < 3:
        print 'copy_snapshot_xsd.py SRC DES'\
              'copy the last snaport xsd file from SRC directory to DES directory'\
              'with the file name end with SNAPSHOT.xsd'
        sys.exit();
    copy_snapshot_xsds(sys.argv[1], sys.argv[2]);
    

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