#!/usr/bin/env python

# -----------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
# -----------------------------------------------------------------------



import os
import sys
import getopt
import subprocess
import re
import string

global DUCC_HOME
global autostart


DUCC_HOME = os.environ['HOME'] + '/ducc_runtime'
autostart = True


def usage(msg):
    if (msg != None):
        print msg

    print 'Runs a ping-only service on the indicated endpoint.'
    print ''
    print 'Usage:'
    print ''
    print '   customsvc options'
    print ''
    print '   -r, --runtime <ducc_home>   The location of DUCC_HOME.  Default is ', DUCC_HOME
    print '   --na                        No autostart  Default is', (not autostart)
    print '   -h, -? --help               This message'
    print ''
    print 'This always runs as a CUSTOM pinger.'
    print ''
    sys.exit(0)

def format_classpath(cp):
    toks = cp.split(':')
    for c in toks:
        print '    ', c

def main():

    global DUCC_HOME
    global autostart
    global log_pinger

    try:
        opts, args  = getopt.getopt(sys.argv[1:], 'r:?h', ['na', 'runtime=', 'help'])
    except:
        print "Unknown option"
        usage(None)
    
    for ( o, a ) in opts:
        if o in ('-r', '--runtime') :
            DUCC_HOME = a
        elif o in ('--na'):
            autostart = False
        elif o in ('-?', '-h'):
            usage(None)
        else:
            usage(None)

    DUCCLIB = os.environ['HOME'] + '/ducclib'

    project_home = os.path.abspath('..')

    if(autostart):
        description = 'Custom Service Type auto-start'
    else:
        description = 'Custom Service Type on-demand'
        
    props = {
        'description'              : description,
        'service_request_endpoint' : 'CUSTOM:localhost:7175',

        'service_ping_dolog'       : 'false',
        'autostart'                : str(autostart),
        'service_ping_class'       : 'CustomPing',
        'service_ping_classpath'   : os.getcwd(),
        'service_ping_timeout'     : '10000',
        'process_executable'       : 'service',
        'process_executable_args'  : '7175'
        
        }
    
    print 'Service setup using'
    print '   project_home  : ' + project_home
    print '   DUCC_HOME     : ' + DUCC_HOME
    print '   autostart     : ' + str(autostart)
    print '   CLASSPATH:'
    format_classpath(props['service_ping_classpath'])

    print 'Register new service'
    CMD = DUCC_HOME + '/bin/ducc_services --register '
    for k in props.keys():
        v = props[k]
        if ( v == None ):
            CMD = CMD + '--' + k + ' '
        else:
            CMD = CMD + '--' + k + ' ' + "'" + props[k] + "' "
    print CMD
    os.system(CMD)
    return

main()
