name: DePloY
on:
  release:
    types: [published]
env:
  NODE_ENV: DePloY
jobs:
  package:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macOS-latest, windows-latest]
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-node@master
      - name: Install Dependencies
        run: |
          npm i
          npm i -g typescript rimraf
      - name: Prepare to package
        run: npm run init
      - name: Package
        run: |
          npm run pkg
          rimraf dist/app
          node util/zip dist ${{ matrix.os }}.zip --zip
      - name: Upload bundle
        env:
          SSHHOST: ${{ secrets.MAIN_HOST }}
          SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
          SSH_KEY: ${{ secrets.SSH_KEY }}
        run: |
          tsc util/uploadFile util/zip
          node util/uploadFile ${{ matrix.os }}.zip /home/PreMiD/download/util/${{ matrix.os }}.zip
  createInstallers:
    runs-on: "ubuntu-latest"
    needs: package
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-node@master
      - name: Install dependencies
        run: |
          sudo npm i
          sudo npm i -g typescript
      - name: Download InstallBuilder
        run: |
          wget https://clients.bitrock.com/installbuilder/installbuilder-enterprise-20.12.0-linux-x64-installer.run
          chmod u+x installbuilder-enterprise-20.12.0-linux-x64-installer.run
      - name: Install InstallBuilder
        run: |
          ./installbuilder-enterprise-20.12.0-linux-x64-installer.run --installer-language en --prefix ./installbuilder --mode unattended
          echo "${{ secrets.IBLICENSE }}" > ./installbuilder/license.xml
      - name: Prepare Upgrade Installer
        run: |
          tsc util/prepare
          node util/prepare
      - name: Create Upgrade Installer (MacOS 64bit)
        run: |
          installbuilder/bin/builder build installer_assets/PreMiD-Upgrade.xml osx
      - name: Create Upgrade Installer (Windows)
        run: |
          installbuilder/bin/builder build installer_assets/PreMiD-Upgrade.xml windows
      - name: Upload files
        env:
          SSHHOST: ${{ secrets.MAIN_HOST }}
          SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
          SSH_KEY: ${{ secrets.SSH_KEY }}
        run: |
          tsc util/uploadFile util/zip
          node util/uploadFile dist/installer/upgrader.exe /home/PreMiD/download/upgrader.exe
          node util/uploadFile dist/installer/upgrader.app.zip /home/PreMiD/download/util/upgrader.app.zip
      - name: Finalize build
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.MAIN_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          script: |
            cd /home/PreMiD/download/util
            unzip upgrader.app.zip
            tar -czvf upgrader.app.tgz upgrader.app
            mv upgrader.app.tgz ../
            rm -rf upgrader.app upgrader.app.zip
            unzip windows-latest.zip
            cd windows-latest/PreMiD-win32-x64/
            zip -r ../../PreMiD-win32-x64.zip .
            mv ../../PreMiD-win32-x64.zip /home/PreMiD/download/
            cd ../PreMiD-win32-ia32/
            zip -r ../../PreMiD-win32-x86.zip .
            mv ../../PreMiD-win32-x86.zip /home/PreMiD/download/
            cd ../..
            rm -rf windows-latest windows-latest.zip
            unzip macOS-latest.zip
            cd macOS-latest/PreMiD-darwin-x64/
            zip -r ../../PreMiD-darwin-x64.zip .
            mv ../../PreMiD-darwin-x64.zip /home/PreMiD/download/
            cd ../..
            rm -rf macOS-latest macOS-latest.zip
