#!/bin/bash
echo ""
echo "=============================================================================================================="
echo "====              MacOS Distribution Script for Cabbage Plugins                                           ===="
echo "=============================================================================================================="
echo "====                                                                                                      ===="
echo "====     Usage:                                                                                           ===="
echo "====     ./prepareForDistribution.sh PluginFullNameAndPath PluginBinaryName DeveloperID AppleID AppPass   ===="
echo "=============================================================================================================="
echo ""

# Rory Walsh 2021

if [ "$EUID" -ne 0 ]
  then echo "Error: Please run this command as root using sudo..."
  echo ""
  exit
fi

echo $#

if [[ $# -lt 5 ]]; then
    echo "Error: Illegal number of parameters..."
    echo ""
    exit
fi

if [[ $# -eq 3 ]]; then
    # echo "Minimising Csound framework size"
    export MINIMISE_CSOUND_FRAMEWORK=1
    # echo ""
fi


export PLUGIN_NAME=$1
export PLUGIN_ZIP="${PLUGIN_NAME%.vst}"
export PLUGIN_BINARY=$2
export DEVELOPER_ID=$3

export APPLE_ID=$4
export APP_PASS=$5

if [[ ! -d "$PLUGIN_NAME" ]]
then
    echo "The file: $PLUGIN_NAME does exist on your filesystem. Please check your path..."
    echo ""
    exit
fi

sudo codesign -s "$DEVELOPER_ID" $PLUGIN_NAME/Contents/MacOS/$PLUGIN_BINARY --timestamp --deep --force
ditto -c -k --sequesterRsrc --keepParent $PLUGIN_NAME $PLUGIN_ZIP.zip
