blob: 38bb2b612f86b97b254f857d37e08edcf362e65e [file] [log] [blame]
Willy Tarreauf1c6ccf2022-05-30 15:19:06 +02001#!/usr/bin/env bash
2#
3# Scan a branch directory for source tarballs and rebuild the releases.json
4# file for that branch. md5 and sha256 are added if present. The highest
5# numberred version is referenced as the latest release.
6#
7# Usage: $0 [-b branch] [-o outfile] /path/to/download/branch
8#
9
10USAGE="Usage: ${0##*/} [-b branch] [-o outfile] DIR"
11OUTPUT=
12BRANCH=
13DIR=
14
15die() {
16 [ "$#" -eq 0 ] || echo "$*" >&2
17 exit 1
18}
19
20err() {
21 echo "$*" >&2
22}
23
24quit() {
25 [ "$#" -eq 0 -o -n "$QUIET" ] || echo "$*"
26 exit 0
27}
28
29emit_json() {
30 printf '{\n "branch": "%s",\n' ${BRANCH}
31 latest=""
32 for file in $(find "$DIR/src" -name 'haproxy-[0-9]*.gz' -printf "%P\n" |grep -v '[0-9]-patches*' | sort -rV ); do
33 rel="${file##*haproxy-}"
34 rel="${rel%%.tar.*}"
35 if [ -z "$latest" ]; then
36 latest="$rel";
37 printf ' "latest_release": "%s",\n' ${latest}
38 printf ' "releases": {\n'
39 else
40 printf ",\n"
41 fi
42 printf ' "%s": {\n' ${rel}
43 printf ' "file": "%s"' ${file}
44 if [ -s "$DIR/src/$file.md5" ]; then
45 printf ',\n "md5": "%s"' $(awk '{print $1}' "$DIR/src/$file.md5")
46 fi
47 if [ -s "$DIR/src/$file.sha256" ]; then
48 printf ',\n "sha256": "%s"' $(awk '{print $1}' "$DIR/src/$file.sha256")
49 fi
50 printf '\n }'
51 done
52
53 if [ -n "$latest" ]; then
54 printf "\n }" ## "releases"
55 fi
56
57 printf '\n}\n'
58}
59
60
61### main
62
63while [ -n "$1" -a -z "${1##-*}" ]; do
64 case "$1" in
65 -b) BRANCH="$2" ; shift 2 ;;
66 -o) OUTPUT="$2" ; shift 2 ;;
67 -h|--help) quit "$USAGE" ;;
68 *) die "$USAGE" ;;
69 esac
70done
71
72if [ $# -ne 1 ]; then
73 die "$USAGE"
74fi
75
76DIR="$1" ; shift
77if [ -z "$DIR" ]; then
78 die "Missing download directory name."
79fi
80
81if [ ! -d "$DIR/." ]; then
82 die "Download directory doesn't exist : $DIR"
83fi
84
85if [ ! -d "$DIR/src" ]; then
86 die "Download directory must contain 'src' : $DIR"
87fi
88
89if [ -z "$BRANCH" ]; then
90 BRANCH=${DIR##*/}
91 if [ -n "${BRANCH//[0-9.]}" ]; then
92 die "Couldn't determine branch number from dir name: $BRANCH"
93 fi
94fi
95
96# echo "debug: DIR=$DIR BRANCH=$BRANCH"
97if [ -n "$OUTPUT" ]; then
98 emit_json > "$OUTPUT.tmp"
99 mv -f "$OUTPUT.tmp" "$OUTPUT"
100 rm -f "$OUTPUT.tmp"
101else
102 emit_json
103fi