2023-04-13 06:04:38 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-04-13 01:53:57 +00:00
|
|
|
|
|
|
|
|
2023-08-17 08:43:59 +00:00
|
|
|
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:"
|
2023-08-18 09:28:14 +00:00
|
|
|
echo " --package <dir-name> add a dir to build, can use multi times"
|
|
|
|
echo " --package-dir <dir> all dir in this directory is a package,"
|
|
|
|
echo " can use multi times"
|
2023-08-17 08:43:59 +00:00
|
|
|
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"
|
2023-08-18 09:28:14 +00:00
|
|
|
local log_exe_str="${4-}"
|
|
|
|
|
2023-08-17 08:43:59 +00:00
|
|
|
local final_exe_str="${exe_str/$place_holder/$package_name}"
|
2023-08-18 09:28:14 +00:00
|
|
|
|
|
|
|
if [[ -n "${log_exe_str}" ]] ; then
|
|
|
|
msg_info "Running: $final_exe_str"
|
|
|
|
fi
|
2023-08-17 08:43:59 +00:00
|
|
|
|
|
|
|
$final_exe_str
|
|
|
|
}
|
|
|
|
|
|
|
|
shopt -s nullglob
|
|
|
|
set -o functrace
|
|
|
|
set -o nounset
|
|
|
|
set -o errexit
|
|
|
|
set -o errtrace
|
|
|
|
|
2023-08-18 04:22:11 +00:00
|
|
|
_DELIMITER="================================================================"
|
2023-08-17 08:43:59 +00:00
|
|
|
_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 ;;
|
2023-08-18 09:28:14 +00:00
|
|
|
--package-exists-hook)
|
|
|
|
PACKAGE_EXISTS_HOOK="$2"
|
|
|
|
# strip white space, see https://stackoverflow.com/a/3352015
|
|
|
|
PACKAGE_EXISTS_HOOK="${PACKAGE_EXISTS_HOOK#"${PACKAGE_EXISTS_HOOK%%[![:space:]]*}"}"
|
|
|
|
PACKAGE_EXISTS_HOOK="${PACKAGE_EXISTS_HOOK%"${PACKAGE_EXISTS_HOOK##*[![:space:]]}"}"
|
|
|
|
shift ;;
|
2023-08-17 08:43:59 +00:00
|
|
|
--)
|
|
|
|
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
|
|
|
|
|
2023-08-18 07:57:26 +00:00
|
|
|
if [[ -n "${PACKAGE_DIR-}" && "${#PACKAGE_DIR}" -gt 0 ]] ; then
|
2023-08-17 08:43:59 +00:00
|
|
|
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"
|
2023-04-13 01:53:57 +00:00
|
|
|
done
|
2023-08-17 08:43:59 +00:00
|
|
|
if [[ "${#_PACKGES_TO_BUILD[@]}" -eq 0 ]] ; then
|
|
|
|
msg_info "No package to build, exiting..."
|
|
|
|
fi
|
|
|
|
|
2023-08-18 09:28:14 +00:00
|
|
|
if [[ -n "$PACKAGE_EXISTS_HOOK" ]] ; then
|
|
|
|
msg_info "hook for checking if package exists: $PACKAGE_EXISTS_HOOK"
|
|
|
|
fi
|
|
|
|
|
2023-08-17 08:43:59 +00:00
|
|
|
for package in "${_PACKGES_TO_BUILD[@]}" ; do
|
|
|
|
(
|
2023-08-18 04:22:11 +00:00
|
|
|
msg_info "$_DELIMITER"
|
2023-08-17 08:43:59 +00:00
|
|
|
msg_info "Start for $package"
|
|
|
|
cd "$package"
|
|
|
|
|
2023-08-18 12:08:38 +00:00
|
|
|
function retrieve_source {
|
|
|
|
"$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
|
|
|
|
}
|
|
|
|
function is_dynamic_pkgver {
|
|
|
|
"$MAKEDEB_PATH" --is-dynamic-pkgver "${MAKEDEB_ARGS[@]}"
|
|
|
|
}
|
2023-08-17 08:43:59 +00:00
|
|
|
|
2023-08-18 12:08:38 +00:00
|
|
|
package_name=""
|
|
|
|
source_ready=0
|
2023-08-17 08:43:59 +00:00
|
|
|
|
|
|
|
if [[ -n "${PACKAGE_EXISTS_HOOK}" ]] ; then
|
2023-08-18 12:08:38 +00:00
|
|
|
if is_dynamic_pkgver ; then
|
|
|
|
retrieve_source
|
|
|
|
source_ready=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
package_name="$("$MAKEDEB_PATH" --packagelist "${MAKEDEB_ARGS[@]}")"
|
|
|
|
|
2023-08-18 09:28:14 +00:00
|
|
|
msg_info "Checking if package $package_name exists..."
|
2023-08-17 08:43:59 +00:00
|
|
|
|
2023-08-18 12:08:38 +00:00
|
|
|
run_hook "$PACKAGE_EXISTS_HOOK" "{}" "$package_name" log_exe_str && ret="$?" || ret="$?"
|
2023-08-17 08:43:59 +00:00
|
|
|
if [[ $ret -eq 0 ]] ; then
|
|
|
|
msg_info "Package ${package_name} already built, skip"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-08-18 12:08:38 +00:00
|
|
|
if (( ! source_ready )) ; then
|
|
|
|
retrieve_source
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "$package_name" ]] ; then
|
|
|
|
package_name="$("$MAKEDEB_PATH" --packagelist "${MAKEDEB_ARGS[@]}")"
|
|
|
|
fi
|
|
|
|
|
2023-08-18 09:28:14 +00:00
|
|
|
msg_info "Start building..."
|
|
|
|
|
2023-08-17 08:43:59 +00:00
|
|
|
"$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
|
2023-08-18 04:22:11 +00:00
|
|
|
) || true
|
2023-08-17 08:43:59 +00:00
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|