]> git.donarmstrong.com Git - debhelper.git/blob - dh_lib
r40: Initial Import
[debhelper.git] / dh_lib
1 # Library functions for debhelper programs.
2
3 # Run a command, and display the command to stdout if verbose mode is on.
4 # All commands that modifiy files in $TMP should be ran via this 
5 # function.
6 # Note that this cannot handle complex commands, especially anything
7 # involving redirection. Use complex_doit instead.
8 doit() {
9         verbose_echo "$@"
10         eval '$@'
11 }
12
13 # This is an identical command to doit, except the parameter passed to it
14 # are evaled with double quotes. This version can handle compound commands.
15 complex_doit() {
16         verbose_echo "$@"
17         eval "$@"
18 }
19
20 # Echo something if the verbose flag is on.
21 verbose_echo() {
22         if [ "$DH_VERBOSE" ]; then
23                 echo "  $@"
24         fi
25 }
26
27 # Echo an error message and exit.
28 error() {
29         echo `basename $0`": $1" >&2
30         exit 1
31 }
32
33 # Pass it a name of a binary package, it returns the name of the tmp dir to
34 # use, for that package.
35 # This is for back-compatability with the debian/tmp tradition.
36 tmpdir() {
37         if [ "$DH_TMPDIR" ]; then
38                 echo "$DH_TMPDIR"
39         elif [ "$1" = "$MAINPACKAGE" ]; then
40                 echo debian/tmp
41         else
42                 echo "debian/$PACKAGE"
43         fi
44 }
45
46 # Pass it a name of a binary package, it returns the name to prefix to files
47 # in debian for this package.
48 pkgext() {
49         if [ "$1" != "$MAINPACKAGE" ]; then
50                 echo "$PACKAGE."
51         fi
52 }
53
54 # Automatically add a shell script snippet to a debian script.
55 # Only works if the script has #DEBHELPER# in it.
56 #
57 # Parameters:
58 # 1: script to add to
59 # 2: filename of snippet
60 # 3: sed commands to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
61 autoscript() {
62         autoscript_script=$1
63         autoscript_filename=$2
64         autoscript_sed=$3
65         autoscript_debscript=debian/`pkgext $PACKAGE`$autoscript_script.debhelper
66
67         if [ -e "$DH_AUTOSCRIPTDIR/$autoscript_filename" ]; then
68                 autoscript_filename="$DH_AUTOSCRIPTDIR/$autoscript_filename"
69         else
70                 if [ -e "/usr/lib/debhelper/autoscripts/$autoscript_filename" ]; then
71                         autoscript_filename="/usr/lib/debhelper/autoscripts/$autoscript_filename"
72                 else
73                         error "/usr/lib/debhelper/autoscripts/$autoscript_filename does not exist"
74                 fi
75         fi
76
77         complex_doit "echo \"# Automatically added by `basename $0` on `822-date`\" >> $autoscript_debscript"
78         complex_doit "sed \"$autoscript_sed\" $autoscript_filename >> $autoscript_debscript"
79         complex_doit "echo '# End automatically added section' >> $autoscript_debscript"
80 }
81
82 # Argument processing and global variable initialization is below.
83
84 # Parse command line.
85 set -- `getopt xvidrnakp:P:u: $*`
86
87 for i; do
88         case "$i"
89         in
90                 -v)
91                         DH_VERBOSE=1
92                         shift
93                         ;;
94                 -i)
95                         DH_DOINDEP=1
96                         shift
97                         ;;
98                 -a)
99                         DH_DOARCH=1
100                         shift
101                         ;;
102                 -p)
103                         DH_DOPACKAGES="$DH_DOPACKAGES $2"
104                         shift
105                         shift
106                         ;;
107                 -n)
108                         DH_NOSCRIPTS=1
109                         shift
110                         ;;
111                 -x)     
112                         DH_EXCLUDE=1
113                         shift
114                         ;;
115                 -d)
116                         DH_D_FLAG=1
117                         shift
118                         ;;
119                 -r)
120                         DH_R_FLAG=1
121                         shift
122                         ;;
123                 -k)
124                         DH_K_FLAG=1
125                         shift
126                         ;;
127                 -P)
128                         DH_TMPDIR="$2"
129                         shift
130                         shift
131                         ;;
132                 -u)
133                         DH_U_PARAMS="$2"
134                         shift
135                         shift
136                         ;;
137                 --)
138                         shift
139                         break
140                         ;;
141         esac
142 done
143
144 # Get the package version.
145 # Note that the 2>/dev/null is because a bug in dpkg-parsechangelog makes it
146 # output a bogus error message to stderr.
147 # If it actually has a real error, then the expr will fail, and this whole
148 # script will come crashing to a halt, which is good enough to inform
149 # the user something's wrong. :-)
150 VERSION=`expr "\`dpkg-parsechangelog 2>/dev/null\`" : '.*Version: \(.*\).*Distribution:'`
151
152 # Get the name of the main binary package.
153 MAINPACKAGE=`grep ^Package: debian/control | cut -d " " -f 2 | head -1`
154
155 # Is this a native Debian package?
156 if ! expr "$VERSION" : '.*-' >/dev/null; then
157         NATIVE=1
158 fi
159
160 if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
161         # Figure out all the binary packages to be produced, by looking at the
162         # control file. Break it into 2 lists, INDEP_PACKAGES and ARCH_PACKAGES.
163         #
164         # First, get the list of all binary packages.
165         PACKAGES=`grep ^Package: debian/control | cut -d " " -f 2 | tr "\n" " "`
166         # Remove trailing space.
167         PACKAGES=`expr "$PACKAGES" : '\(.*\) '`
168         # Loop on the list of architectures. Note that we tac the result to reverse
169         # it, becuase we are going through the list of packages in reverse.
170         for ARCH in `grep ^Architecture: debian/control | tac | cut -d " " -f 2` ; do
171                 THISPKG=`expr "$PACKAGES" : '.* \(.*\)'` || true
172                 if [ ! "$THISPKG" ]; then
173                         THISPKG=$PACKAGES
174                 fi
175                 PACKAGES=`expr "$PACKAGES" : '\(.*\) .*'` || true
176                 if [ ! "$THISPKG" ]; then
177                         error "debian/control invalid - too many Architecture lines or too few Package lines"
178                 fi
179                 if [ "$ARCH" = "all" ]; then
180                         INDEP_PACKAGES="$INDEP_PACKAGES $THISPKG"
181                 else
182                         ARCH_PACKAGES="$ARCH_PACKAGES $THISPKG"
183                 fi
184         done
185
186         if [ "$PACKAGES" ]; then
187                 error "debian/control invalid - too many Architecure lines or too few Package lines"
188         fi
189         if [ "$DH_DOINDEP" ]; then
190                 DH_DOPACKAGES="$DH_DOPACKAGES $INDEP_PACKAGES"
191         fi
192         if [ "$DH_DOARCH" ]; then
193                 DH_DOPACKAGES="$DH_DOPACKAGES $ARCH_PACKAGES"
194         fi
195 fi
196
197 # Check if packages to build have been specified, if not, fall back to 
198 # the default, doing them all. Note that DH_DOPACKAGES may have a leading
199 # space and be empty otherwise.
200 if [ ! "$DH_DOPACKAGES" -o "$DH_DOPACKAGES" = " " ]; then
201         if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
202                 error "I have no package to build."
203         fi
204         DH_DOPACKAGES=`grep ^Package: debian/control | cut -d " " -f 2`
205 fi
206
207 # Check to see if -P was specified. If so, we can only act on a single
208 # package.
209 if [ "$DH_TMPDIR" ] && echo "$DH_DOPACKAGES" | egrep -q '.+ .+' ; then
210         error "-P was specified, but multiple packages would be acted on."
211 fi