-
Notifications
You must be signed in to change notification settings - Fork 3
/
link.pl
executable file
·179 lines (154 loc) · 5.47 KB
/
link.pl
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/perl
# --
# Copyright (C) 2001-2017 OTRS AG, http://otrs.com/
# 2017-2017 Complemento, https://www.complemento.net.br
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --
=head1 NAME
link.pl - script for linking OTRS modules into framework root
=head1 SYNOPSIS
link.pl -h
link.pl <source-module-folder> <otrs-folder>
=head1 DESCRIPTION
This script installs a given OTRS module into the OTRS framework by creating
appropriate links.
Beware that code from the .sopm file is not executed.
So this script can be used for an already installed module, when linking
files from CVS checkout directory.
Please send any questions, suggestions & complaints to <[email protected]>
=head1 TODO
When running the scripts twice, the '.old' files might be overwritten.
=cut
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Pod::Usage;
use File::Spec ();
# get options
my ($OptHelp);
GetOptions( 'h' => \$OptHelp ) || pod2usage(
-verbose => 1,
message => 'invalid params'
);
if ($OptHelp) {
pod2usage( -verbose => 0 ); # this will exit the script
}
# Now get the work done
my $Source = shift || die "Need module location as ARG0";
$Source = File::Spec->rel2abs($Source);
if ( !-d $Source ) {
die "ERROR: invalid module directory '$Source'";
}
my $Dest = shift || die "Need Framework-Root location as ARG1";
$Dest = File::Spec->rel2abs($Dest);
if ( !-d $Dest ) {
die "ERROR: invalid Framework-Root directory '$Dest'";
}
Clean($Dest);
my @Dirs;
my $Start = $Source;
R($Start);
sub Clean {
my $In = shift;
my @List = glob("$In/*");
for my $File (@List) {
$File =~ s/\/\//\//g;
next if ( $File =~ /RELEASE/ );
# recurse into subdirectories
if ( -d $File ) {
Clean($File);
}
else {
my $OrigFile = $File;
$File =~ s/$Dest//;
# Unlink files that points to /opt/src/otrs
if ( -l "$Dest/$File" && readlink("$Dest/$File") =~ m/$Source/) {
unlink("$Dest/$File") || die "ERROR: Can't unlink symlink: $Dest/$File";
}
}
}
}
sub R {
my $In = shift;
my @List = glob("$In/*");
my $cmd = "cd /opt/otrs && /opt/otrs/bin/otrs.Console.pl Admin::Package::ListInstalledFiles --allow-root|grep ':' | awk -F ': ' '{print \$2}'";
my @PackageFileList = `$cmd`;
chomp @PackageFileList;
my %PackageFileListHash = map { $_ => 1 } @PackageFileList;
for my $File (@List) {
$File =~ s/\/\//\//g;
# recurse into subdirectories
if ( -d $File ) {
R($File);
}
else {
my $OrigFile = $File;
$File =~ s/$Start//;
# check directory of location (in case create a directory)
if ( "$Dest/$File" =~ /^(.*)\/(.+?|)$/ )
{
my $Directory = $1;
my @Directories = split( /\//, $Directory );
my $DirectoryCurrent = '';
for my $Directory (@Directories) {
$DirectoryCurrent .= "/$Directory";
if ( $DirectoryCurrent && !-d $DirectoryCurrent ) {
if ( mkdir $DirectoryCurrent ) {
print STDERR "NOTICE: Create Directory $DirectoryCurrent\n";
}
else {
die "ERROR: can't create directory $DirectoryCurrent: $!";
}
}
}
}
if ( -l "$Dest/$File" ) {
unlink("$Dest/$File") || die "ERROR: Can't unlink symlink: $Dest/$File";
}
#if ( -e "$Dest/$File" ) {
#if ( rename( "$Dest/$File", "$Dest/$File.old" ) ) {
#print "NOTICE: Backup orig file: $Dest/$File.old\n";
#}
#else {
#die "ERROR: Can't rename $Dest/$File to $Dest/$File.old: $!";
#}
#}
if ( !-e $Dest ) {
die "ERROR: No such directory: $Dest";
}
elsif ( !-e $OrigFile ) {
die "ERROR: No such orig file: $OrigFile";
}
# installed by an AddOn such OTRS::ITSM
my $F = substr($File,1);
# If file is not part of a package
if ( ! $PackageFileListHash{$F} ) {
# Then remove it if exists
unlink("$Dest/$File");
# Try to link OTRS Source
if ( !symlink( $OrigFile, "$Dest/$File" ) ) {
die "ERROR: Can't $File link: $!";
} else {
print "NOTICE: Link: $OrigFile -> \n";
print "NOTICE: $Dest/$File\n";
}
}
}
}
}
exit 0;