#!/usr/bin/python
# -*- coding: UTF-8 -*-

import getopt


from svmon_client import report

from svmon_client import operating_system_parser
from svmon_client import services
from svmon_client import json_operations

def main(argv):
    
    try:
      opts, args = getopt.getopt(argv,'h:t:S:THVpd:',['help','version','site=','test',\
		'host=','tag=','print','dump','type=','list-service-type','show-config'])
    except getopt.GetoptError:
        print (help_info())
        sys.exit(0)
    if len(opts) == 0:
        print (help_info())
        sys.exit(0)
    svmonreport = report.SVMONReport()
    print_service = 0
    save = 0
    readfile =0
    filename=""
    for opt, arg in opts:
#        print opt,arg
        if opt in ['-H', '--help']:
            print (help_info())
            sys.exit(0)
        if opt in ['-V','--version']:
            print (version_info())
            sys.exit(0)
        if opt in ['-S', '--site']:
            svmonreport.set_site(arg)
        if opt in ['-h','--host']:
            svmonreport.set_host(arg)
        if opt in ['-t','--type']:
            #print(arg)
            #print(type(arg))
            if services.in_service_list(arg):
            	svmonreport.set_service_type(arg)
            else:
                print("Such service type is not supported, the supported list gives:")
                exit(0)
        #if opt in ['-v','--tag']:
        #    tags.append(arg)
        if opt in ['-p','--print']:
            print_service = 1
        if opt in ['-T','--test']:
            svmonreport.set_site("SITE")
            svmonreport.set_host("HOST")
            svmonreport.set_service_type("svmon_client")
            svmonreport.set_operating_system(operating_system_parser.get_os_from_file())
            svmonreport.refresh_service_name_and_tag()
            reports= svmonreport.print_report()

            if len(reports) > 0:
                for item in reports:
                    print(item)
            else:
                print("reporting zero service components")


            print("You have successfully installed svmon client")
            exit(0)

        if opt in ['-d', '--dump']:
            save=1
        if opt in ['--list-service-type']:
            print(service_type_info())
        if opt in ['--show-config']:
            svmonreport.print_config_file()


     	       
    if save == 1:
        print("Saving configurations ....")
        svmonreport.save_to_json()



    if print_service == 1:
        svmonreport.set_operating_system(operating_system_parser.get_os_from_file())
        svmonreport.refresh_service_name_and_tag()
        reports = svmonreport.print_report()
        if len(reports) > 0:
            for item in reports:
                print(item)
        else:
            print("reporting zero service components in printing")
            exit(0)



         
        

def help_info():
    return '''    
    SVMON client --  Service Versions Monitoring
    SVMON collects information of softwares in EUDAT services
    and their components. SVMON client would supports following
    options.

      -H or --help    Help information
      -V or --version SVMON client version
      -S or --site    Declear the host site
      -T or --test    Check that all version are collected properly without sending data to the server
      -h or --host    Declear the host name
      -s or --service Declear the service
      -t or --type    Declear the service type
      -v or --tag     Declear the Tag at site. By default, it will read from "config.json"; if it can
                      fetch no information from the configuration file, then the client automatically
                      calls its internal functions to get the tag of the service component(s).
      -p or --print   Print the collected info to the stardard output
      -d or --dump    Save configurations to a file in JSON format, defualt "svmon_client/config.json".
      --list-service-type List all supported service types
    
    This client supports three ways of inputing parameters. It  collects 
    information from options; if no such options are given, it will goes 
    into the file when the specified or default file will be used; if no
    paramters fills, it finally searches in sqlite database. And the client 
    also collects operating system information, it will call the internal 
    functions 
      
     '''

def service_type_info():
    return '''
Currently, we are supporting following service types:

b2safe
gitlab
svmon
b2access
b2find
b2handle
b2drop
dpmt
eudat_website
b2gether
gocdb
b2access

Please contact us if you plan to add other services.
    '''

def version_info():
    return "This is SVMON client version 1.0.1\n"

if __name__=="__main__":
    import sys
 #   print sys.version
    main(sys.argv[1:])
    

