Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | ########################################################################### |
| 3 | # Makefile for ABI Dumper |
Andrey Ponomarenko | 5d886e3 | 2015-04-12 19:00:43 +0300 | [diff] [blame] | 4 | # Install/remove the tool for GNU/Linux |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 5 | # |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 6 | # Copyright (C) 2013-2015 Andrey Ponomarenko's ABI Laboratory |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 7 | # |
| 8 | # Written by Andrey Ponomarenko |
| 9 | # |
| 10 | # This program is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License or the GNU Lesser |
| 12 | # General Public License as published by the Free Software Foundation. |
| 13 | # |
| 14 | # This program is distributed in the hope that it will be useful, |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | # GNU General Public License for more details. |
| 18 | # |
| 19 | # You should have received a copy of the GNU General Public License |
| 20 | # and the GNU Lesser General Public License along with this program. |
| 21 | # If not, see <http://www.gnu.org/licenses/>. |
| 22 | ########################################################################### |
| 23 | use Getopt::Long; |
| 24 | Getopt::Long::Configure ("posix_default", "no_ignore_case"); |
| 25 | use File::Path qw(mkpath rmtree); |
| 26 | use File::Copy qw(copy); |
| 27 | use File::Basename qw(dirname); |
| 28 | use Cwd qw(abs_path); |
| 29 | use File::Find; |
| 30 | use strict; |
| 31 | |
| 32 | my $TOOL_SNAME = "abi-dumper"; |
| 33 | my $ARCHIVE_DIR = abs_path(dirname($0)); |
| 34 | |
| 35 | my $HELP_MSG = " |
| 36 | NAME: |
| 37 | Makefile for ABI Dumper |
| 38 | |
| 39 | DESCRIPTION: |
| 40 | Install $TOOL_SNAME command and private modules. |
| 41 | |
| 42 | USAGE: |
Andrey Ponomarenko | 557b089 | 2015-09-06 01:29:39 +0300 | [diff] [blame] | 43 | sudo perl $0 -install -prefix /usr |
| 44 | sudo perl $0 -remove -prefix /usr |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 45 | |
| 46 | OPTIONS: |
| 47 | -h|-help |
| 48 | Print this help. |
| 49 | |
| 50 | --prefix=PREFIX |
Andrey Ponomarenko | 557b089 | 2015-09-06 01:29:39 +0300 | [diff] [blame] | 51 | Install files in PREFIX [/usr]. |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 52 | |
| 53 | -install |
| 54 | Command to install the tool. |
| 55 | |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 56 | -remove |
| 57 | Command to remove the tool. |
| 58 | |
| 59 | EXTRA OPTIONS: |
| 60 | --destdir=DESTDIR |
| 61 | This option is for maintainers to build |
| 62 | RPM or DEB packages inside the build root. |
| 63 | The environment variable DESTDIR is also |
| 64 | supported. |
| 65 | \n"; |
| 66 | |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 67 | if(not @ARGV) |
| 68 | { |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 69 | print $HELP_MSG; |
| 70 | exit(0); |
| 71 | } |
| 72 | |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 73 | my ($PREFIX, $DESTDIR, $Help, $Install, $Remove); |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 74 | |
| 75 | GetOptions( |
| 76 | "h|help!" => \$Help, |
| 77 | "prefix=s" => \$PREFIX, |
| 78 | "destdir=s" => \$DESTDIR, |
| 79 | "install!" => \$Install, |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 80 | "remove!" => \$Remove |
| 81 | ) or exit(1); |
| 82 | |
| 83 | sub scenario() |
| 84 | { |
| 85 | if($Help) |
| 86 | { |
| 87 | print $HELP_MSG; |
| 88 | exit(0); |
| 89 | } |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 90 | if(not $Install and not $Remove) |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 91 | { |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 92 | print STDERR "ERROR: command is not selected (-install or -remove)\n"; |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 93 | exit(1); |
| 94 | } |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 95 | |
| 96 | if($Install) |
| 97 | { # remove old version first |
| 98 | $Remove = 1; |
| 99 | } |
| 100 | |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 101 | if($PREFIX ne "/") { |
| 102 | $PREFIX=~s/[\/]+\Z//g; |
| 103 | } |
| 104 | if(not $PREFIX) |
| 105 | { # default prefix |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 106 | $PREFIX = "/usr"; |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 107 | } |
| 108 | if(my $Var = $ENV{"DESTDIR"}) |
| 109 | { |
| 110 | print "Using DESTDIR environment variable\n"; |
| 111 | $DESTDIR = $Var; |
| 112 | } |
| 113 | if($DESTDIR) |
| 114 | { |
| 115 | if($DESTDIR ne "/") { |
| 116 | $DESTDIR=~s/[\/]+\Z//g; |
| 117 | } |
| 118 | if($DESTDIR!~/\A\//) |
| 119 | { |
| 120 | print STDERR "ERROR: destdir is not absolute path\n"; |
| 121 | exit(1); |
| 122 | } |
| 123 | if(not -d $DESTDIR) |
| 124 | { |
| 125 | print STDERR "ERROR: you should create destdir directory first\n"; |
| 126 | exit(1); |
| 127 | } |
| 128 | $PREFIX = $DESTDIR.$PREFIX; |
| 129 | if(not -d $PREFIX) |
| 130 | { |
| 131 | print STDERR "ERROR: you should create installation directory first (destdir + prefix):\n mkdir -p $PREFIX\n"; |
| 132 | exit(1); |
| 133 | } |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | if($PREFIX!~/\A\//) |
| 138 | { |
| 139 | print STDERR "ERROR: prefix is not absolute path\n"; |
| 140 | exit(1); |
| 141 | } |
| 142 | if(not -d $PREFIX) |
| 143 | { |
| 144 | print STDERR "ERROR: you should create prefix directory first\n"; |
| 145 | exit(1); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | print "INSTALL PREFIX: $PREFIX\n"; |
| 150 | |
| 151 | # paths |
| 152 | my $EXE_PATH = "$PREFIX/bin"; |
| 153 | my $MODULES_PATH = "$PREFIX/share/$TOOL_SNAME"; |
| 154 | my $REL_PATH = "../share/$TOOL_SNAME"; |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 155 | my $TOOL_PATH = "$EXE_PATH/$TOOL_SNAME"; |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 156 | |
| 157 | if(not -w $PREFIX) |
| 158 | { |
| 159 | print STDERR "ERROR: you should be root\n"; |
| 160 | exit(1); |
| 161 | } |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 162 | if($Remove) |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 163 | { |
| 164 | if(-e $EXE_PATH."/".$TOOL_SNAME) |
| 165 | { # remove executable |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 166 | print "-- Removing $TOOL_PATH\n"; |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 167 | unlink($EXE_PATH."/".$TOOL_SNAME); |
| 168 | } |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 169 | elsif(not $Install) { |
| 170 | print "The tool is not installed\n"; |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 171 | } |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 172 | |
| 173 | if(-d $ARCHIVE_DIR."/modules") |
| 174 | { |
| 175 | if(-d $MODULES_PATH) |
| 176 | { # remove modules |
| 177 | print "-- Removing $MODULES_PATH\n"; |
| 178 | rmtree($MODULES_PATH); |
| 179 | } |
| 180 | elsif(not $Install) { |
| 181 | print "The modules of the tool are not installed\n"; |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 182 | } |
| 183 | } |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 184 | } |
| 185 | if($Install) |
| 186 | { |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 187 | # configure |
| 188 | my $Content = readFile($ARCHIVE_DIR."/".$TOOL_SNAME.".pl"); |
| 189 | if($DESTDIR) { # relative path |
| 190 | $Content=~s/MODULES_INSTALL_PATH/$REL_PATH/; |
| 191 | } |
| 192 | else { # absolute path |
| 193 | $Content=~s/MODULES_INSTALL_PATH/$MODULES_PATH/; |
| 194 | } |
| 195 | |
| 196 | # copy executable |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 197 | print "-- Installing $TOOL_PATH\n"; |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 198 | mkpath($EXE_PATH); |
| 199 | writeFile($EXE_PATH."/".$TOOL_SNAME, $Content); |
Andrey Ponomarenko | 4819d01 | 2015-08-21 22:04:52 +0300 | [diff] [blame] | 200 | chmod(0755, $EXE_PATH."/".$TOOL_SNAME); |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 201 | |
| 202 | # copy modules |
| 203 | if(-d $ARCHIVE_DIR."/modules") |
| 204 | { |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 205 | print "-- Installing $MODULES_PATH\n"; |
| 206 | mkpath($MODULES_PATH); |
| 207 | copyDir($ARCHIVE_DIR."/modules", $MODULES_PATH); |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | # check PATH |
| 211 | if($ENV{"PATH"}!~/(\A|:)\Q$EXE_PATH\E[\/]?(\Z|:)/) { |
| 212 | print "WARNING: your PATH variable doesn't include \'$EXE_PATH\'\n"; |
| 213 | } |
| 214 | } |
| 215 | exit(0); |
| 216 | } |
| 217 | |
Andrey Ponomarenko | b3ffe4c | 2013-05-24 14:31:47 +0400 | [diff] [blame] | 218 | sub copyDir($$) |
| 219 | { |
| 220 | my ($From, $To) = @_; |
| 221 | my %Files; |
| 222 | find(\&wanted, $From); |
| 223 | sub wanted { |
| 224 | $Files{$File::Find::dir."/$_"} = 1 if($_ ne "."); |
| 225 | } |
| 226 | foreach my $Path (sort keys(%Files)) |
| 227 | { |
| 228 | my $Inst = $Path; |
| 229 | $Inst=~s/\A\Q$ARCHIVE_DIR\E/$To/; |
| 230 | if(-d $Path) |
| 231 | { # directories |
| 232 | mkpath($Inst); |
| 233 | } |
| 234 | else |
| 235 | { # files |
| 236 | mkpath(dirname($Inst)); |
| 237 | copy($Path, $Inst); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | sub readFile($) |
| 243 | { |
| 244 | my $Path = $_[0]; |
| 245 | return "" if(not $Path or not -f $Path); |
| 246 | open(FILE, $Path) || die ("can't open file \'$Path\': $!\n"); |
| 247 | local $/ = undef; |
| 248 | my $Content = <FILE>; |
| 249 | close(FILE); |
| 250 | return $Content; |
| 251 | } |
| 252 | |
| 253 | sub writeFile($$) |
| 254 | { |
| 255 | my ($Path, $Content) = @_; |
| 256 | return if(not $Path); |
| 257 | open(FILE, ">".$Path) || die ("can't open file \'$Path\': $!\n"); |
| 258 | print FILE $Content; |
| 259 | close(FILE); |
| 260 | } |
| 261 | |
Andrey Ponomarenko | 1215ea2 | 2015-08-14 17:56:21 +0300 | [diff] [blame] | 262 | scenario(); |