#! /usr/bin/ensim-python
# --------------------------------------------------------------------------
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# --------------------------------------------------------------------------

import ensimapplpath
import sys
import getopt
import re

sys.path.insert(0, '/usr/lib/opcenter')
import appsvcquery
appsvcquery.initialise(usefrontend=0)

BADCHARS='[ \@\#\!\%\&\;\`\'\\\"\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\n\\r]'
badchars = re.compile(BADCHARS)

def get_bad_string(str):
    # we want to disallow shell special characters, for security reason.
    # we want to prevent malicious users from performing bad operations
    mybadchars = ''
    matchlist = badchars.findall(str)
 
    # if there is no exception, we return everything
    if matchlist:
	mybadchars = "%s" % matchlist
    return mybadchars

def usage():
    strusage = '''
Usage: /usr/bin/svcquery -h
       /usr/bin/svcquery -a
       /usr/bin/svcquery -l
       /usr/bin/svcquery -i service_name\n'''

    sys.stderr.write(strusage)

def printList(mylist):
    for i in mylist:
        sys.stdout.write("%s\n" % i)  

def listall():
    mylist = appsvcquery.getAllServicesList()
    printList(mylist)

def listApplianceServices():
    mylist = appsvcquery.getAppServicesList()
    printList(mylist)    

if __name__=='__main__':
    try:
        options, args = getopt.getopt(sys.argv[1:], "hali:")

        optiondict = {}
        for option,value  in options:
            optiondict[option] = value
        
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)

    if '-h' in optiondict.keys():
        usage()
        sys.exit(0)

    if '-a' in optiondict.keys():
        listall()
        sys.exit(0)
        
    if '-l' in optiondict.keys():
        listApplianceServices()
        sys.exit(0)
    
    if '-i' in optiondict.keys():
        svc = optiondict['-i']
        if not svc:
            usage()
            sys.exit(2)
        # paranoid: check the service name
        badchars = get_bad_string(svc)
        if badchars:
            sys.stderr.write("The command contains illegal characters: %s \n" % badchars)
            sys.exit(1)
            
        dict = appsvcquery.getSvcAttrDict(svc)
        for i in dict.keys():
            sys.stdout.write('%s="%s"\n' % (i, dict[i]))
        sys.exit(0)

    # compatibility until shaw changes this to -a
    if not optiondict.keys():
        listall()
        sys.exit(0)
