]> git.donarmstrong.com Git - debhelper.git/blob - dh_lib
r144: 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 # Output a warning.
41 warning() {
42         echo `basename $0`": $1" >&2
43 }
44
45 # Pass it a name of a binary package, it returns the name of the tmp dir to
46 # use, for that package.
47 # This is for back-compatability with the debian/tmp tradition.
48 tmpdir() {
49         if [ "$DH_TMPDIR" ]; then
50                 echo "$DH_TMPDIR"
51         elif [ "$1" = "$MAINPACKAGE" ]; then
52                 echo debian/tmp
53         else
54                 echo "debian/$PACKAGE"
55         fi
56 }
57
58 # Pass this the name of a binary package, and the name of the file wanted
59 # for the package, and it will return the actual filename to use. For
60 # example if the package is foo, and the file is somefile, it will look for 
61 # debian/somefile, and if found return that, otherwise, if the package is
62 # the main package, it will look for debian/foo, and if found, return that. 
63 # Failing that, it will return nothing.
64 pkgfile() {
65         if [ -e "debian/$1.$2" ]; then
66                 echo "debian/$1.$2"
67         elif [ "$1" = "$MAINPACKAGE" -a -e "debian/$2" ]; then
68                 echo "debian/$2"
69         fi
70 }
71
72 # Pass it a name of a binary package, it returns the name to prefix to files
73 # in debian for this package.
74 pkgext() {
75         if [ "$1" != "$MAINPACKAGE" ]; then
76         echo "$PACKAGE."
77         fi
78 }
79
80 # Returns 1 if the package is a native debian package, null otherwise.
81 # As a side effect, sets $VERSION to the version of this package.
82 # Caches return code so it only needs to run dpkg-parsechangelog once.
83 isnative() {
84         if [ -z "$DH_ISNATIVE" ]; then
85                 # Make sure we look at the correct changelog.
86                 isnative_changelog=`pkgfile $PACKAGE changelog`
87                 if [ ! "$isnative_changelog" ]; then
88                         isnative_changelog=debian/changelog
89                 fi
90                 # Get the package version.
91                 # Note that the 2>/dev/null is because a bug in dpkg-parsechangelog makes it
92                 # output a bogus error message to stderr.
93                 # If it actually has a real error, then the expr will fail, and this whole
94                 # script will come crashing to a halt, which is good enough to inform
95                 # the user something's wrong. :-)
96                 VERSION=`expr "\`dpkg-parsechangelog -l$isnative_changelog 2>/dev/null\`" : \
97                         '.*Version: \(.*\).*Distribution:'`
98                 # Is this a native Debian package?
99                 if expr "$VERSION" : '.*-' >/dev/null; then
100                         DH_ISNATIVE=1
101                 else
102                         DH_ISNATIVE=0
103                 fi
104         fi
105
106         return "$DH_ISNATIVE"
107 }
108
109 # Automatically add a shell script snippet to a debian script.
110 # Only works if the script has #DEBHELPER# in it.
111 #
112 # Parameters:
113 # 1: script to add to
114 # 2: filename of snippet
115 # 3: sed commands to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
116 autoscript() {
117         autoscript_script=$1
118         autoscript_filename=$2
119         autoscript_sed=$3
120         autoscript_debscript=debian/`pkgext $PACKAGE`$autoscript_script.debhelper
121
122         if [ -e "$DH_AUTOSCRIPTDIR/$autoscript_filename" ]; then
123                 autoscript_filename="$DH_AUTOSCRIPTDIR/$autoscript_filename"
124         else
125                 if [ -e "/usr/lib/debhelper/autoscripts/$autoscript_filename" ]; then
126                         autoscript_filename="/usr/lib/debhelper/autoscripts/$autoscript_filename"
127                 else
128                         error "/usr/lib/debhelper/autoscripts/$autoscript_filename does not exist"
129                 fi
130         fi
131
132         complex_doit "echo \"# Automatically added by `basename $0`\" >> $autoscript_debscript"
133         complex_doit "sed \"$autoscript_sed\" $autoscript_filename >> $autoscript_debscript"
134         complex_doit "echo '# End automatically added section' >> $autoscript_debscript"
135 }
136
137 # Argument processing and global variable initialization is below.
138
139 # If DH_OPTIONS is set, prepend it to the command line.
140 if [ "$DH_OPTIONS" ]; then
141         set -- $DH_OPTIONS $@
142 fi
143
144 # Check to see if an argument on the command line starts with a dash.
145 # if so, we need to pass this off to the resource intensive perl.
146 for arg; do
147         if expr "$arg" : '-' >/dev/null ; then
148                 parseopt=1
149                 break
150         fi
151 done
152 if [ "$parseopt" ]; then
153         parseopt=""
154         # Parse command line. I wrote a perl program to do this becuase
155         # getopt(1) is so broken. Note: the quotes around $@ are very
156         # important!
157         eval `dh_getopt.pl "$@"`
158         if [ "$DH_PARSE_ERROR" ]; then
159                 error "$DH_PARSE_ERROR"
160         fi
161 fi
162
163 # Get the name of the main binary package (first one listed in
164 # debian/control).
165 MAINPACKAGE=`grep ^Package: debian/control | cut -d " " -f 2 | head -1`
166
167 # Check if packages to build have been specified, if not, fall back to 
168 # the default, doing them all.
169 if [ ! "$DH_DOPACKAGES" ]; then
170         if [ "$DH_DOINDEP" -o "$DH_DOARCH" -o "$DH_DOSAME" ]; then
171                 error "I have no package to build."
172         fi
173         DH_DOPACKAGES=`grep ^Package: debian/control | cut -d " " -f 2 | tr "\n" " "`
174 fi
175
176 # Check to see if -P was specified. If so, we can only act on a single
177 # package.
178 if [ "$DH_TMPDIR" ] && echo "$DH_DOPACKAGES" | egrep -q '.+ .+' ; then
179         error "-P was specified, but multiple packages would be acted on."
180 fi
181
182 # Figure out which package is the first one we were instructed to build.
183 # This package gets special treatement, files and directories specified on
184 # the command line may effect it.
185 for PACKAGE in $DH_DOPACKAGES ; do
186         DH_FIRSTPACKAGE="$PACKAGE"
187         break
188 done