name: Build Web

on:
  push:
    branches:
      - master

#concurrency:
#  group: ci-release-${{ github.ref }}-1
#  cancel-in-progress: true

jobs:
  build-main:
    name: Build Web - MAIN
    env:
      commit_secret: ${{secrets.commit_secret}}
      commit_email: ${{secrets.commit_email}}
      commit_name: ${{secrets.commit_name}}
      app_sentry: ${{secrets.app_sentry}}
      api_secret: ${{secrets.api_secret}}
      sentry_auth_token: ${{secrets.sentry_auth_token}}
      sentry_org: ${{secrets.sentry_org}}
      sentry_project: ${{secrets.sentry_project}}
      sentry_url: ${{secrets.sentry_url}}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v1

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.16.2'
          #channel: 'stable'

      - name: Install Sentry
        run: |
          curl -sL https://sentry.io/get-cli/ | bash

      - name: Check Flutter
        run: |
          flutter doctor -v
          flutter pub get
          flutter config --enable-web

      - name: Prepare App
        run: |
          cp lib/.env.dart.example lib/.env.dart
          sed -i 's/secret/${{secrets.api_secret}}/g' lib/.env.dart
          echo "const FLUTTER_VERSION = const <String, String>" > lib/flutter_version.dart
          flutter --version --machine >> lib/flutter_version.dart
          echo ";" >> lib/flutter_version.dart

      - name: Build Hosted App
        run: |
          #export SENTRY_RELEASE=$(sentry-cli releases propose-version)
          #sentry-cli --auth-token ${{secrets.sentry_auth_token}} --url ${{secrets.sentry_url}} releases --org ${{secrets.sentry_org}} new $SENTRY_RELEASE --project ${{secrets.sentry_project}}
          #sentry-cli --auth-token ${{secrets.sentry_auth_token}} --url ${{secrets.sentry_url}} releases --org ${{secrets.sentry_org}} set-commits --auto $SENTRY_RELEASE

          flutter build web --dart-define=SENTRY_RELEASE=$SENTRY_RELEASE --source-maps

          sed -i '/index.html/d' build/web/flutter_service_worker.js

          git config --global user.email ${{secrets.commit_email}}
          git config --global user.name ${{secrets.commit_name}}

          git clone https://${{secrets.commit_secret}}@github.com/invoiceninja/invoiceninja.git
          cd invoiceninja

          git checkout v5-develop
          cp -r ../build/web/* ./public/
          rm ./public/index.html
          git add .
          git commit -m 'Admin Portal - Hosted'
          git push
          cd ..

          #sentry-cli --auth-token ${{secrets.sentry_auth_token}} --url ${{secrets.sentry_url}} releases --project ${{secrets.sentry_project}} --org ${{secrets.sentry_org}} files $SENTRY_RELEASE upload-sourcemaps . --ext dart --rewrite
          cd ./build/web
          #sentry-cli --auth-token ${{secrets.sentry_auth_token}} --url ${{secrets.sentry_url}} releases --project ${{secrets.sentry_project}} --org ${{secrets.sentry_org}} files $SENTRY_RELEASE upload-sourcemaps . --ext map --ext js --rewrite
          cd ../..

          #sentry-cli --auth-token ${{secrets.sentry_auth_token}} --url ${{secrets.sentry_url}} releases --org ${{secrets.sentry_org}} finalize $SENTRY_RELEASE
          #sentry-cli --auth-token ${{secrets.sentry_auth_token}} --url ${{secrets.sentry_url}} releases --org ${{secrets.sentry_org}} deploys $SENTRY_RELEASE new -e production

      - name: Build Profile App
        run: |
          flutter build web --profile

          git clone https://${{secrets.commit_secret}}@github.com/invoiceninja/invoiceninja.git invoiceninja_profile
          cd invoiceninja_profile

          git checkout v5-develop
          cp ../build/web/main.dart.js ./public/main.profile.dart.js
          git add .
          git commit -m 'Admin Portal - Profile'
          git push
          cd ..      

      - name: Build Selfhosted App
        run: |
          cp lib/utils/oauth.dart.foss lib/utils/oauth.dart
          cp lib/utils/app_review.dart.foss lib/utils/app_review.dart
          cp lib/ui/app/upgrade_dialog.dart.foss lib/ui/app/upgrade_dialog.dart
          cp lib/ui/app/pinput.dart.foss lib/ui/app/pinput.dart
          cp pubspec.foss.yaml pubspec.yaml 
          flutter pub get

          flutter build web

          git clone https://${{secrets.commit_secret}}@github.com/invoiceninja/invoiceninja.git invoiceninja_foss
          cd invoiceninja_foss
          git checkout v5-develop
          cp ../build/web/main.dart.js ./public/main.foss.dart.js
          cp ../build/web/main.dart.js.map ./public/main.foss.dart.js.map
          git add .
          git commit -m 'Admin Portal - Selfhosted'
          git push