#!/usr/bin/python

import sys

# This is a simple script to build an aggregated SOAP server with AWS

pcks = sys.argv[1:]

# Initialize the list of CB to aggregate passed on the command line

if len(pcks) == 0:
    print("missing Ada unit service names in argument")
    exit

# First: write the spec

with open('agg_server_cb.ads', 'w') as f:
    f.write("""
--  DO NOT EDIT : generated by awsasc

with AWS.Response;
with AWS.Status;

with SOAP.Dispatchers.Callback;
with SOAP.Message.Payload;
with SOAP.WSDL.Schema;

package Agg_Server_CB is

   use AWS;
   use SOAP;

   pragma Style_Checks (Off);

   type Handler is new SOAP.Dispatchers.Callback.Handler with null record;

   overriding function Schema
     (Dispatcher : Handler;
      SOAPAction : String)
      return WSDL.Schema.Definition;

   function Create
     (HTTP_Callback : AWS.Response.Callback) return Handler;
   --  Returns an handler whose SOAP_Callback is the one below

   function SOAP_CB
     (SOAPAction : String;
      Payload    : Message.Payload.Object;
      Request    : AWS.Status.Data)
      return Response.Data;

end Agg_Server_CB;
""")

# Second: write the body

with open('agg_server_cb.adb', 'w') as f:

    f.write("--  DO NOT EDIT : generated by awsasc\n\n")
    f.write("with Ada.Strings.Unbounded;\n\n")
    f.write("with SOAP.Message.Response.Error;\n")
    f.write("with SOAP.Message.XML;\n\n")

    for cb in pcks:
        f.write("with %s.CB;\n" % cb)

    f.write("""
package body Agg_Server_CB is

   use Ada.Strings.Unbounded;

   use SOAP;

   ------------
   -- Create --
   ------------

   function Create
     (HTTP_Callback : AWS.Response.Callback) return Handler is
   begin
      return
        (SOAP.Dispatchers.Callback.Create
          (HTTP_Callback, SOAP_CB'Access,
           SOAP.WSDL.Schema.Empty) with null record);
   end Create;

   ------------
   -- Schema --
   ------------

   overriding function Schema
     (Dispatcher : Handler;
      SOAPAction : String)
      return WSDL.Schema.Definition is
   begin""")

    for cb in pcks:
        f.write("""
      %s %s.CB.Is_SOAPAction_Defined (SOAPAction) then
         return %s.Schema;
        """ % ("if" if cb == pcks[0] else "elsif", cb, cb))

    f.write("""
      else
         return SOAP.WSDL.Schema.Empty;
      end if;
   end Schema;
""")

    f.write("""
   -------------
   -- SOAP_CB --
   -------------

   function SOAP_CB
     (SOAPAction : String;
      Payload    : Message.Payload.Object;
      Request    : AWS.Status.Data)
      return Response.Data is
   begin
""")

    for cb in pcks:
        f.write("""
      %s %s.CB.Is_SOAPAction_Defined (SOAPAction) then
         return %s.CB.SOAP_CB (SOAPAction, Payload, Request);
      """ % ("if" if cb == pcks[0] else "elsif", cb, cb))

    f.write("""
      else
         return Message.Response.Build
           (Message.Response.Error.Build
             (Message.Response.Error.Client,
              "Wrong SOAP action " & SOAPAction));
      end if;
    end SOAP_CB;

end Agg_Server_CB;
""")
