1 # Library functions for debhelper programs.
3 # Joey Hess, GPL copyright 1997, 1998.
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
8 # Note that this cannot handle complex commands, especially anything
9 # involving redirection. Use complex_doit instead.
12 if [ ! "$DH_NO_ACT" ]; then
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.
22 if [ ! "$DH_NO_ACT" ]; then
27 # Echo something if the verbose flag is on.
29 if [ "$DH_VERBOSE" ]; then
34 # Echo an error message and exit.
36 echo `basename $0`": $1" >&2
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.
44 if [ "$DH_TMPDIR" ]; then
46 elif [ "$1" = "$MAINPACKAGE" ]; then
49 echo "debian/$PACKAGE"
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
60 if [ -e "debian/$1.$2" ]; then
62 elif [ "$1" = "$MAINPACKAGE" -a -e "debian/$2" ]; then
67 # Pass it a name of a binary package, it returns the name to prefix to files
68 # in debian for this package.
70 if [ "$1" != "$MAINPACKAGE" ]; then
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.
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
99 # Automatically add a shell script snippet to a debian script.
100 # Only works if the script has #DEBHELPER# in it.
103 # 1: script to add to
104 # 2: filename of snippet
105 # 3: sed commands to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
108 autoscript_filename=$2
110 autoscript_debscript=debian/`pkgext $PACKAGE`$autoscript_script.debhelper
112 if [ -e "$DH_AUTOSCRIPTDIR/$autoscript_filename" ]; then
113 autoscript_filename="$DH_AUTOSCRIPTDIR/$autoscript_filename"
115 if [ -e "/usr/lib/debhelper/autoscripts/$autoscript_filename" ]; then
116 autoscript_filename="/usr/lib/debhelper/autoscripts/$autoscript_filename"
118 error "/usr/lib/debhelper/autoscripts/$autoscript_filename does not exist"
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"
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() {
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
145 PACKAGES=`expr "$PACKAGES" : '\(.*\) .*'` || true
147 if [ ! "$THISPKG" ]; then
148 error "debian/control invalid - too many Architecture lines or too few Package lines"
151 if [ "$ARCH" = "all" ]; then
152 INDEP_PACKAGES="$INDEP_PACKAGES $THISPKG"
154 ARCH_PACKAGES="$ARCH_PACKAGES $THISPKG"
158 if [ "$PACKAGES" ]; then
159 error "debian/control invalid - too many Architecure lines or too few Package lines"
163 # Argument processing and global variable initialization is below.
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.
168 if expr "$arg" : '-' >/dev/null ; then
173 if [ "$parseopt" ]; then
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
178 eval `dh_getopt.pl "$@"`
179 if [ "$DH_PARSE_ERROR" ]; then
180 error "$DH_PARSE_ERROR"
184 # Get the name of the main binary package (first one listed in
186 MAINPACKAGE=`grep ^Package: debian/control | cut -d " " -f 2 | head -1`
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."
194 DH_DOPACKAGES=`grep ^Package: debian/control | cut -d " " -f 2 | tr "\n" " "`
197 # Check to see if -P was specified. If so, we can only act on a single
199 if [ "$DH_TMPDIR" ] && echo "$DH_DOPACKAGES" | egrep -q '.+ .+' ; then
200 error "-P was specified, but multiple packages would be acted on."
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"
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