]> git.donarmstrong.com Git - debhelper.git/blob - dh_lib
r24: 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 doit() {
7         verbose_echo "$@"
8         eval "$@"
9 }
10
11 # Echo something if the verbose flag is on.
12 verbose_echo() {
13         if [ "$DH_VERBOSE" ]; then
14                 echo "  $@"
15         fi
16 }
17
18 # Echo an error message and exit.
19 error() {
20         echo `basename $0`": $1" >&2
21         exit 1
22 }
23
24 # Pass it a name of a binary package, it returns the name of the tmp dir to
25 # use, for that package.
26 # This is for back-compatability with the debian/tmp tradition.
27 tmpdir() {
28         if [ "$DH_TMPDIR" ]; then
29                 echo "$DH_TMPDIR"
30         elif [ "$1" = "$MAINPACKAGE" ]; then
31                 echo debian/tmp
32         else
33                 echo "debian/$PACKAGE"
34         fi
35 }
36
37 # Pass it a name of a binary package, it returns the name to prefix to files
38 # in debian for this package.
39 pkgext() {
40         if [ "$1" != "$MAINPACKAGE" ]; then
41                 echo "$PACKAGE."
42         fi
43 }
44
45 # Automatically add a shell script snippet to a debian script.
46 # Only works if the script has #DEBHELPER# in it.
47 #
48 # Parameters:
49 # 1: script to add to
50 # 2: filename of snippet
51 # 3: sed commands to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
52 autoscript() {
53         autoscript_script=$1
54         autoscript_filename=$2
55         autoscript_sed=$3
56         autoscript_debscript=debian/`pkgext $PACKAGE`$autoscript_script.debhelper
57
58         if [ -e "$DH_AUTOSCRIPTDIR/$autoscript_filename" ]; then
59                 autoscript_filename="$DH_AUTOSCRIPTDIR/$autoscript_filename"
60         else
61                 if [ -e "/usr/lib/debhelper/autoscripts/$autoscript_filename" ]; then
62                         autoscript_filename="/usr/lib/debhelper/autoscripts/$autoscript_filename"
63                 else
64                         error "/usr/lib/debhelper/autoscripts/$autoscript_filename does not exist"
65                 fi
66         fi
67
68         # Running doit doesn't cut it here.
69         doit "echo \"# Automatically added by `basename $0` on `822-date`\" >> $autoscript_debscript"
70         doit "sed \"$autoscript_sed\" $autoscript_filename >> $autoscript_debscript"
71         doit "echo '# End automatically added section' >> $autoscript_debscript"
72 }
73
74 # Argument processing and global variable initialization is below.
75
76 # Parse command line.
77 set -- `getopt xvianp:P: $*`
78
79 for i; do
80         case "$i"
81         in
82                 -v)
83                         DH_VERBOSE=1
84                         shift
85                         ;;
86                 -i)
87                         DH_DOINDEP=1
88                         shift
89                         ;;
90                 -a)
91                         DH_DOARCH=1
92                         shift
93                         ;;
94                 -p)
95                         DH_DOPACKAGES="$DH_DOPACKAGES $2"
96                         shift
97                         shift
98                         ;;
99                 -n)
100                         DH_NOSCRIPTS=1
101                         shift
102                         ;;
103                 -x)     
104                         DH_EXCLUDE=1
105                         shift
106                         ;;
107                 -P)
108                         DH_TMPDIR="$2"
109                         shift
110                         shift
111                         ;;
112                 --)
113                         shift
114                         break
115                         ;;
116         esac
117 done
118
119 # Get the package version from the changelog.
120 LINE=`head -1 debian/changelog`
121 VERSION=`expr "$LINE" : '.* (\(.*\))'`
122
123 # Get the name of the main binary package.
124 MAINPACKAGE=`grep ^Package: debian/control | cut -d " " -f 2 | head -1`
125
126 # Is this a native Debian package?
127 if ! expr "$VERSION" : '.*-' >/dev/null; then
128         NATIVE=1
129 fi
130
131 if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
132         # Figure out all the binary packages to be produced, by looking at the
133         # control file. Break it into 2 lists, INDEP_PACKAGES and ARCH_PACKAGES.
134         #
135         # First, get the list of all binary packages.
136         PACKAGES=`grep ^Package: debian/control | cut -d " " -f 2 | tr "\n" " "`
137         # Remove trailing space.
138         PACKAGES=`expr "$PACKAGES" : '\(.*\) '`
139         # Loop on the list of architectures. Note that we tac the result to reverse
140         # it, becuase we are going through the list of packages in reverse.
141         for ARCH in `grep ^Architecture: debian/control | tac | cut -d " " -f 2` ; do
142                 THISPKG=`expr "$PACKAGES" : '.* \(.*\)'` || true
143                 if [ ! "$THISPKG" ]; then
144                         THISPKG=$PACKAGES
145                 fi
146                 PACKAGES=`expr "$PACKAGES" : '\(.*\) .*'` || true
147                 if [ ! "$THISPKG" ]; then
148                         error "debian/control invalid - too many Architecture lines or too few Package lines"
149                 fi
150                 if [ "$ARCH" = "all" ]; then
151                         INDEP_PACKAGES="$INDEP_PACKAGES $THISPKG"
152                 else
153                         ARCH_PACKAGES="$ARCH_PACKAGES $THISPKG"
154                 fi
155         done
156
157         if [ "$PACKAGES" ]; then
158                 error "debian/control invalid - too many Architecure lines or too few Package lines"
159         fi
160         if [ "$DH_DOINDEP" ]; then
161                 DH_DOPACKAGES="$DH_DOPACKAGES $INDEP_PACKAGES"
162         fi
163         if [ "$DH_DOARCH" ]; then
164                 DH_DOPACKAGES="$DH_DOPACKAGES $ARCH_PACKAGES"
165         fi
166 fi
167
168 # Check if packages to build have been specified, if not, fall back to 
169 # the default, doing them all. Note that DH_DOPACKAGES may have a leading
170 # space and be empty otherwise.
171 if [ ! "$DH_DOPACKAGES" -o "$DH_DOPACKAGES" = " " ]; then
172         if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
173                 error "I have no package to build."
174         fi
175         DH_DOPACKAGES=`grep ^Package: debian/control | cut -d " " -f 2`
176 fi
177
178 # Check to see if -P was specified. If so, we can only act on a single
179 # package.
180 if [ "$DH_TMPDIR" ] && echo "$DH_DOPACKAGES" | egrep -q '.+ .+' ; then
181         error "-P was specified, but multiple packages would be acted on."
182 fi