+
+ if [ $n -gt 1 ]; then
+ ed $SOURCE <<EOF
+/^Source: /a
+$ed_fragment1
+.
+/^%setup /a
+$ed_fragment2
+.
+wq
+EOF
+ fi
+ fi
+
+ local linuxrelease=$(find_linux_release "$linux")
+ if ! $RPMBUILD $BUILD_TYPE --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
+ ${FIND_REQUIRES:+--define "__find_requires $FIND_REQUIRES"} \
+ --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
+ --define "KVERSION ${linuxrelease}" \
+ --define "$K_SRC ${linux}" \
+ --define "LIB_MOD_DIR /lib/modules/${linuxrelease}/updates" \
+ ${OFA_KERNEL_RELEASE:+--define "_release $OFA_KERNEL_RELEASE"} \
+ --define "configure_options --without-quilt $OFED_CORE $OFED_HARDWARE $OFED_ISCSI" \
+ ${SOURCE} 2>&1; then
+ fatal 1 "Error building kernel-ib"
+ fi
+
+}
+
+store_for_reuse() {
+ local articles="$1"
+ local module="$2"
+ local location="$3"
+ local signature="$4"
+ local use_links="$5"
+
+ local linkflag=""
+ if $use_links; then
+ linkflag="l"
+ fi
+
+ local default_iface=$(/sbin/ip route get 192.1.1.1 | sed -ne 's/.* dev \(.*\) * src .*/\1/p')
+ if [ -z "$default_iface" ]; then
+ fatal 1 "Failed to determine the default route interface"
+ fi
+ local unique_id=$(/sbin/ip addr show dev $default_iface | sed -ne '/ inet /s/ *inet \(.*\)\/.*/\1/p' | head -1)
+ if [ -z "$unique_id" ]; then
+ fatal 1 "Failed to determine a unique id from interface $default_interface"
+ fi
+
+ local finallocation="$location"/"$signature"/"$module"
+ location="$location"/"$signature-${unique_id}"/"$module"
+ mkdir -p "$location"
+ # the cleanup script removes any directory that doesn't have a
+ # .lastused, so let's try to prevent that as soon as we can
+ # this solution still slightly racy with the cleanup script
+ # but the race is a lot tighter now
+ touch -t 197001010000 "$location/.lastused"
+ ## use eval/echo here to make sure shell expansions are performed
+ #if ! cp -a${linkflag} $(eval echo $articles) "$location"; then
+ local article
+ for article in $(eval echo $articles); do
+ if ! cp -a${linkflag} "$article" "$location"; then
+ error "Failed to copy \"$article\" to \"$location\" in store_for_reuse()"
+ # rename the cache location so that it's not cached
+ # product, but is around for analysis
+ mv "$location"{,-bad-$(date +%s)} ||
+ error "failed to clean up a failed cache attempt" \
+ "in \"$location\" -- manual cleanup will be" \
+ "necessary"
+ return 1
+ fi
+ done
+
+ # flag the cache as complete (i.e. in case lbuild was previously
+ # interrupted while caching)
+ touch "$location/.lastused"
+
+ # put the temporary location into the final location
+ # (last one wins)
+ mkdir -p "${finallocation%/*}"
+ mv "$location" "$finallocation"
+ rmdir "${location%/*}"
+ return 0
+
+}
+
+reuse() {
+ local module="$1"
+ local dest="$2"
+ local use_links="${3:-false}"
+ local signature="$4"
+
+ if [ -n "$REUSEBUILD" ] && [ -d "$REUSEBUILD/$signature/$module" ]; then
+ if [ ! -f "$REUSEBUILD/$signature/$module/.lastused" ]; then
+ # the .lastused flag is populated at the end of the caching to
+ # signal that the caching was completed. if that flag is not
+ # there, then the cache is invalid (and should be removed in fact)
+ mv "$REUSEBUILD/$signature/$module"{,-bad-$(date +%s)} ||
+ fatal 1 "failed to clean up a bad cache in location $REUSEBUILD/$signature/$module\" -- manual cleanup will be necessary"
+ return 1
+ fi
+
+ # so that we know how stale this entry is
+ touch $REUSEBUILD/$signature/$module/.lastused
+
+ if $use_links; then
+ if ls $REUSEBUILD/$signature/$module/* >/dev/null 2>&1; then
+ cp -al $REUSEBUILD/$signature/$module/* $dest/