From 06d3a7fb5aa1527ba397127759f0cce358f82eb0 Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
Date: Fri, 11 Nov 2016 06:45:15 -0800
Subject: [PATCH] scripts/mkimage.sh: generate yaml

---
 scripts/mkimage-yaml.sh | 74 +++++++++++++++++++++++++++++++++++++++++
 scripts/mkimage.sh      | 14 ++++++++
 2 files changed, 88 insertions(+)
 create mode 100755 scripts/mkimage-yaml.sh

diff --git a/scripts/mkimage-yaml.sh b/scripts/mkimage-yaml.sh
new file mode 100755
index 000000000000..b3e49626a03c
--- /dev/null
+++ b/scripts/mkimage-yaml.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+progname=$(basename $0)
+
+usage() {
+	echo "usage: $progname --checksums <checksums> --arch <arch> FILE..."
+}
+
+checksums="sha256 sha512"
+while [ $# -gt 0 ]; do
+	opt="$1"
+	shift
+	case "$opt" in
+	--checksums) checksums="$1"; shift ;;
+	--arch) arch="$1"; shift ;;
+	--branch) branch="$1"; shift;;
+	--release) release="$1"; shift;;
+	--flavor) flavor="$1"; shift;;
+	--) break ;;
+	-*) usage; exit 1;;
+	esac
+done
+
+set -- $opt "$@"
+
+releasedir="$branch/releases/$arch"
+if [ -z "$branch" ]; then
+	git_branch="$(git rev-parse --abbrev-ref HEAD)"
+	case "$git_branch" in
+	*-stable) branch=${git_branch%-stable};;
+	*) branch=edge;;
+	esac
+fi
+
+[ -n "$arch" ] || arch=$(apk --print-arch)
+
+if [ -z "$release" ]; then
+	release=$(git describe --always)
+	if git describe --exact-match >/dev/null 2>&1; then
+		release=${release#v}
+	fi
+fi
+
+for image; do
+	filepath="$releasedir/${image##*/}"
+	datetime="$(stat -c "%y" $image)"
+	size="$(stat -c "%s" $image)"
+	date=${datetime%% *}
+	time=${datetime#* }
+	file=${filepath##*/}
+	flavor=${file%-${release}-${arch}.*}
+
+	cat <<-EOF
+	-
+	  branch: $branch
+	  arch: $arch
+	  version: $release
+	  flavor: $flavor
+	  file: $file
+	  iso: $file
+	  date: $date
+	  time: $time
+	  size: $size
+EOF
+	# generate checksums if missing
+	for hash in ${checksums}; do
+		if ! [ -f "$image.$hash" ]; then
+			${hash}sum $image > $image.$hash
+		fi
+		echo "  $hash: $(cut -d' ' -f1 $image.$hash)"
+	done
+
+
+done
diff --git a/scripts/mkimage.sh b/scripts/mkimage.sh
index 806247213b03..6ad920043431 100644
--- a/scripts/mkimage.sh
+++ b/scripts/mkimage.sh
@@ -53,6 +53,7 @@ usage() {
 
 $0	[--tag RELEASE] [--outdir OUTDIR] [--workdir WORKDIR]
 		[--arch ARCH] [--profile PROFILE] [--hostkeys] [--simulate]
+		[--yaml FILE]
 $0	--help
 
 options:
@@ -64,6 +65,7 @@ options:
 --simulate	Don't execute commands
 --tag		Build images for tag RELEASE
 --workdir	Specify temporary working directory (cache)
+--yaml
 
 known profiles: $(echo $all_profiles | sort -u)
 
@@ -172,6 +174,11 @@ build_profile() {
 				${_c}sum "$output_file" > "${output_file}.${_c}"
 			done
 		fi
+
+		if [ -n "$_yaml_out" ]; then
+			$mkimage_yaml --release $RELEASE \
+				"$output_file" >> "$_yaml_out"
+		fi
 	fi
 }
 
@@ -179,6 +186,8 @@ build_profile() {
 load_plugins "$(dirname $0)"
 [ -z "$HOME" ] || load_plugins "$HOME/.mkimage"
 
+mkimage_yaml="$(dirname $0)"/mkimage-yaml.sh
+
 # parse parameters
 while [ $# -gt 0 ]; do
 	opt="$1"
@@ -193,6 +202,7 @@ while [ $# -gt 0 ]; do
 	--hostkeys) _hostkeys="--hostkeys";;
 	--simulate) _simulate="yes";;
 	--checksum) _checksum="yes";;
+	--yaml) _yaml="yes";;
 	--) break ;;
 	-*) usage; exit 1;;
 	esac
@@ -233,6 +243,10 @@ for ARCH in $req_arch; do
 	fi
 	abuild-apk update --root "$APKROOT"
 
+	if [ "$_yaml" = "yes" ]; then
+		_yaml_out=${OUTDIR:-.}/latest-release.yaml
+		echo "---" > "$_yaml_out"
+	fi
 	for PROFILE in $req_profiles; do
 		(build_profile) || exit 1
 	done
-- 
GitLab