#!/usr/bin/env python
#
print("""
################################################################################
#  Given the desirable external WDSL enpoint, the internal WDSL endpoint, and
#  the internal endpoint, this program will generate the data group entries for
#  the F5 Proxy-pass setup in production.
#
#  Please refer to production documents
#  'F5 SAP PI Endpoint Proxy Setup Procedures' for more details
#
################################################################################
# Version:      0.2,
# Date:         February 25 2019
# Author:       Sam Li, yangsec888@gmail.com, x.x.1972
# Usage:
#               $ dg-sap
###############################################################################

""")
import sys
import os
import f5_admin

# Given a standard url, return relative url
def url_to_relative(url):
    url=url.strip()
    if "http://" in url:
        url=url.replace("http://","")
    if "https://" in url:
        url=url.replace("https://","")
    arr_url=url.split("/")
    arr_url.pop(0)
    return "/" + "/".join(arr_url)

# Given a standard url, return protocol headless url
def url_to_headless(url):
    url=url.strip()
    if "https://" in url:
        return url.replace("https://","")
    elif "http://" in url:
        return url.replace("http://","")
    else:
        return url

# check if the url is https
def is_https(url):
    if "https://" in url:
        return True
    else:
        return False

# Given SAP external wsdl and endpoint, return the recode host
def sap_ep_to_host(sap_ep,ext_wsdl):
    sap_ep=sap_ep.strip()
    host=url_to_headless(sap_ep).split(".")[0]
    host_list=list(host)
    host_list.pop()
    if "omm-q" in ext_wsdl:
        host_list.append("q")
    elif "omm-d" in ext_wsdl:
        host_list.append("d")
    else:
        host_list.append("p")
    my_host="".join(host_list)
    return my_host

################################################################################
#                               Main Program
################################################################################
if __name__ == '__main__':
    ext_wsdl = input("Please enter the disirable External WSDL: ")
    pi_wsdl = input("Please enter the PI WSDL: ")
    sap_ep = input("Please enter the SAP Endpoint: ")

    print("You'll need 2 proxy-pass setup in the F5. ")
    print("Prepare for the first data group entry ...")
    key1 = url_to_relative(ext_wsdl)
    val1 = "|" + key1 + "|" + url_to_relative(pi_wsdl) + "&endp_url=" + ext_wsdl + "|" + " " + sap_ep_to_host(sap_ep,ext_wsdl)
    dg1 = "\"" + key1 + "\"" + " := " + "\"" + val1 #+ "\"" + ","
    if is_https(pi_wsdl):
        dg1 = dg1 + "-" + "https" + "\"" + ","
    else:
        dg1 = dg1 + "-" + "http" + "\"" + ","

    print("Prepare for the 2nd data group entry ...")
    key2 = url_to_relative(ext_wsdl).replace("/wsdl","")
    val2 = "|" + key2 + "|" + url_to_headless(sap_ep) + "|" + " " + sap_ep_to_host(sap_ep,ext_wsdl)
    dg2 = "\"" + key2 + "\"" + " := " + "\"" + val2 # + "\"" + ","
    if is_https(ext_wsdl):
        dg2 = dg2 + "-" + "https" + "\"" + ","
    else:
        dg2 = dg2 + "-" + "http" + "\"" + ","

    print("\n",dg1)
    print(dg2,"\n")
    ## Print the reminder
    print("""
All done. Please add the above 2 entries to the proper external data group file. Refer to the Evernote for details.

""")
