name: Build
on:
  workflow_dispatch:
    inputs:
        release:
          description: 'Set to True if are running a release'     
          required: true
          default: 'False'
        version:
          description: 'If running a release, set the version e.g (icedtea-web-1.8.4)'     
          required: true
          default: 'icedtea-web-1.8.x'
  pull_request:
    branches:
      - 1.8
jobs:
  release:
    if: github.event.inputs.release == 'True'
    name: Create Draft release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
    - name: Create Draft Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.event.inputs.version }}
        release_name: 'Official Release of ${{ github.event.inputs.version }}'
        draft: true
        prerelease: false

  linux:
    if: always()
    needs: release
    name: Linux
    runs-on: ubuntu-latest
    container: centos:7
    steps:
      - uses: actions/checkout@v2
        with:
          path: icedtea-web

      - name: Install Dependencies
        run: |
          yum -y install autoconf bind-utils bzip2 cpio elfutils-libelf-devel gcc gcc-c++ glibc glibc-common glibc-devel \
          gmp-devel java-1.8.0-openjdk-devel libcurl-devel make mpfr-devel perl unzip which zip
          curl -o tagsoup.jar "https://repo1.maven.org/maven2/org/ccil/cowan/tagsoup/tagsoup/1.2.1/tagsoup-1.2.1.jar"
          sha256sum tagsoup.jar | awk '$1!="ac97f7b4b1d8e9337edfa0e34044f8d0efe7223f6ad8f3a85d54cc1018ea2e04"{exit 1}'
          curl -o rhino.zip "https://ftp.mozilla.org/pub/mozilla.org/js/rhino1_6R7.zip"
          sha256sum rhino.zip | awk '$1!="c94c6de3a29b3acbc4eee732e688f75a5d94bd02c9878be4ceb4d3cd220f3866"{exit 1}'
          unzip -j rhino.zip "*/js.jar"

      - name: Compile rust
        run: |
          curl -o rust.tar.gz "https://static.rust-lang.org/dist/rust-1.34.1-x86_64-unknown-linux-gnu.tar.gz"
          sha256sum rust.tar.gz | awk '$1!="8e2eead11bd5bf61409e29018d007c6fc874bcda2ff54db3d04d1691e779c14e"{exit 1}'
          tar -zxf rust.tar.gz
          cd rust-1.34.1-x86_64-unknown-linux-gnu
          ./install.sh

      - name: Compile pkg-config
        run: |
          curl -o pkg-config.tar.gz "https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz"
          sha256sum pkg-config.tar.gz | awk '$1!="6fc69c01688c9458a57eb9a1664c9aba372ccda420a02bf4429fe610e7e7d591"{exit 1}'
          tar -zxf pkg-config.tar.gz
          cd pkg-config-0.29.2
          ./configure --with-internal-glib
          make install

      - name: Compile automake
        run: |
          curl -o automake-1.13.1.tar.gz "https://ftp.gnu.org/gnu/automake/automake-1.13.1.tar.gz"
          sha256sum automake-1.13.1.tar.gz | awk '$1!="51bc10031847e9965c4f2c16a0a66552309ce28ea82b1afa8cef736643ebaa27"{exit 1}'
          tar -zxf automake-1.13.1.tar.gz 
          cd automake-1.13.1
          bash configure
          make && make install
          rm -rf /usr/local/bin/automake
          ln -s /usr/local/bin/automake-1.13 /usr/local/bin/automake

      - name: Build
        run: |
          export ICEDTEAWEB_INSTALL="/icedtea-web-image"
          export JAVA_EXE="$(which java)"
          export JAVA_PATH="$(readlink -e "${JAVA_EXE}")"
          export BIN_PATH="$(dirname "${JAVA_PATH}")"
          export JRE_PATH="$(dirname "${BIN_PATH}")"
          export JAVA_HOME="$(dirname "${JRE_PATH}")"
          export TAGSOUP="${PWD}/tagsoup.jar"
          export RHINO="${PWD}/js.jar"
          export ACLOCAL_PATH="/usr/local/share/aclocal"
          export PATH="/usr/local/bin:${PATH}"
          export WORKSPACE="${PWD}"
          pkg-config --version
          cd icedtea-web
          echo "Invoking autogen.sh"
          ./autogen.sh
          echo "Incoking IcedTea-Web configure"
          ./configure --disable-native-plugin --prefix="${ICEDTEAWEB_INSTALL}" --with-itw-libs=BUNDLED --with-rhino="${RHINO}" --with-tagsoup="${TAGSOUP}" --with-jdk-home="${JAVA_HOME}"
          echo "Build IcedTea-Web"
          make DESTDIR="${WORKSPACE}"
          
      - name: Build Distribution
        run: |
          export WORKSPACE="${PWD}"
          echo "Create IcedTea-Web Distribution"
          cd icedtea-web
          make linux-bin-dist DESTDIR="${WORKSPACE}"
          find . -name "icedtea-web-*.linux.bin.zip"
          for zip in icedtea-web-*.linux.bin.zip; do
            sha256sum "$zip" > "$zip.sha256.txt"
          done

      - uses: actions/upload-artifact@v2
        with:
          name: icedtea-web_build_x86-64_linux
          path: |
            icedtea-web/icedtea-web-*.linux.bin.*

      - name: Upload binary
        uses: actions/upload-release-asset@v1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: icedtea-web/${{ github.event.inputs.version }}.linux.bin.zip
          asset_name: ${{ github.event.inputs.version }}.linux.bin.zip
          asset_content_type: application/zip

      - name: Upload shasum
        uses: actions/upload-release-asset@v1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: icedtea-web/${{ github.event.inputs.version }}.linux.bin.zip.sha256.txt
          asset_name: ${{ github.event.inputs.version }}.linux.bin.zip.sha256.txt
          asset_content_type: application/zip

  portable:
    if: always()
    needs: release
    name: Portable
    runs-on: ubuntu-latest
    container: centos:7
    steps:
      - uses: actions/checkout@v2
        with:
          path: icedtea-web

      - name: Install Dependencies
        run: |
          yum -y install autoconf bind-utils bzip2 cpio elfutils-libelf-devel gcc gcc-c++ glibc glibc-common glibc-devel \
          gmp-devel java-1.8.0-openjdk-devel libcurl-devel make mpfr-devel perl unzip which zip
          curl -o tagsoup.jar "https://repo1.maven.org/maven2/org/ccil/cowan/tagsoup/tagsoup/1.2.1/tagsoup-1.2.1.jar"
          sha256sum tagsoup.jar | awk '$1!="ac97f7b4b1d8e9337edfa0e34044f8d0efe7223f6ad8f3a85d54cc1018ea2e04"{exit 1}'
          curl -o mslinks.jar "https://repo1.maven.org/maven2/com/github/vatbub/mslinks/1.0.5/mslinks-1.0.5.jar"
          sha256sum mslinks.jar | awk '$1!="e14d756f81b310b75baeb5baf219d25592b6a8635eb215c4059f17493b0cea5c"{exit 1}'
          curl -o rhino.zip "https://ftp.mozilla.org/pub/mozilla.org/js/rhino1_6R7.zip"
          sha256sum rhino.zip | awk '$1!="c94c6de3a29b3acbc4eee732e688f75a5d94bd02c9878be4ceb4d3cd220f3866"{exit 1}'
          unzip -j rhino.zip "*/js.jar"

      - name: Compile pkg-config
        run: |
          curl -o pkg-config.tar.gz "https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz"
          sha256sum pkg-config.tar.gz | awk '$1!="6fc69c01688c9458a57eb9a1664c9aba372ccda420a02bf4429fe610e7e7d591"{exit 1}'
          tar -zxf pkg-config.tar.gz
          cd pkg-config-0.29.2
          ./configure --with-internal-glib
          make install

      - name: Compile automake
        run: |
          curl -o automake-1.13.1.tar.gz "https://ftp.gnu.org/gnu/automake/automake-1.13.1.tar.gz"
          sha256sum automake-1.13.1.tar.gz | awk '$1!="51bc10031847e9965c4f2c16a0a66552309ce28ea82b1afa8cef736643ebaa27"{exit 1}'
          tar -zxf automake-1.13.1.tar.gz 
          cd automake-1.13.1
          bash configure
          make && make install
          rm -rf /usr/local/bin/automake
          ln -s /usr/local/bin/automake-1.13 /usr/local/bin/automake

      - name: Build
        run: |
          export ICEDTEAWEB_INSTALL="/icedtea-web-image"
          export JAVA_EXE="$(which java)"
          export JAVA_PATH="$(readlink -e "${JAVA_EXE}")"
          export BIN_PATH="$(dirname "${JAVA_PATH}")"
          export JRE_PATH="$(dirname "${BIN_PATH}")"
          export JAVA_HOME="$(dirname "${JRE_PATH}")"
          export WORKSPACE="${PWD}"
          pkg-config --version
          cd icedtea-web
          echo "Invoking autogen.sh"
          ./autogen.sh
          echo "Incoking IcedTea-Web configure"
          ./configure --disable-native-plugin --enable-bats-on-linux --enable-shell-launchers --prefix="${ICEDTEAWEB_INSTALL}" --with-itw-libs=BUNDLED --with-rhino="${WORKSPACE}/js.jar" --with-mslinks="${WORKSPACE}/mslinks.jar" --with-tagsoup="${WORKSPACE}/tagsoup.jar" --with-jdk-home="${JAVA_HOME}"
          echo "Build IcedTea-Web"
          make DESTDIR="${WORKSPACE}/dest"
          
      - name: Build Distribution
        run: |
          export WORKSPACE="${PWD}"
          echo "Create IcedTea-Web Distribution"
          cd icedtea-web
          make linux-bin-dist DESTDIR="${WORKSPACE}/dest"
          find . -name "icedtea-web-*.linux.bin.zip"
          for zip in icedtea-web-*.linux.bin.zip; do
            zip_rename="${zip/linux/portable}"
              mv "$zip" "$zip_rename"
            sha256sum "$zip_rename" > "$zip_rename.sha256.txt"
          done

      - uses: actions/upload-artifact@v2
        with:
          name: icedtea-web_build_x86-64_portable
          path: |
            icedtea-web/icedtea-web-*.portable.bin.*

      - name: Upload binary
        uses: actions/upload-release-asset@v1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: icedtea-web/${{ github.event.inputs.version }}.portable.bin.zip
          asset_name: ${{ github.event.inputs.version }}.portable.bin.zip
          asset_content_type: application/zip

      - name: Upload shasum
        uses: actions/upload-release-asset@v1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: icedtea-web/${{ github.event.inputs.version }}.portable.bin.zip.sha256.txt
          asset_name: ${{ github.event.inputs.version }}.portable.bin.zip.sha256.txt
          asset_content_type: application/zip

  macos:
    if: always()
    needs: release
    name: macOS
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v2
        with:
          path: icedtea-web

      - name: Install Dependencies
        run: |
          brew install autoconf automake coreutils freetype gnu-sed rust
          curl -o tagsoup.jar "https://repo1.maven.org/maven2/org/ccil/cowan/tagsoup/tagsoup/1.2.1/tagsoup-1.2.1.jar"
          sha256sum tagsoup.jar | awk '$1!="ac97f7b4b1d8e9337edfa0e34044f8d0efe7223f6ad8f3a85d54cc1018ea2e04"{exit 1}'
          curl -o rhino.zip "https://ftp.mozilla.org/pub/mozilla.org/js/rhino1_6R7.zip"
          sha256sum rhino.zip | awk '$1!="c94c6de3a29b3acbc4eee732e688f75a5d94bd02c9878be4ceb4d3cd220f3866"{exit 1}'
          unzip -j rhino.zip "*/js.jar"

      - name: Build
        run: |
          export ICEDTEAWEB_INSTALL="/icedtea-web-image"
          export PATH="/usr/local/opt/gnu-sed/libexec/gnubin/:/usr/local/opt/coreutils/bin/:$PATH"
          export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
          export TAGSOUP="${PWD}/tagsoup.jar"
          export RHINO="${PWD}/js.jar"
          export ACLOCAL_PATH="/usr/local/share/aclocal"
          export WORKSPACE="${PWD}"
          pkg-config --version
          cd icedtea-web
          echo "Invoking autogen.sh"
          ./autogen.sh
          echo "Incoking IcedTea-Web configure"
          ./configure --disable-native-plugin --prefix="${ICEDTEAWEB_INSTALL}" --with-itw-libs=BUNDLED --with-rhino="${RHINO}" --with-tagsoup="${TAGSOUP}" --with-jdk-home="${JAVA_HOME}" --with-jre-home="${JAVA_HOME}/jre"
          echo "Build IcedTea-Web"
          make DESTDIR="${WORKSPACE}"
          
      - name: Build Distribution
        run: |
          export WORKSPACE="${PWD}"
          echo "Create IcedTea-Web Distribution"
          cd icedtea-web
          make macos-bin-dist DESTDIR="${WORKSPACE}"
          find . -name "icedtea-web-*.linux.bin.zip"
          for zip in icedtea-web-*.macos.bin.zip; do
            sha256sum "$zip" > "$zip.sha256.txt"
          done

      - uses: actions/upload-artifact@v2
        with:
          name: icedtea-web_build_x86-64_macos
          path: |
            icedtea-web/icedtea-web-*.macos.bin.*

      - name: Upload binary
        uses: actions/upload-release-asset@v1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: icedtea-web/${{ github.event.inputs.version }}.macos.bin.zip
          asset_name: ${{ github.event.inputs.version }}.macos.bin.zip
          asset_content_type: application/zip

      - name: Upload shasum
        uses: actions/upload-release-asset@v1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: icedtea-web/${{ github.event.inputs.version }}.macos.bin.zip.sha256.txt
          asset_name: ${{ github.event.inputs.version }}.macos.bin.zip.sha256.txt
          asset_content_type: application/zip

  windows:
    if: always()
    needs: release
    name: Windows
    runs-on: windows-latest
    steps:
      - name: Restore cygwin packages from cache
        id: cygwin
        uses: actions/cache@v2
        with:
          path: C:\cygwin_packages
          key: cygwin-packages-${{ runner.os }}-v1

      - name: Download Cygwin
        run: |
          Invoke-WebRequest -UseBasicParsing 'https://cygwin.com/setup-x86_64.exe' -OutFile 'C:\temp\cygwin.exe'
          Invoke-WebRequest -UseBasicParsing 'https://cygwin.com/setup-x86_64.exe.sig' -OutFile 'C:\temp\cygwin.exe.sig'

      - name: Download Cygwin keys
        shell: bash {0}
        run: |
          gpg -v --keyserver keys.gnupg.net --recv-key 1A698DE9E2E56300
          gpg -v --keyserver keys.gnupg.net --recv-key 676041BA
          gpg -v --keyserver ipv4.pool.sks-keyservers.net --recv-key 1A698DE9E2E56300
          gpg -v --keyserver ipv4.pool.sks-keyservers.net --recv-key 676041BA
          gpg -v --keyserver keyserver.ubuntu.com --recv-key 1A698DE9E2E56300
          gpg -v --keyserver keyserver.ubuntu.com --recv-key 676041BA
          exit 0

      - name: Check Cygwin sig
        run: |
          gpg --keyid-format=long --with-fingerprint --verify 'C:\temp\cygwin.exe.sig' 'C:\temp\cygwin.exe'
          Start-Process -Wait -FilePath 'C:\temp\cygwin.exe' -ArgumentList '--packages autoconf,automake,cpio,curl,gcc,git,gnupg,grep,libtool,make,mingw64-x86_64-gcc-core,perl,pkg-config,unzip,wget,zip --quiet-mode --download --local-install --delete-orphans --site https://mirrors.kernel.org/sourceware/cygwin/ --local-package-dir C:\cygwin_packages --root C:\cygwin64'

      - name: Set PATH
        run: echo "C:\cygwin64\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

      - name: Cygwin git configuration
        shell: bash
        run: mkdir $HOME && git config --system core.autocrlf false

      - uses: actions/checkout@v2

      - name: Install Dependencies
        run: |
          # Create C:/cygwin64/usr/share/java if it doesn't exist
          New-Item -Path C:\cygwin64\usr\share\java -Type Directory
          # Retrieve tagsoup jar
          Invoke-WebRequest -UseBasicParsing 'https://repo1.maven.org/maven2/org/ccil/cowan/tagsoup/tagsoup/1.2.1/tagsoup-1.2.1.jar' -OutFile 'C:\cygwin64\usr\share\java\tagsoup.jar'
          $checksum = (Get-FileHash -Algorithm SHA256 'C:\cygwin64\usr\share\java\tagsoup.jar' | Select-Object -ExpandProperty Hash)
          If ($checksum -ne "AC97F7B4B1D8E9337EDFA0E34044F8D0EFE7223F6AD8F3A85D54CC1018EA2E04") {
            Write-Host "Checksums did not match!"
            exit 1
          }
          # Retrieve mslinks jar
          Invoke-WebRequest -UseBasicParsing 'https://repo1.maven.org/maven2/com/github/vatbub/mslinks/1.0.5/mslinks-1.0.5.jar' -OutFile 'C:\cygwin64\usr\share\java\mslinks.jar'
          $checksum = (Get-FileHash -Algorithm SHA256 'C:\cygwin64\usr\share\java\mslinks.jar' | Select-Object -ExpandProperty Hash)
          If ($checksum -ne "E14D756F81B310B75BAEB5BAF219D25592B6A8635EB215C4059F17493B0CEA5C") {
            Write-Host "Checksums did not match!"
            exit 1
          }
          # Retrieve Wixgen jar
          Invoke-WebRequest -UseBasicParsing 'https://github.com/akashche/wixgen/releases/download/1.7/wixgen.jar' -OutFile 'C:\cygwin64\usr\share\java\wixgen.jar'
          $checksum = (Get-FileHash -Algorithm SHA256 'C:\cygwin64\usr\share\java\wixgen.jar' | Select-Object -ExpandProperty Hash)
          If ($checksum -ne "57E68A91C46A2F4B1B41A3F93793E331D62D6D151DDC222FA4D3EC9CE876F967") {
            Write-Host "Checksums did not match!"
            exit 1
          }
          # Retrieve Rhino
          Invoke-WebRequest -UseBasicParsing 'https://ftp.mozilla.org/pub/mozilla.org/js/rhino1_6R7.zip' -OutFile 'rhino1_6R7.zip'
          $checksum = (Get-FileHash -Algorithm SHA256 'rhino1_6R7.zip' | Select-Object -ExpandProperty Hash) 
          If ($checksum -ne "C94C6DE3A29B3ACBC4EEE732E688F75A5D94BD02C9878BE4CEB4D3CD220F3866") {
            Write-Host "Checksums did not match!"
            exit 1
          }
          7z.exe x rhino1_6R7.zip
          Move-Item -Path rhino1_6R7\js.jar -Destination C:\cygwin64\usr\share\java
          # Retrieve AzureSignTool
          New-Item -Path C:\AzureSignTool -Type Directory
          Invoke-WebRequest -UseBasicParsing 'https://github.com/vcsjones/AzureSignTool/releases/download/1.0.1/AzureSignTool.zip' -OutFile 'AzureSignTool.zip'
          $checksum = (Get-FileHash -Algorithm SHA256 'AzureSignTool.zip' | Select-Object -ExpandProperty Hash) 
          If ($checksum -ne "4178B8E5DC6423E75DA2184C3B0D78167A0A59B82A20842884B780B1E815BA89") {
            Write-Host "Checksums did not match!"
            exit 1
          }
          Expand-Archive -LiteralPath 'AzureSignTool.zip' -DestinationPath 'C:\AzureSignTool'

      - name: Enable 8dot3 shortnames
        run: |
          fsutil behavior set disable8dot3 0
          cmd /c dir /x "C:\Program Files (x86)"
          fsutil file setshortname "C:\Program Files (x86)\WiX Toolset v3.11" "WIXTOO~1.14"
        shell: cmd

      - name: Build
        run: bash .github/workflows/windows_build.sh
        shell: cmd

      - uses: actions/upload-artifact@v2
        with:
          name: icedtea-web_build_x86-64_win
          path: |
            icedtea-web-*.win.bin.*
            win-installer.build/icedtea-web-*.msi*

      - name: Codesign
        run: bash .github/workflows/windows_sign.sh
        if: github.event.inputs.release == 'True'
        env:
          description_url: 'https://adoptopenjdk.net'
          key_vault_url: 'https://adoptopenjdk-codesign.vault.azure.net'
          key_vault_certificate: 'LondonJavaCommunity'
          key_vault_client_id: ${{ secrets.key_vault_client_id }}
          key_vault_client_secret: ${{ secrets.key_vault_client_secret }}
        shell: cmd

      - name: Upload binary
        uses: actions/upload-release-asset@v1.0.1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: release/${{ github.event.inputs.version }}.win.bin.zip
          asset_name: ${{ github.event.inputs.version }}.win.bin.zip
          asset_content_type: application/zip

      - name: Upload shasum
        uses: actions/upload-release-asset@v1.0.1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: release/${{ github.event.inputs.version }}.win.bin.zip.sha256.txt
          asset_name: ${{ github.event.inputs.version }}.win.bin.zip.sha256.txt
          asset_content_type: application/zip

      - name: Upload MSI
        uses: actions/upload-release-asset@v1.0.1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: release/${{ github.event.inputs.version }}.msi
          asset_name: ${{ github.event.inputs.version }}.msi
          asset_content_type: application/zip

      - name: Upload MSI shasum
        uses: actions/upload-release-asset@v1.0.1
        if: github.event.inputs.release == 'True'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: release/${{ github.event.inputs.version }}.msi.sha256.txt
          asset_name: ${{ github.event.inputs.version }}.msi.sha256.txt
          asset_content_type: application/zip
