#!/usr/bin/env python
#******************************************************************************
# 
#  Project:  2009 Web Map Server Benchmarking
#  Purpose:  Create jmeter .xml control file from a template via substitution.
#  Author:   Frank Warmerdam, warmerdam@pobox.com
# 
#******************************************************************************
#  Copyright (c) 2009, Frank Warmerdam
# 
#  Permission is hereby granted, free of charge, to any person obtaining a
#  copy of this software and associated documentation files (the "Software"),
#  to deal in the Software without restriction, including without limitation
#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
#  and/or sell copies of the Software, and to permit persons to whom the
#  Software is furnished to do so, subject to the following conditions:
# 
#  The above copyright notice and this permission notice shall be included
#  in all copies or substantial portions of the Software.
# 
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#  DEALINGS IN THE SOFTWARE.
#******************************************************************************

import string
import sys

# =============================================================================
def Usage():
    print 'Usage: mkjbenchxml.py -l layer [-b cgi] [-m map] output.xml'
    print
    print 'eg.  mkjbenchxml.py -l topo_bigtiff -b mapserv560beta1.fcgi \ '
    print '            -m /opt/benchmarking/mapserver/raster.map bigtiff_fcgi.xml'
    sys.exit(1)

# =============================================================================
def Replace( text, target, replacement ):
    
    offset = string.find(text,target)
    while offset != -1:
        text = text[:offset] + replacement + text[offset+len(target):]
        offset = string.find(text,target)

    return text

# =============================================================================

if __name__ == '__main__':

    argv = sys.argv
    
    layer = None
    map = '/opt/benchmarking/mapserver/raster.map'
    binary = 'mapserv.fcgi'
    template = 'template.xml'
    output_file = None
    
    # Parse command line arguments.
    
    i = 1
    while i < len(argv):
        arg = argv[i]

        if arg == '-wkt' or arg == '-pretty_wkt' or arg == '-proj4' \
           or arg == '-postgis' or arg == '-xml':
            output_format = arg

        elif arg[:5] == '-skip':
            report_error = 0
            
        elif arg == '-l' and i < len(argv)-1:
            i = i + 1
            layer = argv[i]
            
        elif arg == '-m' and i < len(argv)-1:
            i = i + 1
            map = argv[i]
            
        elif arg == '-b' and i < len(argv)-1:
            i = i + 1
            binary = argv[i]
            
        elif arg[0] == '-':
            Usage()

        elif output_file is None:
            output_file = arg

        else:
            Usage()

        i = i + 1

if layer is None or output_file is None:
    Usage()

    
# Load the template into memory.

text = open(template).read()

text = Replace(text,'@@@MAP@@@',map)
text = Replace(text,'@@@LAYER@@@',layer)
text = Replace(text,'@@@CGI@@@',binary)

open(output_file,'w').write( text )

