Files
rmm-openwrt/scripts/prepare-package-repository.sh
T

85 lines
2.5 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
if [ "$#" -ne 3 ]; then
echo "usage: $0 <downloaded-artifacts-dir> <output-dir> <agent-version>" >&2
exit 2
fi
source_dir="$1"
output_dir="$2"
agent_version="$3"
if [ ! -d "$source_dir" ]; then
echo "artifact directory does not exist: $source_dir" >&2
exit 1
fi
case "$agent_version" in
''|*[!0-9A-Za-z._-]*)
echo "invalid agent version: $agent_version" >&2
exit 1
;;
esac
if [ -e "$output_dir" ] && [ -n "$(find "$output_dir" -mindepth 1 -maxdepth 1 -print -quit 2>/dev/null)" ]; then
echo "output directory must be empty: $output_dir" >&2
exit 1
fi
mkdir -p "$output_dir/feeds/$agent_version/openwrt" "$output_dir/keys/usign" "$output_dir/keys/apk"
artifact_count=0
for artifact_dir in "$source_dir"/openwrt-*; do
[ -d "$artifact_dir" ] || continue
artifact_name="$(basename "$artifact_dir")"
remainder="${artifact_name#openwrt-}"
openwrt_release="${remainder%%-*}"
target_label="${remainder#"$openwrt_release"-}"
if [ -z "$openwrt_release" ] || [ -z "$target_label" ] || [ "$target_label" = "$remainder" ]; then
echo "cannot parse artifact name: $artifact_name" >&2
exit 1
fi
destination="$output_dir/feeds/$agent_version/openwrt/$openwrt_release/$target_label"
mkdir -p "$destination"
cp "$artifact_dir"/* "$destination/"
while IFS= read -r public_key; do
cp "$public_key" "$output_dir/keys/usign/$(basename "$public_key")"
done < <(find "$artifact_dir" -maxdepth 1 -type f -regextype posix-extended \
-regex '.*/[0-9a-f]{16}' -print)
if [ -f "$artifact_dir/rmm-openwrt.pem" ]; then
cp "$artifact_dir/rmm-openwrt.pem" "$output_dir/keys/apk/rmm-openwrt.pem"
fi
artifact_count=$((artifact_count + 1))
done
if [ "$artifact_count" -eq 0 ]; then
echo "no openwrt-* artifact directories found in $source_dir" >&2
exit 1
fi
mkdir -p "$output_dir/feeds/stable"
cp -a "$output_dir/feeds/$agent_version/." "$output_dir/feeds/stable/"
touch "$output_dir/.nojekyll"
cat > "$output_dir/index.html" <<EOF
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OpenWrt RMM package repository</title>
</head>
<body>
<main>
<h1>OpenWrt RMM package repository</h1>
<p>Текущая стабильная версия агента: <strong>${agent_version}</strong>.</p>
<p>Инструкции подключения находятся в документации проекта.</p>
</main>
</body>
</html>
EOF