]> git.donarmstrong.com Git - debhelper.git/blob - dh_link
r496: * Man page cleanups, Closes: #119335
[debhelper.git] / dh_link
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_link - create symlinks in package build directories
6
7 =cut
8
9 use strict;
10 use Debian::Debhelper::Dh_Lib;
11
12 =head1 SYNOPSIS
13
14 B<dh_link> [S<I<debhelper options>>] [B<-A>] [S<I<source destination ...>>]
15
16 =head1 DESCRIPTION
17
18 dh_link is a debhelper program that creates symlinks in package build
19 directories.
20
21 dh_link accepts a list of pairs of source and destination files. The source
22 files are the already existing files that will be symlinked from. The
23 destination files are the symlinks that will be created. There B<must> be
24 an equal number of source and destination files specified.
25
26 The list can be specified in two ways. A file named debian/package.links
27 can list pairs of files. If you use this file, you should put each pair
28 of files on its own line, and separate the files within the pair with
29 whitespace. Also, pairs of files can be specified as parameters - these
30 pairs will only be created in the package build directory of the first
31 package dh_link is told to act on. By default, this is the first binary
32 package in debian/control, but if you use -p, -i, or -a flags, it will be
33 the first package specified by those flags.
34
35 Be sure you B<do> specify the full filename to both the source and
36 destination files (unlike you would do if you were using something like
37 L<ln(1)>).
38
39 dh_link will generate symlinks that comply with debian policy - absolute
40 when policy says they should be absolute, and relative links with as short
41 a path as possible. It will also create any subdirectories it needs to to put
42 the symlinks in.
43
44 =head1 OPTIONS
45
46 =over 4
47
48 =item B<-A>, B<--all>
49
50 Create any links specified by command line parameters in ALL packages
51 acted on, not just the first.
52
53 =item I<source destination ...>
54
55 Create a file named "destination" as a link to a file named "source". Do
56 this in the package build directory of the first package acted on.
57 (Or in all packages if -A is specified.)
58
59 =back
60
61 =head1 EXAMPLES
62
63  dh_link usr/share/man/man1/foo.1 usr/share/man/man1/bar.1
64
65 Make bar.1 be a symlink to foo.1
66
67  dh_link var/lib/foo usr/lib/foo \
68    usr/X11R6/man/man1/foo.1x usr/share/man/man1/bar.1
69
70 Make /usr/lib/foo/ be a link to /var/lib/foo/, and bar.1 be a symlink to
71 the X man page foo.1x
72
73 =cut
74
75 init();
76
77 foreach my $package (@{$dh{DOPACKAGES}}) {
78         my $tmp=tmpdir($package);
79         my $file=pkgfile($package,"links");
80
81         my @links;
82         if ($file) {
83                 @links=filearray($file);
84         }
85
86         # Make sure it has pairs of symlinks and destinations. If it
87         # doesn't, $#links will be _odd_ (not even, -- it's zero-based).
88         if (int($#links/2) eq $#links/2) {
89                 error("$file lists a link without a destination.");
90         }
91
92         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
93                 push @links, @ARGV;
94         }
95
96         # Same test as above, including arguments this time.
97         if (int($#links/2) eq $#links/2) {
98                 error("parameters list a link without a destination.");
99         }
100
101         while (@links) {
102                 my $dest=pop @links;
103                 my $src=pop @links;
104
105                 # Relavatize src and dest.
106                 $src=~s:^/::;
107                 $dest=~s:^/::;
108
109                 # Make sure the directory the link will be in exists.
110                 my $basedir=Debian::Debhelper::Dh_Lib::dirname("$tmp/$dest");
111                 if (! -e $basedir) {
112                         doit("install","-d",$basedir);
113                 }
114                 
115                 # Policy says that if the link is all within one toplevel
116                 # directory, it should be relative. If it's between
117                 # top level directories, leave it absolute.
118                 my @src_dirs=split(m:/+:,$src);
119                 my @dest_dirs=split(m:/+:,$dest);
120                 if ($src_dirs[0] eq $dest_dirs[0]) {
121                         # Figure out how much of a path $src and $dest
122                         # share in common.
123                         my $x;
124                         for ($x=0; $x<$#src_dirs && $src_dirs[$x] eq $dest_dirs[$x]; $x++) {}
125                         # Build up the new src.
126                         $src="";
127                         for (1..$#dest_dirs - $x) {
128                                 $src.="../";
129                         }
130                         for ($x .. $#src_dirs) {
131                                 $src.=$src_dirs[$_]."/";
132                         }
133                         $src=~s:/$::;
134                 }
135                 else {
136                         # Make sure it's properly absolute.
137                         $src="/$src";
138                 }
139                 
140                 doit("ln","-sf",$src,"$tmp/$dest");
141         }
142 }
143
144 =head1 SEE ALSO
145
146 L<debhelper(1)>
147
148 This program is a part of debhelper.
149
150 =head1 AUTHOR
151
152 Joey Hess <joeyh@debian.org>
153
154 =cut