#!/usr/bin/env bash
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
# --------------------------------------------------------------------------------------------
# usage: bin/build <layers dir> <platform dir> <plan path>
set -eo pipefail

layersDir=$1
platformDir=$2
planPath=$3
bpLaunchFile="$1/launch.toml"
runFile="`pwd`/oryx-run.sh"

# Extract the name of the first platform specified in the build plan
firstPlatform=$(head -n 1 /dev/stdin)
firstPlatform=${firstPlatform:1} # Remove [
firstPlatform=${firstPlatform%]} # Remove ]

echo "# Running 'oryx buildpack-build .' in '`pwd`'..."
oryx buildpack-build .

echo "# Running 'oryx run-script --platform $firstPlatform --output $runFile' in '`pwd`'..."
oryx run-script --platform $firstPlatform --output $runFile
chmod +x $runFile

echo "processes = [{ type = \"web\", command = \"$runFile\" }]" > $bpLaunchFile
