]> git.donarmstrong.com Git - debhelper.git/blob - dh_lib
r66: Initial Import
[debhelper.git] / dh_lib
1 # Library functions for debhelper programs.
2 #
3 # Joey Hess, GPL copyright 1997, 1998.
4
5 # Run a command, and display the command to stdout if verbose mode is on.
6 # All commands that modifiy files in $TMP should be ran via this 
7 # function.
8 # Note that this cannot handle complex commands, especially anything
9 # involving redirection. Use complex_doit instead.
10 doit() {
11         verbose_echo "$@"
12         if [ ! "$DH_NO_ACT" ]; then
13                 eval '$@'
14         fi
15 }
16
17
18 # This is an identical command to doit, except the parameter passed to it
19 # are evaled with double quotes. This version can handle compound commands.
20 complex_doit() {
21         verbose_echo "$@"
22         if [ ! "$DH_NO_ACT" ]; then
23                 eval "$@"
24         fi
25 }
26
27 # Echo something if the verbose flag is on.
28 verbose_echo() {
29         if [ "$DH_VERBOSE" ]; then
30                 echo "  $@"
31         fi
32 }
33
34 # Echo an error message and exit.
35 error() {
36         echo `basename $0`": $1" >&2
37         exit 1
38 }
39
40 # Pass it a name of a binary package, it returns the name of the tmp dir to
41 # use, for that package.
42 # This is for back-compatability with the debian/tmp tradition.
43 tmpdir() {
44         if [ "$DH_TMPDIR" ]; then
45                 echo "$DH_TMPDIR"
46         elif [ "$1" = "$MAINPACKAGE" ]; then
47                 echo debian/tmp
48         else
49                 echo "debian/$PACKAGE"
50         fi
51 }
52
53 # Pass this the name of a binary package, and the name of the file wanted
54 # for the package, and it will return the actual filename to use. For
55 # example if the package is foo, and the file is somefile, it will look for 
56 # debian/somefile, and if found return that, otherwise, it will look for
57 # debian/foo, and if found, return that. Failing that, it will return
58 # nothing.
59 pkgfile() {
60         if [ -e "debian/$1.$2" ]; then
61                 echo "debian/$1.$2"
62         elif [ "$1" = "$MAINPACKAGE" -a -e "debian/$2" ]; then
63                 echo "debian/$2"
64         fi
65 }
66
67 # Pass it a name of a binary package, it returns the name to prefix to files
68 # in debian for this package.
69 pkgext() {
70         if [ "$1" != "$MAINPACKAGE" ]; then
71         echo "$PACKAGE."
72         fi
73 }
74
75 # Returns 1 if the package is a native debian package, null otherwise.
76 # As a side effect, sets $VERSION to the version of this package.
77 # Caches return code so it only needs to run dpkg-parsechangelog once.
78 isnative() {
79         if [ -z "$DH_ISNATIVE" ]; then
80                 # Get the package version.
81                 # Note that the 2>/dev/null is because a bug in dpkg-parsechangelog makes it
82                 # output a bogus error message to stderr.
83                 # If it actually has a real error, then the expr will fail, and this whole
84                 # script will come crashing to a halt, which is good enough to inform
85                 # the user something's wrong. :-)
86                 VERSION=`expr "\`dpkg-parsechangelog 2>/dev/null\`" : \
87                         '.*Version: \(.*\).*Distribution:'`
88                 # Is this a native Debian package?
89                 if expr "$VERSION" : '.*-' >/dev/null; then
90                         DH_ISNATIVE=1
91                 else
92                         DH_ISNATIVE=0
93                 fi
94         fi
95
96         return "$DH_ISNATIVE"
97 }
98
99 # Automatically add a shell script snippet to a debian script.
100 # Only works if the script has #DEBHELPER# in it.
101 #
102 # Parameters:
103 # 1: script to add to
104 # 2: filename of snippet
105 # 3: sed commands to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
106 autoscript() {
107         autoscript_script=$1
108         autoscript_filename=$2
109         autoscript_sed=$3
110         autoscript_debscript=debian/`pkgext $PACKAGE`$autoscript_script.debhelper
111
112         if [ -e "$DH_AUTOSCRIPTDIR/$autoscript_filename" ]; then
113                 autoscript_filename="$DH_AUTOSCRIPTDIR/$autoscript_filename"
114         else
115                 if [ -e "/usr/lib/debhelper/autoscripts/$autoscript_filename" ]; then
116                         autoscript_filename="/usr/lib/debhelper/autoscripts/$autoscript_filename"
117                 else
118                         error "/usr/lib/debhelper/autoscripts/$autoscript_filename does not exist"
119                 fi
120         fi
121
122         complex_doit "echo \"# Automatically added by `basename $0`\" >> $autoscript_debscript"
123         complex_doit "sed \"$autoscript_sed\" $autoscript_filename >> $autoscript_debscript"
124         complex_doit "echo '# End automatically added section' >> $autoscript_debscript"
125 }
126
127 # Sets 2 global variables, INDEP_PACKAGES is all the arch-independant
128 # packages, ARCH_PACKAGES is the arch-dependant packages.
129 get_arch_indep_packages() {
130         INDEP_PACKAGES=""
131         ARCH_PACKAGES=""
132         
133         # First, get the list of all binary packages.
134         # Notice we want the list in reverse order, thus the tac.
135         PACKAGES=`grep ^Package: debian/control | cut -d " " -f 2 | tac | tr "\n" " "`
136         # Remove trailing space.
137         PACKAGES=`expr "$PACKAGES" : '\(.*\) '`
138         # Loop on the list of architectures.
139         for ARCH in `grep ^Architecture: debian/control | cut -d " " -f 2` ; do
140                 # Pull the last package off the list.
141                 THISPKG=`expr "$PACKAGES" : '.* \(.*\)'` || true
142                 if [ ! "$THISPKG" ]; then
143                         THISPKG=$PACKAGES
144                 fi
145                 PACKAGES=`expr "$PACKAGES" : '\(.*\) .*'` || true
146         
147                 if [ ! "$THISPKG" ]; then
148                         error "debian/control invalid - too many Architecture lines or too few Package lines"
149                 fi
150
151                 if [ "$ARCH" = "all" ]; then
152                         INDEP_PACKAGES="$INDEP_PACKAGES $THISPKG"
153                 else
154                         ARCH_PACKAGES="$ARCH_PACKAGES $THISPKG"
155                 fi
156         done
157
158         if [ "$PACKAGES" ]; then
159                 error "debian/control invalid - too many Architecure lines or too few Package lines"
160         fi
161 }
162
163 # Argument processing and global variable initialization is below.
164
165 # Check to see if an argument on the command line starts with a dash.
166 # if so, we need to pass this off to the resource intensive perl.
167 for arg; do
168         if expr "$arg" : '-' >/dev/null ; then
169                 parseopt=1
170                 break
171         fi
172 done
173 if [ "$parseopt" ]; then
174         parseopt=""
175         # Parse command line. I wrote a perl program to do this becuase
176         # getopt(1) is so broken. Note: the quotes around $@ are very
177         # important!
178         eval `dh_getopt.pl "$@"`
179         if [ "$DH_PARSE_ERROR" ]; then
180                 error "$DH_PARSE_ERROR"
181         fi
182 fi
183
184 # Get the name of the main binary package (first one listed in
185 # debian/control).
186 MAINPACKAGE=`grep ^Package: debian/control | cut -d " " -f 2 | head -1`
187
188 # Check if packages to build have been specified, if not, fall back to 
189 # the default, doing them all.
190 if [ ! "$DH_DOPACKAGES" ]; then
191         if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
192                 error "I have no package to build."
193         fi
194         DH_DOPACKAGES=`grep ^Package: debian/control | cut -d " " -f 2 | tr "\n" " "`
195 fi
196
197 # Check to see if -P was specified. If so, we can only act on a single
198 # package.
199 if [ "$DH_TMPDIR" ] && echo "$DH_DOPACKAGES" | egrep -q '.+ .+' ; then
200         error "-P was specified, but multiple packages would be acted on."
201 fi
202
203 # Figure out which package is the first one we were instructed to build.
204 # This package gets special treatement, files and directories specified on
205 # the command line may effect it.
206 for PACKAGE in $DH_DOPACKAGES ; do
207         DH_FIRSTPACKAGE="$PACKAGE"
208         break
209 done
210
211 # Check to see if: DH_FIRSTPACKAGE is not the MAINPACKAGE, and
212 # some command line arguements are passed. Display a warning, becuase
213 # debhelper's behaviour has changed in this case.
214 if [ "$DH_FIRSTPACKAGE" != "$MAINPACKAGE" -a "$*" ]; then
215         echo `basename $0`": Warning: my behavior has changed, and command line" >&2
216         echo `basename $0`": arguments \"$*\" will apply to package \"$DH_FIRSTPACKAGE\"" >&2
217 fi