]> gitweb @ CieloNegro.org - stowinstall.git/blob - stowinstall
Change the interpreter path
[stowinstall.git] / stowinstall
1 #!/usr/bin/perl
2 # -*- cperl -*-
3 use strict;
4 use warnings;
5 use File::Spec;
6
7 if (!@ARGV) {
8     print "This is stowinstall,\n";
9     print " does `make install DESTDIR=...' for the GNU stow.\n";
10     print "\n";
11     print "  Usage: stowinstall [-f] [stow-dir]\n";
12     print "  (e.g. stowinstall /usr/local/stow/emacs)\n";
13     print "\n";
14     print "  Options:\n";
15     print "    -f --force      force install even if the Makefile\n";
16     print "                    doesn't have any \$(DESTDIR)\n";
17     print "    --file=<file>   specify the name of Makefile explicitly\n";
18     print "    --prefix=<dir>  specify the prefix instead of scanning Makefile\n";
19     print "    --target=<targ> specify the target instead of `install'\n";
20     print "\n";
21     print "stowinstall can only install packages whose Makefile corresponds:\n";
22     print " * file name is exactly `Makefile'\n";
23     print " * the Makefile has target `install'\n";
24     print " * the Makefile has `prefix = ...' form\n";
25     print " * the Makefile has at least one `\$(DESTDIR)' form\n";
26     print "autotools is our friend; it generates such Makefile:s.\n";
27 }
28
29 my $stowdir;
30 my $opt_force;
31 my $opt_prefix;
32 my $opt_file = 'Makefile';
33 my $opt_target = 'install';
34 foreach (@ARGV) {
35     if (/^-f$/ or /^--force$/) {
36         $opt_force = 1;
37     }
38     elsif (/^--prefix=(.+)$/) {
39         $opt_prefix = $1;
40     }
41     elsif (/^--file=(.+)$/) {
42         $opt_file = $1;
43     }
44     elsif (/^--target=(.+)$/) {
45         $opt_target = $1;
46     }
47     else {
48         $stowdir = $_;
49     }
50 }
51
52 if (-r $opt_file) {
53     print "stowinstall: $opt_file exists.\n";
54 }
55 else {
56     die "stowinstall: $opt_file doesn't exist.\n";
57 }
58
59 my $makefile = do {
60     local $/ = undef;
61     open my $fh, '<', $opt_file;
62     <$fh>;
63 };
64
65 my ($prefix, $destdir, $target_install);
66 $prefix = $opt_prefix;
67 do {
68     foreach my $line (split /\r?\n/, $makefile) {
69         if (!$prefix and
70               $line =~ /^prefix\s*=\s*(.+?)\s*$/) {
71             $prefix = $1;
72             if ($prefix =~ /^(["'])(.+?)\1$/) {
73                 $prefix = $2;
74             }
75         }
76         elsif ($line =~ /\$\(DESTDIR\)/) {
77             $destdir = 1;
78         }
79         elsif ($line =~ /^\Q$opt_target\E\s*:/) {
80             $target_install = 1;
81         }
82     }
83 };
84
85 if ($prefix) {
86     print "stowinstall: found prefix $prefix\n";
87 }
88 else {
89     die "stowinstall: prefix not found...\n";
90 }
91
92 if ($destdir) {
93     print "stowinstall: found \$(DESTDIR).\n";
94 }
95 else {
96     if ($opt_force) {
97         print "stowinstall: \$(DESTDIR) not found; doing force install. THIS IS DANGEROUS.\n";
98     }
99     else {
100         die "stowinstall: \$(DESTDIR) not found; this $opt_file seems not to support DESTDIR.\n";
101     }
102 }
103
104 if ($target_install) {
105     print "stowinstall: found target $opt_target.\n";
106 }
107 else {
108     if ($opt_force) {
109         print "stowinstall: $opt_target target not found, but proceeding anyway...\n";
110     }
111     else {
112         die "stowinstall: $opt_target target not found; this $opt_file seems not to support $opt_target!\n";
113     }
114 }
115
116 if (!-d $stowdir) {
117     mkdir $stowdir or
118       die "stowinstall: failed to mkdir $stowdir\n";
119 }
120
121 my @destdir = File::Spec->splitdir(
122     File::Spec->canonpath("/tmp/stowinstall-$$/".$prefix));
123 my $linkname = pop @destdir;
124
125 my $path = '';
126 foreach (@destdir) {
127     $path .= '/'.$_;
128     if (!-d $path) {
129         mkdir $path or
130           die "stowinstall: failed to mkdir $path\n";
131     }
132 }
133
134 my $linktarget = File::Spec->catdir(@destdir, $linkname);
135 symlink $stowdir => $linktarget or
136   die "stowinstall: failed to symlink $stowdir => $linktarget\n";
137
138 system("make $opt_target DESTDIR=/tmp/stowinstall-$$");
139 system("rm -rf /tmp/stowinstall-$$");