#!/usr/bin/perl
#$Id: update_wsdl.pl,v 1.5 2022/02/03 16:32:58 sqko Exp $
use strict;
use warnings;
use Pod::WSDL;
use Sys::Hostname;
my $html_path = shift;
my $cgi_path = '';
my @list = ('report.cgi','cgi_service.cgi','session.cgi');
if ($html_path && $html_path =~ /(.*)\/html\//)
{
    $cgi_path = $1.'/cgi-bin/celltest/';
}
if (@ARGV)
{
    ## Fix for cgi files not together with html files on Ubuntu
    $cgi_path = '/usr/lib/cgi-bin/celltest/';
}
my $host = hostname();
foreach my $f (@list)
{
    my $file = $cgi_path.$f;
    $f =~ /(.*)\.cgi/;
    my $out = $html_path.'/'.$1.'.wsdl';
    next unless -e $file;
    my $url = $file;
    $url =~ s/.*cgi-bin/cgi-bin/;
    $url = 'https://'.$host.'/'.$url;
    my $pod = new Pod::WSDL(
	source => $file, 
	location => $url,
	pretty => 1,
	withDocumentation => 1,
	);
    my $content = $pod->WSDL;
    ## Fix HandlerService line to be unique across multiple servers ##
    $content =~ s/HandlerService/HandlerService$host/;
    open OUT,'>',$out or die "Could not open file $out for writing $!\n";
    print OUT $content;
    close OUT;
}
