#!/bin/bash

rm -rf sign || true

# Name of your app.
APP="Medis"
# The path of you app to sign.
APP_PATH="sign/$APP.app"
# The path to the location you want to put the signed package.
RESULT_PATH="$APP.pkg"
# The name of certificates you requested.
APP_KEY="3rd Party Mac Developer Application: Zihua Li (35DFNA47LC)"
INSTALLER_KEY="3rd Party Mac Developer Installer: Zihua Li (35DFNA47LC)"

FW_PATH="$APP_PATH/Contents/Frameworks"

mkdir sign
cp -a "$APP.app" sign

echo "Signing..."
codesign --verbose -fs "$APP_KEY" --entitlements child.plist "$FW_PATH/Electron Framework.framework/Libraries/libnode.dylib"
codesign --verbose -fs "$APP_KEY" --entitlements child.plist "$FW_PATH/Electron Framework.framework/Versions/A"
codesign --verbose -fs "$APP_KEY" --entitlements child.plist "$FW_PATH/$APP Helper EH.app"
codesign --verbose -fs "$APP_KEY" --entitlements child.plist "$FW_PATH/$APP Helper NP.app"
codesign --verbose -fs "$APP_KEY" --entitlements child.plist "$FW_PATH/$APP Helper.app"
codesign --verbose -fs "$APP_KEY" --entitlements parent.plist "$APP_PATH"

echo ""
echo "Verifying signatures..."
codesign --verify --deep --display --verbose=4 "$APP_PATH"

productbuild --component "$APP_PATH" /Applications --sign "$INSTALLER_KEY" "$RESULT_PATH"

echo ""
echo "Veriyfing Gatekeeper acceptance..."
spctl --ignore-cache --no-cache --assess --type execute --verbose=4 "$APP_PATH"
