debian-packages/script/build_all.sh

156 lines
4.7 KiB
Bash
Executable file

#!/usr/bin/env bash
function msg_info {
(( QUIET )) && return
local mesg="$1" ; shift
printf "INFO: $mesg\n" "$@"
}
function msg_warn {
(( QUIET )) && return
local mesg="$1" ; shift
printf "WARN: $mesg\n" "$@"
}
function usage {
echo "$_PROGRAM_NAME:"
echo " --package <dir-name> add a dir to build, allow use it multi times"
echo " --package-dir <dir> all dir in this directory is a package"
echo " --makedeb-path <path> specify the path of makedeb"
echo " --upload-to-webdav https://webdav_host/path/dir"
echo " upload built file to a webdav server"
echo " --webdav-user <username> username of webdav server (if required)"
echo " --webdav-pass <password> password of webdav server (if required)"
echo ' --package-exists-hook "/path/to/hook --args {}"'
echo " this program will run the hook to check"
echo " if specific file already built, the {} will"
echo " be replaced with the final package name,"
echo " exit with 0 means package exists"
}
function upload_to_webdav {
local file="$1"
local webdav_path="$2"
local webdav_user="$3"
local webdav_pass="$4"
if [[ -z "$webdav_user" || -z "$webdav_pass" ]] ; then
curl --show-error --silent \
--upload-file "$file" "${webdav_path}/$file"
else
curl --show-error --silent --user "${webdav_user}:${webdav_pass}" \
--upload-file "$file" "${webdav_path}/$file"
fi
}
function run_hook {
local exe_str="$1"
local place_holder="$2"
local package_name="$3"
local final_exe_str="${exe_str/$place_holder/$package_name}"
echo $final_exe_str
$final_exe_str
}
shopt -s nullglob
set -o functrace
set -o nounset
set -o errexit
set -o errtrace
_DELIMITER="================================================================"
_PROGRAM_NAME="$0"
_PACKGES_TO_BUILD=()
MAKEDEB_PATH=""
UPLOAD_TO_WEBDAV=""
WEBDAV_USER=""
WEBDAV_PASS=""
PACKAGE_DIR=()
PACKAGE_EXISTS_HOOK=""
QUIET=0
while (( "$#" >= 1 )); do
case "$1" in
--package) _PACKGES_TO_BUILD+=("$2") ; shift ;;
--package-dir) PACKAGE_DIR+=("$2") ; shift ;;
--makedeb-path) MAKEDEB_PATH="$2" ; shift ;;
--upload-to-webdav) UPLOAD_TO_WEBDAV="$2" ; shift ;;
--webdav-user) WEBDAV_USER="$2" ; shift ;;
--webdav-pass) WEBDAV_PASS="$2" ; shift ;;
--package-exists-hook) PACKAGE_EXISTS_HOOK="$2" ; shift ;;
--)
shift
MAKEDEB_ARGS=("$@")
while (( "$#" > 1 )) ; do # leave one arg for shift command at the end of while loop
shift
done
;;
-h|--help) usage ; exit 0 ;;
*) echo "Unkown option $1"
echo "Use $0 --help for help"
exit 1 ;;
esac
shift
done
if [[ -z "${MAKEDEB_PATH}" ]] ; then
MAKEDEB_PATH="${MAKEDEB_PATH:-"$(which makedeb)"}"
else
MAKEDEB_PATH="$(realpath "$MAKEDEB_PATH")"
fi
if [[ -n "${PACKAGE_DIR-}" && "${#PACKAGE_DIR}" -gt 0 ]] ; then
for p in "${PACKAGE_DIR[@]}" ; do
for f in "$p"/* ; do
_PACKGES_TO_BUILD+=("$f")
done
done
fi
for package in "${_PACKGES_TO_BUILD[@]}" ; do
msg_info "Package to be built: $package"
done
if [[ "${#_PACKGES_TO_BUILD[@]}" -eq 0 ]] ; then
msg_info "No package to build, exiting..."
fi
for package in "${_PACKGES_TO_BUILD[@]}" ; do
(
msg_info "$_DELIMITER"
msg_info "Start for $package"
cd "$package"
"$MAKEDEB_PATH" --nobuild "${MAKEDEB_ARGS[@]}" && ret=$? || ret=$?
if [[ $ret -ne 0 ]] ; then
msg_warn "Error occurred when running makedeb, skip this package"
exit 4
fi
package_name="$("$MAKEDEB_PATH" --packagelist "${MAKEDEB_ARGS[@]}")"
if [[ -n "${PACKAGE_EXISTS_HOOK}" ]] ; then
ret=0
run_hook "$PACKAGE_EXISTS_HOOK" "{}" "$package_name" || ret="$?"
if [[ $ret -eq 0 ]] ; then
msg_info "Package ${package_name} already built, skip"
exit 0
fi
fi
"$MAKEDEB_PATH" --noextract "${MAKEDEB_ARGS[@]}" && ret=$? || ret=$?
if [[ $ret -ne 0 ]] ; then
msg_warn "Error occurred when running makedeb, skip this package"
exit 4
fi
if [[ -n "${UPLOAD_TO_WEBDAV}" ]] ; then
msg_info "Uploading $package_name to $UPLOAD_TO_WEBDAV"
upload_to_webdav "$package_name" "$UPLOAD_TO_WEBDAV" "$WEBDAV_USER" "$WEBDAV_PASS"
fi
) || true
done