summaryrefslogtreecommitdiffstats
path: root/scripts/make-releases-json
blob: 38bb2b612f86b97b254f857d37e08edcf362e65e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
#
# Scan a branch directory for source tarballs and rebuild the releases.json
# file for that branch. md5 and sha256 are added if present. The highest
# numberred version is referenced as the latest release.
#
# Usage: $0 [-b branch] [-o outfile] /path/to/download/branch
#

USAGE="Usage: ${0##*/} [-b branch] [-o outfile] DIR"
OUTPUT=
BRANCH=
DIR=

die() {
	[ "$#" -eq 0 ] || echo "$*" >&2
	exit 1
}

err() {
	echo "$*" >&2
}

quit() {
	[ "$#" -eq 0 -o -n "$QUIET" ] || echo "$*"
	exit 0
}

emit_json() {
	printf '{\n  "branch": "%s",\n' ${BRANCH}
	latest=""
	for file in $(find "$DIR/src" -name 'haproxy-[0-9]*.gz' -printf "%P\n" |grep -v '[0-9]-patches*' | sort -rV ); do
		rel="${file##*haproxy-}"
		rel="${rel%%.tar.*}"
		if [ -z "$latest" ]; then
			latest="$rel";
			printf '  "latest_release": "%s",\n' ${latest}
			printf '  "releases": {\n'
		else
			printf ",\n"
		fi
		printf '    "%s": {\n' ${rel}
		printf '      "file": "%s"' ${file}
		if [ -s "$DIR/src/$file.md5" ]; then
			printf ',\n      "md5": "%s"' $(awk '{print $1}' "$DIR/src/$file.md5")
		fi
		if [ -s "$DIR/src/$file.sha256" ]; then
			printf ',\n      "sha256": "%s"' $(awk '{print $1}' "$DIR/src/$file.sha256")
		fi
		printf '\n    }'
	done

	if [ -n "$latest" ]; then
		printf "\n  }"  ## "releases"
	fi

	printf '\n}\n'
}


### main

while [ -n "$1" -a -z "${1##-*}" ]; do
	case "$1" in
		-b)        BRANCH="$2"    ; shift 2 ;;
		-o)        OUTPUT="$2"    ; shift 2 ;;
		-h|--help) quit "$USAGE" ;;
		*)         die  "$USAGE" ;;
	esac
done

if [ $# -ne 1 ]; then
	die "$USAGE"
fi

DIR="$1" ; shift
if [ -z "$DIR" ]; then
	die "Missing download directory name."
fi

if [ ! -d "$DIR/." ]; then
	die "Download directory doesn't exist : $DIR"
fi

if [ ! -d "$DIR/src" ]; then
	die "Download directory must contain 'src' : $DIR"
fi

if [ -z "$BRANCH" ]; then
	BRANCH=${DIR##*/}
	if [ -n "${BRANCH//[0-9.]}" ]; then
		die "Couldn't determine branch number from dir name: $BRANCH"
	fi
fi

# echo "debug: DIR=$DIR BRANCH=$BRANCH"
if [ -n "$OUTPUT" ]; then
	emit_json > "$OUTPUT.tmp"
	mv -f "$OUTPUT.tmp" "$OUTPUT"
	rm -f "$OUTPUT.tmp"
else
	emit_json
fi