]> matita.cs.unibo.it Git - helm.git/blob - helm/scripts/init.d/loadPredefinedStylesheets.pl
ocaml 3.09 transition
[helm.git] / helm / scripts / init.d / loadPredefinedStylesheets.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 use LWP::UserAgent;
5 use URI::Escape;
6
7 my $usage = <<EOHELP;
8 loadPredefinedStylesheets.pl [ uwobo_panel_conf_file ]
9 loadPredefinedStylesheets.pl { -u | --unload } [ uwobo_panel_conf_file ]
10 loadPredefinedStylesheets.pl { -r | --reload } [ uwobo_panel_conf_file ]
11 loadPredefinedStylesheets.pl { -h | --help   } [ uwobo_panel_conf_file ]
12
13 If uwobo_panel_conf_file is provided, it's used as HTML file from which
14 parse XSLT stylesheets, otherwise UWOBO_PANEL_CONF environment variable
15 is used.
16 EOHELP
17
18 sub getPredefinedStylesheets ($) {
19   my $confile = shift;
20   my %stylesconf;
21   my $inForm = 0; # currenlty inside 'predefinedStylesheets' form
22   open(CONF, "< $confile") || die "Can't open $confile";
23   while(my $l = <CONF>) {
24     chomp($l);
25     if (not $inForm) {
26       if ($l =~ /<form name="predefinedStylesheets">/) {
27         $inForm = 1;
28       }
29     } else {  # in form
30       if ($l =~ /<\/form>/) {
31         $inForm = 0;
32       } elsif ($l =~ /<option value="[^"]+">/) {
33         my $val = $l;
34         $val =~ s/.*<option value="([^"]+)".*>/$1/;
35         my ($key, $ur, $use_getter) = split /,/, $val;
36         $stylesconf{$key}[0] = $ur;
37         if ($use_getter =~ /true/) {
38           $use_getter = 1;
39         } elsif ($use_getter =~ /false/) {
40           $use_getter = 0;
41         } else {
42           die "Unknown value '$use_getter' for 'use_getter' field.";
43         }
44         $stylesconf{$key}[1] = $use_getter;
45       }
46     }
47   }
48   close(CONF);
49   return %stylesconf;
50 }
51
52 my $action = "load";
53 my $opt = shift || "";
54 die $usage if (($opt eq "-h") or ($opt eq "--help"));
55 if (($opt eq "-u") or ($opt eq "--unload")) {
56   $action = "unload";
57 } elsif (($opt eq "-r") or ($opt eq "--reload")) {
58   $action = "reload";
59 } else {
60   unshift (@ARGV, $opt);
61 }
62 my $confile =
63   shift || $ENV{"UWOBO_PANEL_CONF"} || die "UWOBO_PANEL_CONF not defined";
64 my $getter_url = $ENV{"HELM_GETTER_URL"} || die "HELM_GETTER_URL not defined";
65 my $uwobo_url = $ENV{"HELM_UWOBO_URL"} || die "HELM_UWOBO_URL not defined";
66 my %styles = getPredefinedStylesheets($confile);
67 my $request_url = "";
68
69 if ($action eq "load") {  # load predefined stylesheets
70   foreach my $k (keys %styles) {
71     $request_url .= "&" unless ($request_url eq "");
72     my $ur = $styles{$k}[0];
73     my $use_getter = $styles{$k}[1];
74     $request_url .= "bind=$k,";
75     my $tmp = "";
76     if ($use_getter) {
77       $tmp .= $getter_url . "/getxslt?uri="
78     }
79     $tmp .= $ur;
80     $request_url .= uri_escape($tmp);
81   }
82   $request_url = $uwobo_url . "/add?$request_url";
83
84 } elsif ($action eq "unload") { # unload predefined stylesheets
85   $request_url = $uwobo_url . "/remove?keys=" . join(',', keys %styles);
86
87 } elsif ($action eq "reload") { # reload predefined stylesheets
88   $request_url = $uwobo_url . "/reload?keys=" . join(',', keys %styles);
89
90 } else {  # unknown action
91   die "Unknown action '$action'";
92 }
93
94 my $agent = LWP::UserAgent->new();
95 my $response = $agent->get($request_url);
96 print $response->as_string();
97 #   print "$request_url\n";
98