#!/bin/bash -e


ROOT=`dirname $0`
RELEASE=""
CI=${CI:-false}

cd "$ROOT/.."

POSITIONAL=()
while [[ $# -gt 0 ]]; do
  key="$1"

  case $key in
      --release)
      RELEASE="production"
      shift
      ;;
      *)
      POSITIONAL+=("$1")
      shift
      ;;
  esac
done
set -- "${POSITIONAL[@]}"

if [ "$OSTYPE" == "msys" ]; then
    if [ $CI != true ]; then
        read -s -p "Windows certificate password: " -r CSC_KEY_PASSWORD
        export CSC_KEY_PASSWORD
    elif [ -n "$CIRCLE_TAG" ]; then
        # We only want to run this on CircleCI as Github doesn't have the CSC_KEY_PASSWORD secret set.
        certutil -f -p ${CSC_KEY_PASSWORD} -importPfx ~/windows-shift-reset-llc.p12
    fi
fi

# Get translations
echo "Updating translations..."
if ! [ -d packages/desktop-client/locale ]; then
    git clone https://github.com/actualbudget/translations packages/desktop-client/locale
fi
pushd packages/desktop-client/locale > /dev/null
git pull
popd > /dev/null
packages/desktop-client/bin/remove-untranslated-languages

yarn workspace loot-core build:node
yarn workspace @actual-app/web build --mode=desktop # electron specific build

# required for running the sync-server server
yarn workspace loot-core build:browser
yarn workspace @actual-app/web build:browser

yarn workspace desktop-electron update-client

(
    cd packages/desktop-electron;
    yarn clean;

    if [ "$RELEASE" == "production" ]; then
        if [ -f ../../.secret-tokens ]; then
            source ../../.secret-tokens
        fi
        yarn build

        echo "\nCreated release"
    else
        SKIP_NOTARIZATION=true yarn build
    fi
)
