]> git.donarmstrong.com Git - debhelper.git/blob - dh_lib
r4: 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 debian/$TMP should be ran via this 
5 # function.
6 function doit() {
7         verbose_echo "$1"
8         $1
9 }
10
11 # Echo something if the verbose flag is on.
12 function verbose_echo() {
13         if [ "$DH_VERBOSE" ]; then
14                 echo "  $1"
15         fi
16 }
17
18 # Echo an error message and exit.
19 function 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, relative to debian/
26 # This is for back-compatability with the debian/tmp tradition.
27 function tmpdir() {
28         if [ "$1" = "$MAINPACKAGE" ]; then
29                 echo tmp
30         else
31                 echo "$PACKAGE"
32         fi
33 }
34
35 # Pass it a name of a binary package, it returns the name to prefix to files
36 # in debian for this package.
37 function pkgext() {
38         if [ "$1" != "$MAINPACKAGE" ]; then
39                 echo "$PACKAGE."
40         fi
41 }
42
43 # Argument processing and global variable initialization is below.
44
45 # Parse command line.
46 set -- `getopt viap: $*`
47
48 for i; do
49         case "$i"
50         in
51                 -v)
52                         DH_VERBOSE=1
53                         shift
54                         ;;
55                 -i)
56                         DH_DOINDEP=1
57                         shift
58                         ;;
59                 -a)
60                         DH_DOARCH=1
61                         shift
62                         ;;
63                 -p)
64                         DH_DOPACKAGES="$DH_DOPACKAGES $2"
65                         shift
66                         shift
67                         ;;
68                 --)
69                         shift
70                         break
71                         ;;
72         esac
73 done
74
75 # Get the package version from the changelog.
76 LINE=`head -1 debian/changelog`
77 VERSION=`expr "$LINE" : '.* (\(.*\))'`
78
79 # Get the name of the main binary package.
80 MAINPACKAGE=`grep ^Package: debian/control | cut -d " " -f 2 | head -1`
81
82 # Is this a native Debian package?
83 if ! expr "$VERSION" : '.*-' >/dev/null; then
84         NATIVE=1
85 fi
86
87 if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
88         # Figure out all the binary packages to be produced, by looking at the
89         # control file. Break it into 2 lists, INDEP_PACKAGES and ARCH_PACKAGES.
90         #
91         # First, get the list of all binary packages.
92         PACKAGES=`grep ^Package: debian/control | cut -d " " -f 2 | tr "\n" " "`
93         # Remove trailing space.
94         PACKAGES=`expr "$PACKAGES" : '\(.*\) '`
95         # Loop on the list of architectures. Note that we tac the result to reverse
96         # it, becuase we are going through the list of packages in reverse.
97         for ARCH in `grep ^Architecture: debian/control | tac | cut -d " " -f 2` ; do
98                 THISPKG=`expr "$PACKAGES" : '.* \(.*\)'` || true
99                 if [ ! "$THISPKG" ]; then
100                         THISPKG=$PACKAGES
101                 fi
102                 PACKAGES=`expr "$PACKAGES" : '\(.*\) .*'` || true
103                 if [ ! "$THISPKG" ]; then
104                         error "debian/control invalid - too many Architecture lines or too few Package lines"
105                 fi
106                 if [ "$ARCH" = "all" ]; then
107                         INDEP_PACKAGES="$INDEP_PACKAGES $THISPKG"
108                 else
109                         ARCH_PACKAGES="$ARCH_PACKAGES $THISPKG"
110                 fi
111         done
112
113         if [ "$PACKAGES" ]; then
114                 error "debian/control invalid - too many Architecure lines or too few Package lines"
115         fi
116         if [ "$DH_DOINDEP" ]; then
117                 DH_DOPACKAGES="$DH_DOPACKAGES $INDEP_PACKAGES"
118         fi
119         if [ "$DH_DOARCH" ]; then
120                 DH_DOPACKAGES="$DH_DOPACKAGES $ARCH_PACKAGES"
121         fi
122 fi
123
124 # Check if packages to build have been specified, if not, fall back to 
125 # the default, doing them all. Note that DH_DOPACKAGES may have a leading
126 # space and be empty otherwise.
127 if [ ! "$DH_DOPACKAGES" -o "$DH_DOPACKAGES" = " " ]; then
128         if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
129                 error "I have no package to build."
130         fi
131         DH_DOPACKAGES=`grep ^Package: debian/control | cut -d " " -f 2`
132 fi