1set -e
2
3function get_version() {
4	[ -n "$VERSION" ] && echo $VERSION && return
5	[ -d /source/scripts ] && cd /source && scripts/version.sh || echo "unknown-version"
6}
7
8function _map_cmdline_null()
9{
10	echo -n ""
11}
12
13function _map_env_null()
14{
15	echo "_=_"
16}
17
18function run_build_command_with_build_manifest_parameters() {
19	map_environment=$1
20	map_cmdline=$2
21	shift 2
22
23	# we sort longer strings in front, to make more specific items
24	# first.
25
26	TMPDIR=$(mktemp -d)
27
28	IFS=$'\t'
29	egrep -e "^${OS_DISTRIBUTION}([^-]|$)" -e "^${IMAGE_PLATFORM}" /dbld/build.manifest | sort -r | head -1 | while read os featureflags env cmdline; do
30		unset IFS
31		echo $os $featureflags $env $cmdline
32		declare -a env_values
33		if [ "${env}" != "-" -a "${env}" != "" ]; then
34			# xargs processes removes quote characters just like the shell
35			echo "${env}" | tr ',' '\n' | xargs -n1 echo > $TMPDIR/env.list
36		        readarray -t env_values < $TMPDIR/env.list
37		fi
38
39		if [ "`${map_cmdline} ${featureflags}`" = "" ]; then
40
41			# no extra command line values (e.g.  deb), execute
42			# it without them on the env command line.  In this
43			# case we supplied an extra, zero-length argument to
44			# dpkg-buildpackage that it errored out on (understandably).
45			#
46			# I couldn't find a way to expand a variable in shell that
47			#   1) was able to handle spaces in arguments
48			#   2) could also represent no extra arguments (not even an empty one)
49			#
50			# that's why we are using this ugly if statement above and the
51			# double evaluation of ${map_cmdline}.
52			#
53
54			echo "Runnign build as: " env "${env_values[@]}" "`${map_environment} ${featureflags}`" "$@" $cmdline
55			env "${env_values[@]}" "`${map_environment} ${featureflags}`" "$@" $cmdline
56		else
57			${map_cmdline} ${featureflags} | xargs -n1 echo > $TMPDIR/cmdline.list
58			readarray -t cmdline_values < $TMPDIR/cmdline.list
59			echo "Running build as: " env "${env_values[@]}" "`${map_environment} ${featureflags}`" "$@" $cmdline "${cmdline_values[@]}"
60			env "${env_values[@]}" "`${map_environment} ${featureflags}`" "$@" $cmdline "${cmdline_values[@]}"
61		fi
62	done
63}
64
65function _map_feature_flags_to_deb_build_profiles()
66{
67    echo -n DEB_BUILD_PROFILES=
68    IFS=,
69    for feature in $1; do
70        case "$feature" in
71          nojava|nopython)
72            # these are standard Debian build profiles, keep them intact
73            echo -n "$feature "
74            ;;
75          *)
76            # everything else is prefixed with "sng-"
77            echo -n "sng-$feature "
78            ;;
79        esac
80    done
81    echo
82}
83
84function deb_run_build_command()
85{
86    run_build_command_with_build_manifest_parameters _map_feature_flags_to_deb_build_profiles _map_cmdline_null "$@"
87}
88
89function _map_feature_flags_to_rpmbuild_with_and_without_options()
90{
91    IFS=,
92    echo -n "--define='_dbld 1' "
93    for feature in $1; do
94        case "$feature" in
95            no*)
96                feature_without_no=`echo $feature | sed -e 's/^no//'`
97                echo -n "--define='_without_${feature_without_no} --without-${feature_without_no}' "
98                ;;
99            *)
100                echo -n "--define='_with_${feature} --with-${feature}' "
101                ;;
102        esac
103    done
104}
105
106
107function rpm_run_build_command()
108{
109    run_build_command_with_build_manifest_parameters _map_env_null _map_feature_flags_to_rpmbuild_with_and_without_options "$@"
110}
111
112function validate_man_binary() {
113	MAN=`which man`
114	if ! [ -x ${MAN} ]; then
115		return 0
116	fi
117
118	set +e
119	man_help=$(${MAN} --help 2>&1)
120	rc=$?
121	set -e
122	if [ "$rc" -ne 0 ]; then
123		cat <<EOF
124${man_help}
125
126Your /usr/bin/man binary seems disfunctional within the dbld container.
127This is a dependency of debhelper, which is required to generate syslog-ng
128Debian packages.
129
130This may happen because of an AppArmor bug that you can work around by
131removing the apparmor policy for man on the HOST computer, invoking this
132command as root:
133
134bash# apparmor_parser -R /etc/apparmor.d/usr.bin.man
135
136The error that happens is that man is complaining about a missing
137libmandb.so, which is there, but AppArmor prevents access.
138
139You can validate whether this was successful by querying the running
140apparmor policy:
141
142bash# apparmor_status
143EOF
144		return 1
145	fi
146}
147
148function validate_container() {
149	validate_man_binary
150}
151
152function capture_artifacts()
153{
154	if [ "$MODE" = "release" ]; then
155		ARTIFACT_DIR=${ARTIFACT_DIR:-/dbld/release/$VERSION}
156		echo "Capturing artifacts:" "$*" "into" "${ARTIFACT_DIR}"
157		cp -R -v "$*" "${ARTIFACT_DIR}"
158		echo "The current list of artifacts:"
159		ls -l "${ARTIFACT_DIR}"
160	fi
161}
162
163VERSION=$(get_version)
164