summaryrefslogtreecommitdiffstats
path: root/lib/OpenSLX/DistroUtils/Base.pm
blob: bc6f6f69897d422a5e0fde5384ae2c6ab1d8ad90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package OpenSLX::DistroUtils::Base;

use Data::Dumper;
use OpenSLX::Utils;


sub new
{
    my $class = shift;
    my $self = {};
    return bless $self, $class;
}

sub dumpInit
{
    my $self = shift;
    my $initFile = shift;
    
    print  Dumper($initFile->{'configHash'});
    
    print $self->generateInitFile($initFile);
}

sub _combineBlock
{
    my $self = shift;
    my $block = shift;
    
    my $output;
    
    $output = "#";
    $output .= $block->{'blockDesc'};
    $output .= "\n  ";
    
    my $content = $block->{'content'};
    while ( ($priority, $contentArray) = each %$content )
    {
        $output .= join("\n  ", @$contentArray);
        $output .= "\n  ";
    }
    
    $output .= "\n";
    return $output;
}

sub _renderInfoBlock
{
    my $self = shift;
    my $config = shift;
    
    my $tpl = unshiftHereDoc(<<'    End-of-Here');
    ### BEGIN INIT INFO
    # Provides:             %s
    # Required-Start:       %s
    # Required-Stop:        %s
    # Default-Start:        %s
    # Default-Stop:         %s
    # Short-Description:    %s
    ### END INIT INFO
    
    End-of-Here
    
    return sprintf(
      $tpl,
      $config->{'name'},
      $config->{'requiredStart'},
      $config->{'requiredStop'},
      $config->{'defaultStart'},
      $config->{'defaultStop'},
      $config->{'shortDesc'}
    );
}

sub generateInitFile
{
    my $self = shift;
    my $initFile = shift;
    
    my $config = $initFile->{'configHash'};
    
    $output = "#!/bin/sh\n\n";
    $output .= $self->_renderInfoBlock($config);
    $output .= "set -e \n\n";
    if (keys(%{$config->{'head'}->{'content'}}) > 0) {
        $output .= $self->_combineBlock($config->{'head'});
    }
    if (keys(%{$config->{'functions'}->{'content'}}) > 0) {
        $output .= $self->_combineBlock($config->{'functions'});
    }
    $output .= "case \"\$1\" in \n";
    if (keys(%{$config->{'start'}->{'content'}}) > 0) {
        $output .= "  start)\n";
        $output .= $self->_combineBlock($config->{'start'});
        $output .= "  ;;\n";
    } else {
        # trigger error
        # start is essential
    }
    if (keys(%{$config->{'stop'}->{'content'}}) > 0) {
        $output .= "  stop)\n";
        $output .= $self->_combineBlock($config->{'stop'});
        $output .= "  ;;\n";
    } else {
        # trigger error
        # stop is essential
    }
    if (keys(%{$config->{'reload'}->{'content'}}) > 0) {
        $output .= "  reload)\n";
        $output .= $self->_combineBlock($config->{'relaod'});
        $output .= "  ;;\n";
    }
    if (keys(%{$config->{'restart'}->{'content'}}) > 0) {
        $output .= "  restart)\n";
        $output .= $self->_combineBlock($config->{'restart'});
        $output .= "  ;;\n";
    }
    if (keys(%{$config->{'status'}->{'content'}}) > 0) {
        $output .= "  status)\n";
        $output .= $self->_combineBlock($config->{'status'});
        $output .= "  ;;\n";
    }
    if (keys(%{$config->{'usage'}->{'content'}}) > 0) {
        $output .= "  *)\n";
        $output .= $self->_combineBlock($config->{'usage'});
        $output .= "  exit 1\n";
    } else {
        # try to generate usage
        # $this->_generateUsage();
    }
    $output .= "esac\n\n";
    $output .= "exit 0\n";
    return $output;
    
}

1;