fedora-createrepo-image/scripts/xlion-repo-repackage-deb

211 lines
4.8 KiB
Shell
Executable file

#!/bin/bash
#
# xlion-repo-repackage-deb - Repackage DEB files with fpm.
#
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
SOURCE_DIR=""
TARGET_DIR=""
DATE_VERSION=false
DATE_FORMAT="%Y%m%d"
COMPRESSION="xz"
REMOVE_SOURCE_DIR=false
RECOMMENDS=()
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
info() {
echo -e "${BLUE}$1${NC}" >&2
}
success() {
echo -e "${GREEN}$1${NC}" >&2
}
warn() {
echo -e "${YELLOW}Warning: $1${NC}" >&2
}
show_help() {
cat << EOF
xlion-repo-repackage-deb - Repackage DEB files with fpm.
Usage:
$(basename "$0") --source-dir DIR [options]
Options:
--source-dir DIR Directory containing source .deb files. Required.
--target-dir DIR Output directory. Default: current directory.
--date-version Append +DATE to the original package version.
--date-format FORMAT Date format for --date-version. Default: %Y%m%d.
--compression NAME fpm --deb-compression value. Default: xz.
--deb-recommends NAME Add a Debian Recommends entry. Can be repeated.
--remove-source-dir Remove --source-dir after successful repackaging.
-h, --help Show this help message.
Examples:
$(basename "$0") --source-dir ori --target-dir . --deb-recommends xlion-repo-archive-keyring --remove-source-dir
$(basename "$0") --source-dir ori --target-dir . --date-version --deb-recommends xlion-repo-archive-keyring --remove-source-dir
Notes:
- Logs are printed to stderr.
- This tool only repackages files. It does not create APT metadata or sign repositories.
EOF
}
check_dependencies() {
local missing=()
local cmd
for cmd in "$@"; do
if ! command -v "$cmd" > /dev/null 2>&1; then
missing+=("$cmd")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
error "Missing required CLI tool(s): ${missing[*]}"
fi
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--source-dir)
[[ $# -ge 2 ]] || error "Option --source-dir requires a value."
SOURCE_DIR="$2"
shift 2
;;
--target-dir)
[[ $# -ge 2 ]] || error "Option --target-dir requires a value."
TARGET_DIR="$2"
shift 2
;;
--date-version)
DATE_VERSION=true
shift
;;
--date-format)
[[ $# -ge 2 ]] || error "Option --date-format requires a value."
DATE_FORMAT="$2"
shift 2
;;
--compression)
[[ $# -ge 2 ]] || error "Option --compression requires a value."
COMPRESSION="$2"
shift 2
;;
--deb-recommends)
[[ $# -ge 2 ]] || error "Option --deb-recommends requires a value."
RECOMMENDS+=("$2")
shift 2
;;
--remove-source-dir)
REMOVE_SOURCE_DIR=true
shift
;;
-h | --help)
show_help
exit 0
;;
*)
error "Unknown option: $1"
;;
esac
done
}
resolve_dir() {
local dir="$1"
mkdir -p "$dir" || error "Failed to create directory: $dir"
(cd "$dir" && pwd -P) || error "Failed to resolve directory: $dir"
}
validate_args() {
if [[ -z "$SOURCE_DIR" ]]; then
error "Option --source-dir is required."
fi
if [[ ! -d "$SOURCE_DIR" ]]; then
error "Source directory does not exist: $SOURCE_DIR"
fi
if [[ -z "$TARGET_DIR" ]]; then
TARGET_DIR="."
fi
}
build_fpm_args() {
local -n out_args=$1
local rec
out_args=(fpm -t deb -s deb --deb-compression "$COMPRESSION")
for rec in "${RECOMMENDS[@]}"; do
out_args+=(--deb-recommends "$rec")
done
}
main() {
parse_args "$@"
validate_args
check_dependencies fpm dpkg-deb date find
SOURCE_DIR="$(resolve_dir "$SOURCE_DIR")"
TARGET_DIR="$(resolve_dir "$TARGET_DIR")"
mapfile -t deb_files < <(find "$SOURCE_DIR" -maxdepth 1 -type f -name '*.deb' | sort)
if [[ ${#deb_files[@]} -eq 0 ]]; then
error "No .deb files found in source directory: $SOURCE_DIR"
fi
info "Source directory: $SOURCE_DIR"
info "Target directory: $TARGET_DIR"
date_suffix="$(date "+$DATE_FORMAT")"
pids=()
for deb_file in "${deb_files[@]}"; do
(
info "Processing: $deb_file"
fpm_args=()
build_fpm_args fpm_args
if [[ "$DATE_VERSION" == true ]]; then
original_version="$(dpkg-deb -f "$deb_file" Version)"
[[ -n "$original_version" ]] || error "Failed to read Version from: $deb_file"
fpm_args+=(--version "${original_version}+${date_suffix}")
fi
fpm_args+=(-p "$TARGET_DIR" "$deb_file")
"${fpm_args[@]}"
) &
pids+=("$!")
done
for pid in "${pids[@]}"; do
wait "$pid"
done
if [[ "$REMOVE_SOURCE_DIR" == true ]]; then
rm -rf "$SOURCE_DIR"
info "Removed source directory: $SOURCE_DIR"
fi
success "Repackaged ${#deb_files[@]} DEB file(s)."
}
main "$@"