1#!/bin/bash
2#*******************************************************************************
3# Copyright (c) 2019 IBM Corporation and others.
4#
5# This program and the accompanying materials
6# are made available under the terms of the Eclipse Public License 2.0
7# which accompanies this distribution, and is available at
8# https://www.eclipse.org/legal/epl-2.0/
9#
10# SPDX-License-Identifier: EPL-2.0
11#
12# Contributors:
13#     Sravan Kumar Lakkimsetti - initial API and implementation
14#*******************************************************************************
15
16# USAGE: fn-write-property VAR_NAME
17#   VAR_NAME: Variable name to write as "variable=value" form
18# This script assumes the following variables have been defined and are pointing
19# to an appropriate file (see master-build.sh):
20# BUILD_ENV_FILE=${buildDirectory}/buildproperties.shsource
21# BUILD_ENV_FILE_PHP=${buildDirectory}/buildproperties.php
22# BUILD_ENV_FILE_PROP=${buildDirectory}/buildproperties.properties
23
24# Note we always append to file, assuming if doesn't exist yet and will be
25# created, and for each build, it won't exist, so will be written fresh for
26# each build.
27
28fn-write-property ()
29{
30  echo "export $1=$2" >> $BUILD_ENV_FILE
31  echo "\$$1 = $2;" >> $BUILD_ENV_FILE_PHP
32  echo "$1 = $2" >> $BUILD_ENV_FILE_PROP
33}
34
35# this function executes command passed as command line parameter and
36# if that command fails it exit with the same error code as the failed command
37fn-run-command ()
38{
39  $*
40  returnCode=$?
41  if [ $returnCode != 0 ]; then
42    echo "Execution of \"$*\" failed with return code : $returnCode"
43    exit $returnCode
44  fi
45}
46
47fn-notarize-macbuild ()
48{
49	set -x
50	BUILD_DIR="$1"; shift
51	DMG="$1"; shift
52
53	pushd $BUILD_DIR
54
55	PRIMARY_BUNDLE_ID="$(echo ${DMG} | sed  's/-macosx-cocoa-x86_64.dmg//g')"
56
57	RESPONSE=$(curl -s -X POST -F file=@${DMG} -F 'options={"primaryBundleId": "'${PRIMARY_BUNDLE_ID}'", "staple": true};type=application/json' http://172.30.206.146:8383/macos-notarization-service/notarize)
58
59	UUID=$(echo $RESPONSE | grep -Po '"uuid"\s*:\s*"\K[^"]+')
60    STATUS=$(echo $RESPONSE | grep -Po '"status"\s*:\s*"\K[^"]+')
61
62	while [[ ${STATUS} == 'IN_PROGRESS' ]]; do
63	  sleep 1m
64	  RESPONSE=$(curl -s http://172.30.206.146:8383/macos-notarization-service/${UUID}/status)
65      STATUS=$(echo $RESPONSE | grep -Po '"status"\s*:\s*"\K[^"]+')
66	done
67
68	if [[ ${STATUS} != 'COMPLETE' ]]; then
69	  echo "Notarization failed: ${RESPONSE}"
70	  exit 1
71	fi
72
73	rm "${DMG}"
74
75	curl -JO http://172.30.206.146:8383/macos-notarization-service/${UUID}/download
76	popd
77	set +x
78}
79