summaryrefslogtreecommitdiff
path: root/dh_lib
blob: 4c8c68c6f95588e4862e57c1ebb86765936940f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Library functions for debhelper programs.

# Run a command, and display the command to stdout if verbose mode is on.
# All commands that modifiy files in $TMP should be ran via this 
# function.
# Note that this cannot handle complex commands, especially anything
# involving redirection. Use complex_doit instead.
doit() {
	verbose_echo "$@"
	eval '$@'
}

# This is an identical command to doit, except the parameter passed to it
# are evaled with double quotes. This version can handle compound commands.
complex_doit() {
	verbose_echo "$@"
	eval "$@"
}

# Echo something if the verbose flag is on.
verbose_echo() {
	if [ "$DH_VERBOSE" ]; then
		echo "	$@"
	fi
}

# Echo an error message and exit.
error() {
	echo `basename $0`": $1" >&2
	exit 1
}

# Pass it a name of a binary package, it returns the name of the tmp dir to
# use, for that package.
# This is for back-compatability with the debian/tmp tradition.
tmpdir() {
	if [ "$DH_TMPDIR" ]; then
		echo "$DH_TMPDIR"
	elif [ "$1" = "$MAINPACKAGE" ]; then
		echo debian/tmp
	else
		echo "debian/$PACKAGE"
	fi
}

# Pass it a name of a binary package, it returns the name to prefix to files
# in debian for this package.
pkgext() {
	if [ "$1" != "$MAINPACKAGE" ]; then
		echo "$PACKAGE."
	fi
}

# Automatically add a shell script snippet to a debian script.
# Only works if the script has #DEBHELPER# in it.
#
# Parameters:
# 1: script to add to
# 2: filename of snippet
# 3: sed commands to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
autoscript() {
	autoscript_script=$1
	autoscript_filename=$2
	autoscript_sed=$3
	autoscript_debscript=debian/`pkgext $PACKAGE`$autoscript_script.debhelper

	if [ -e "$DH_AUTOSCRIPTDIR/$autoscript_filename" ]; then
		autoscript_filename="$DH_AUTOSCRIPTDIR/$autoscript_filename"
	else
		if [ -e "/usr/lib/debhelper/autoscripts/$autoscript_filename" ]; then
			autoscript_filename="/usr/lib/debhelper/autoscripts/$autoscript_filename"
		else
			error "/usr/lib/debhelper/autoscripts/$autoscript_filename does not exist"
		fi
	fi

	complex_doit "echo \"# Automatically added by `basename $0` on `822-date`\" >> $autoscript_debscript"
	complex_doit "sed \"$autoscript_sed\" $autoscript_filename >> $autoscript_debscript"
	complex_doit "echo '# End automatically added section' >> $autoscript_debscript"
}

# Argument processing and global variable initialization is below.

# Parse command line.
set -- `getopt xvidrnap:P:u: $*`

for i; do
	case "$i"
	in
		-v)
			DH_VERBOSE=1
			shift
			;;
		-i)
			DH_DOINDEP=1
			shift
			;;
		-a)
			DH_DOARCH=1
			shift
			;;
		-p)
			DH_DOPACKAGES="$DH_DOPACKAGES $2"
			shift
			shift
			;;
		-n)
			DH_NOSCRIPTS=1
			shift
			;;
		-x)	
			DH_EXCLUDE=1
			shift
			;;
		-d)
			DH_D_FLAG=1
			shift
			;;
		-r)
			DH_R_FLAG=1
			shift
			;;
		-P)
			DH_TMPDIR="$2"
			shift
			shift
			;;
		-u)
			DH_U_PARAMS="$2"
			shift
			shift
			;;
		--)
			shift
			break
			;;
	esac
done

# Get the package version from the changelog.
LINE=`head -1 debian/changelog`
VERSION=`expr "$LINE" : '.* (\(.*\))'`

# Get the name of the main binary package.
MAINPACKAGE=`grep ^Package: debian/control | cut -d " " -f 2 | head -1`

# Is this a native Debian package?
if ! expr "$VERSION" : '.*-' >/dev/null; then
       	NATIVE=1
fi

if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
	# Figure out all the binary packages to be produced, by looking at the
	# control file. Break it into 2 lists, INDEP_PACKAGES and ARCH_PACKAGES.
	#
	# First, get the list of all binary packages.
	PACKAGES=`grep ^Package: debian/control | cut -d " " -f 2 | tr "\n" " "`
	# Remove trailing space.
	PACKAGES=`expr "$PACKAGES" : '\(.*\) '`
	# Loop on the list of architectures. Note that we tac the result to reverse
	# it, becuase we are going through the list of packages in reverse.
	for ARCH in `grep ^Architecture: debian/control | tac | cut -d " " -f 2` ; do
		THISPKG=`expr "$PACKAGES" : '.* \(.*\)'` || true
		if [ ! "$THISPKG" ]; then
			THISPKG=$PACKAGES
		fi
	        PACKAGES=`expr "$PACKAGES" : '\(.*\) .*'` || true
		if [ ! "$THISPKG" ]; then
			error "debian/control invalid - too many Architecture lines or too few Package lines"
		fi
		if [ "$ARCH" = "all" ]; then
			INDEP_PACKAGES="$INDEP_PACKAGES $THISPKG"
		else
			ARCH_PACKAGES="$ARCH_PACKAGES $THISPKG"
		fi
	done

	if [ "$PACKAGES" ]; then
		error "debian/control invalid - too many Architecure lines or too few Package lines"
	fi
	if [ "$DH_DOINDEP" ]; then
		DH_DOPACKAGES="$DH_DOPACKAGES $INDEP_PACKAGES"
	fi
	if [ "$DH_DOARCH" ]; then
		DH_DOPACKAGES="$DH_DOPACKAGES $ARCH_PACKAGES"
	fi
fi

# Check if packages to build have been specified, if not, fall back to 
# the default, doing them all. Note that DH_DOPACKAGES may have a leading
# space and be empty otherwise.
if [ ! "$DH_DOPACKAGES" -o "$DH_DOPACKAGES" = " " ]; then
	if [ "$DH_DOINDEP" -o "$DH_DOARCH" ]; then
		error "I have no package to build."
	fi
	DH_DOPACKAGES=`grep ^Package: debian/control | cut -d " " -f 2`
fi

# Check to see if -P was specified. If so, we can only act on a single
# package.
if [ "$DH_TMPDIR" ] && echo "$DH_DOPACKAGES" | egrep -q '.+ .+' ; then
	error "-P was specified, but multiple packages would be acted on."
fi