--- /dev/null
+#!/bin/bash -e
+
+########################################################
+# HELP MESSAGE
+########################################################
+
+function print_help {
+ cat << EOF
+Usage: ${EXEC} publish [OPTS] TARGET DESTINATION
+
+Pushes specified built target to device with all sanitized dependencies
+and prepares the device to run this target.
+
+Options:
+ -h, --help Prints this message.
+ -r, --root Push target with root access rights.
+ -s, --serial SERIAL_NUMBER
+ Send all sdb commands to device with specified
+ serial number.
+EOF
+}
+
+########################################################
+# UTILITY FUNCTIONS
+########################################################
+
+# Workaround for 'sdb shell' inability to propagate error codes
+function sdb_shell {
+ set +e
+
+ error_str='SDB_ERROR'
+ cmd="${SDB_CMD[@]} shell ${@}"
+ output=$(command ${cmd} '||' 'echo' "${error_str}")
+
+ # print the sdb command
+ echo "${cmd}"
+
+ echo "${output}" | grep "${error_str}" >/dev/null
+ if [[ $? -eq 0 ]]; then
+ echo "Error: failed to execute '${@}' on device."
+ exit 1
+ fi
+
+ set -e
+}
+
+########################################################
+# GLOBAL VARIABLES
+########################################################
+
+SDB_CMD=('sdb')
+TARGET_DIR=''
+
+########################################################
+# GLOBAL OPTIONS PARSING
+########################################################
+
+while [[ ${1} = -* ]]; do
+ case ${1} in
+ '-h'|'--help')
+ print_help
+ exit 0
+ ;;
+ '-r'|'--root')
+ ROOT=1
+ shift
+ ;;
+ '-s'|'--serial')
+ SDB_CMD+=("${1}" "${2}")
+ shift 2
+ ;;
+ *)
+ echo "Error: Unknown option ${1}."
+ print_help
+ exit 1
+ ;;
+ esac
+done
+
+if [[ $# -lt 2 ]]; then
+ echo "Error: Missing arguments."
+ print_help
+ exit 1
+fi
+
+########################################################
+# TARGET PUBLISHING
+########################################################
+
+target_name="${1}"
+destination="${2}"
+
+# find specified target
+if [[ -d ${BUILD_ARTIFACTS_DIR} || -n $(ls -A ${BUILD_ARTIFACTS_DIR}) ]]; then
+ for dir in $(echo ${BUILD_ARTIFACTS_DIR}/*/); do
+ if [[ $(basename ${dir}) = ${target_name} ]]; then
+ TARGET_DIR="${dir%/}"
+ break
+ fi
+ done
+fi
+
+if [[ -z ${TARGET_DIR} ]]; then
+ echo "Error: no such target '${target_name}'."
+ exit 1
+fi
+
+# check the device architecture
+echo "Checking architecture..."
+
+declare -A archs=([x86_64]='x86_64' [i686]='i586')
+ARCH=${archs["$(sdb_shell_print uname -m)"]}
+if [[ -z ${ARCH} || ! -d ${TARGET_DIR}/${ARCH} ]]; then
+ echo "Error: there is no built ${target_name} for ${ARCH} architecture."
+ exit 1
+fi
+
+# push debug rpms
+printf "\nPushing debug rpm packages to device...\n"
+
+debug_rpm_dir="/tmp/rpms"
+
+${SDB_CMD[@]} root on >/dev/null
+sdb_shell mkdir -p "${debug_rpm_dir}"
+for debug_rpm in $(find "${TARGET_DIR}/${ARCH}/rpm" -name "*-debug*-*.rpm"); do
+ echo "${debug_rpm}"
+ ${SDB_CMD[@]} push "${debug_rpm}" ${debug_rpm_dir} >/dev/null
+done
+
+printf "\nInstalling debug rpm packages on device...\n"
+
+sdb_shell rpm -ivh --force --nodeps "${debug_rpm_dir}/*.rpm"
+sdb_shell rm -rf "${debug_rpm_dir}"
+
+# push sanitized rpms
+# sanitized libraries will be located at /sanitized/usr/lib[64]/
+printf "\nPushing and extracting sanitized rpm packages on device...\n"
+
+sanitized_rpm_dir="/sanitized"
+
+sdb_shell mkdir -p "${sanitized_rpm_dir}"
+for sanitized_rpm in $(find "${TARGET_DIR}/${ARCH}/rpm" -name "*.rpm" \
+ '!' -name "*-debug*-*.rpm"); do
+ echo "${sanitized_rpm}"
+
+ ${SDB_CMD[@]} push "${sanitized_rpm}" "${sanitized_rpm_dir}" >/dev/null
+
+ rpm_path="${sanitized_rpm_dir}/$(basename ${sanitized_rpm})"
+ rpm2cpio_out="/tmp/rpm2cpio.out"
+
+ sdb_shell rpm2cpio "${rpm_path}" ">${rpm2cpio_out}"
+ sdb_shell cd "${sanitized_rpm_dir}" ";" cat "${rpm2cpio_out}" "|" cpio -idm --no-absolute-filenames
+ sdb_shell rm -f "${rpm_path}"
+ sdb_shell rm -f "${rpm2cpio_out}"
+done
+
+# push targets
+printf "\nPushing fuzzing targets to device...\n"
+
+if (( ROOT != 1 )); then
+ ${SDB_CMD[@]} root off >/dev/null
+fi
+
+sdb_shell mkdir -p "${destination}"
+for target_bin in $(find "${TARGET_DIR}/${ARCH}/target" -type f); do
+ echo "${target_bin}"
+
+ ${SDB_CMD[@]} push "${target_bin}" "${destination}" >/dev/null
+done