#!/bin/bash -ex

BUILD_DIR="$PWD/build"
INTERNAL_PACKAGE="$BUILD_DIR/mas_temp.pkg"
OUTPUT_PACKAGE="$BUILD_DIR/mas.pkg"

IDENTIFIER="com.mphys.mas-cli"
COMPONENTS_PLIST="Homebrew/Components.plist"
DISTRIBUTION_PLIST="Homebrew/Distribution.plist"

# Destination for `xcodebuild install`
# DSTROOT will be updated if unset.
INSTALL_TEMPORARY_FOLDER=${DSTROOT:-build/distributions}

VERSION=$(agvtool what-marketing-version -terse1)

# Run Xcode's install process
script/install

echo "==> 📦 Assemble an installer package"

# Assemble pkg. Using /usr/local prefix to avoid the following errors with "/"
#   - installer: The install failed (The Installer encountered an error that caused the installation to fail. Contact the software manufacturer for assistance.)
#   - GUI warning: This package is incompatible with this version of macOS and may fail to install. (may damage your system... )
pkgbuild \
    --component-plist "$COMPONENTS_PLIST" \
    --identifier "$IDENTIFIER" \
    --install-location "/usr/local" \
    --root "$INSTALL_TEMPORARY_FOLDER" \
    --version "$VERSION" \
    "$INTERNAL_PACKAGE"

# Build 2nd pkg from 1st. Not sure why, but this is how Carthage does it.
# https://github.com/Carthage/Carthage/blob/master/Makefile#L87
productbuild \
    --distribution "$DISTRIBUTION_PLIST" \
    --package-path "$INTERNAL_PACKAGE" \
    "$OUTPUT_PACKAGE"

echo "==> 🔢 Files Hashes"
shasum -a 256 mas.pkg
