name: Release

on:
  push:
    tags: 'v*'

env:
  GO_VERSION: 1.15.8

jobs:
  create-release:
    name: Create
    runs-on: ubuntu-latest
    steps:
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          draft: false
          prerelease: false

      - name: Save release upload URL
        run: echo "${{ steps.create_release.outputs.upload_url }}" > ./upload_url.txt
        shell: bash

      - name: Store release upload URL
        uses: actions/upload-artifact@v2
        with:
          name: upload_url
          path: ./upload_url.txt

  release-sources-with-deps:
    name: Publish sources
    needs: create-release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Get SFTPGo version
        id: get_version
        run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

      - name: Prepare release
        run: |
          go mod vendor
          echo "${SFTPGO_VERSION}" > VERSION.txt
          tar cJvf sftpgo_${SFTPGO_VERSION}_src_with_deps.tar.xz *
        env:
          SFTPGO_VERSION: ${{ steps.get_version.outputs.VERSION }}

      - name: Download release upload URL
        uses: actions/download-artifact@v2
        with:
          name: upload_url

      - name: Get release upload URL
        id: upload_url
        run: |
          URL=$(cat upload_url.txt)
          echo "::set-output name=url::${URL}"
        shell: bash

      - name: Upload Release
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./sftpgo_${{ steps.get_version.outputs.VERSION }}_src_with_deps.tar.xz
          asset_name: sftpgo_${{ steps.get_version.outputs.VERSION }}_src_with_deps.tar.xz
          asset_content_type: application/x-xz

  publish:
    name: Publish binary
    needs: create-release
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - uses: actions/checkout@v2
      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Build for Linux/macOS
        if: startsWith(matrix.os, 'windows-') != true
        run: go build -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/version.date=`date -u +%FT%TZ`" -o sftpgo

      - name: Build for Windows
        if: startsWith(matrix.os, 'windows-')
        run: |
          $GIT_COMMIT = (git describe --always --dirty) | Out-String
          $DATE_TIME = ([datetime]::Now.ToUniversalTime().toString("yyyy-MM-ddTHH:mm:ssZ")) | Out-String
          go build -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=$GIT_COMMIT -X github.com/drakkan/sftpgo/version.date=$DATE_TIME" -o sftpgo.exe

      - name: Initialize data provider
        run: ./sftpgo initprovider
        shell: bash

      - name: Get SFTPGo version
        id: get_version
        run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}
        shell: bash

      - name: Get OS name
        id: get_os_name
        run: |
          if [ $MATRIX_OS == 'ubuntu-latest' ]
          then
            echo ::set-output name=OS::linux
          elif [ $MATRIX_OS == 'macos-latest' ]
          then
            echo ::set-output name=OS::macOS
          else
            echo ::set-output name=OS::windows
          fi
        shell: bash
        env:
          MATRIX_OS: ${{ matrix.os }}

      - name: Gather cross build info
        id: cross_info
        if: ${{ matrix.os == 'ubuntu-latest' }}
        run: |
          GIT_COMMIT=$(git describe --always)
          BUILD_DATE=$(date -u +%FT%TZ)
          echo ::set-output name=sha::${GIT_COMMIT}
          echo ::set-output name=created::${BUILD_DATE}

      - name: Cross build with xgo
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: crazy-max/ghaction-xgo@v1
        with:
          dest: cross
          prefix: sftpgo
          targets: linux/arm64,linux/ppc64le
          v: true
          x: false
          race: false
          ldflags: -s -w -X github.com/drakkan/sftpgo/version.commit=${{ steps.cross_info.outputs.sha }} -X github.com/drakkan/sftpgo/version.date=${{ steps.cross_info.outputs.created }}
          buildmode: default

      - name: Prepare Release for Linux/macOS
        if: startsWith(matrix.os, 'windows-') != true
        run: |
          mkdir -p output/{init,sqlite,bash_completion,zsh_completion}
          echo "For documentation please take a look here:" > output/README.txt
          echo "" >> output/README.txt
          echo "https://github.com/drakkan/sftpgo/blob/${SFTPGO_VERSION}/README.md" >> output/README.txt
          cp LICENSE output/
          cp sftpgo output/
          cp sftpgo.json output/
          cp sftpgo.db output/sqlite/
          cp -r static output/
          cp -r templates output/
          if [ $OS == 'linux' ]
          then
            cp init/sftpgo.service output/init/
          else
            cp init/com.github.drakkan.sftpgo.plist output/init/
          fi
          ./sftpgo gen completion bash > output/bash_completion/sftpgo
          ./sftpgo gen completion zsh > output/zsh_completion/_sftpgo
          ./sftpgo gen man -d output/man/man1
          gzip output/man/man1/*
          if [ $OS == 'linux' ]
          then
            cp -r output output_arm64
            cp -r output output_ppc64le
            cp -r output output_all
          fi
          cd output
          tar cJvf sftpgo_${SFTPGO_VERSION}_${OS}_x86_64.tar.xz *
          cd ..
          if [ $OS == 'linux' ]
          then
            cp cross/sftpgo-linux-arm64 output_arm64/sftpgo
            cd output_arm64
            tar cJvf sftpgo_${SFTPGO_VERSION}_${OS}_arm64.tar.xz *
            cd ..
            cp cross/sftpgo-linux-ppc64le output_ppc64le/sftpgo
            cd output_ppc64le
            tar cJvf sftpgo_${SFTPGO_VERSION}_${OS}_ppc64le.tar.xz *
            cd ..
            mkdir output_all/{arm64,ppc64le}
            cp cross/sftpgo-linux-arm64 output_all/arm64/sftpgo
            cp cross/sftpgo-linux-ppc64le output_all/ppc64le/sftpgo
            cd output_all
            tar cJvf sftpgo_${SFTPGO_VERSION}_${OS}_bundle.tar.xz *
            cd ..
          fi
        env:
          SFTPGO_VERSION: ${{ steps.get_version.outputs.VERSION }}
          OS: ${{ steps.get_os_name.outputs.OS }}

      - name: Prepare Linux Packages
        id: build_linux_pkgs
        if: ${{ matrix.os == 'ubuntu-latest' }}
        run: |
          cp -r pkgs pkgs_arm64
          cp -r pkgs pkgs_ppc64le
          cd pkgs
          ./build.sh
          cd ..
          export NFPM_ARCH=arm64
          export BIN_SUFFIX=-linux-arm64
          cp cross/sftpgo${BIN_SUFFIX} .
          cd pkgs_arm64
          ./build.sh
          cd ..
          export NFPM_ARCH=ppc64le
          export BIN_SUFFIX=-linux-ppc64le
          cp cross/sftpgo${BIN_SUFFIX} .
          cd pkgs_ppc64le
          ./build.sh
          cd ..
          PKG_VERSION=${SFTPGO_VERSION:1}
          echo "::set-output name=pkg-version::${PKG_VERSION}"
        env:
          SFTPGO_VERSION: ${{ steps.get_version.outputs.VERSION }}

      - name: Prepare Release for Windows
        if: startsWith(matrix.os, 'windows-')
        run: |
          mkdir output
          copy .\sftpgo.exe .\output
          copy .\sftpgo.json .\output
          copy .\sftpgo.db .\output
          copy .\LICENSE .\output\LICENSE.txt
          mkdir output\templates
          xcopy .\templates .\output\templates\ /E
          mkdir output\static
          xcopy .\static .\output\static\ /E
          iscc windows-installer\sftpgo.iss
        env:
          SFTPGO_ISS_VERSION: ${{ steps.get_version.outputs.VERSION }}
          SFTPGO_ISS_DOC_URL: https://github.com/drakkan/sftpgo/blob/${{ steps.get_version.outputs.VERSION }}/README.md

      - name: Prepare Portable Release for Windows
        if: startsWith(matrix.os, 'windows-')
        run: |
          mkdir win-portable
          copy .\sftpgo.exe .\win-portable
          copy .\sftpgo.json .\win-portable
          copy .\sftpgo.db .\win-portable
          copy .\LICENSE .\win-portable\LICENSE.txt
          mkdir win-portable\templates
          xcopy .\templates .\win-portable\templates\ /E
          mkdir win-portable\static
          xcopy .\static .\win-portable\static\ /E
          Compress-Archive .\win-portable\* sftpgo_portable_x86_64.zip
        env:
          SFTPGO_VERSION: ${{ steps.get_version.outputs.VERSION }}
          OS: ${{ steps.get_os_name.outputs.OS }}

      - name: Download release upload URL
        uses: actions/download-artifact@v2
        with:
          name: upload_url

      - name: Get release upload URL
        id: upload_url
        run: |
          URL=$(cat upload_url.txt)
          echo "::set-output name=url::${URL}"
        shell: bash

      - name: Upload Linux/macOS Release
        if: startsWith(matrix.os, 'windows-') != true
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./output/sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_x86_64.tar.xz
          asset_name: sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_x86_64.tar.xz
          asset_content_type: application/x-xz

      - name: Upload Linux/arm64 Release
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./output_arm64/sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_arm64.tar.xz
          asset_name: sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_arm64.tar.xz
          asset_content_type: application/x-xz

      - name: Upload Linux/ppc64le Release
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./output_ppc64le/sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_ppc64le.tar.xz
          asset_name: sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_ppc64le.tar.xz
          asset_content_type: application/x-xz

      - name: Upload Linux Bundle Release
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./output_all/sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_bundle.tar.xz
          asset_name: sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_bundle.tar.xz
          asset_content_type: application/x-xz

      - name: Upload Windows Release
        if: startsWith(matrix.os, 'windows-')
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./sftpgo_windows_x86_64.exe
          asset_name: sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_x86_64.exe
          asset_content_type: application/x-dosexec

      - name: Upload Portable Windows Release
        if: startsWith(matrix.os, 'windows-')
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./sftpgo_portable_x86_64.zip
          asset_name: sftpgo_${{ steps.get_version.outputs.VERSION }}_${{ steps.get_os_name.outputs.OS }}_portable_x86_64.zip
          asset_content_type: application/zip

      - name: Upload Debian Package
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./pkgs/dist/deb/sftpgo_${{ steps.build_linux_pkgs.outputs.pkg-version }}-1_amd64.deb
          asset_name: sftpgo_${{ steps.build_linux_pkgs.outputs.pkg-version }}-1_amd64.deb
          asset_content_type: application/vnd.debian.binary-package

      - name: Upload RPM Package
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./pkgs/dist/rpm/sftpgo-${{ steps.build_linux_pkgs.outputs.pkg-version }}-1.x86_64.rpm
          asset_name: sftpgo-${{ steps.build_linux_pkgs.outputs.pkg-version }}-1.x86_64.rpm
          asset_content_type: application/x-rpm

      - name: Upload Debian Package arm64
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./pkgs_arm64/dist/deb/sftpgo_${{ steps.build_linux_pkgs.outputs.pkg-version }}-1_arm64.deb
          asset_name: sftpgo_${{ steps.build_linux_pkgs.outputs.pkg-version }}-1_arm64.deb
          asset_content_type: application/vnd.debian.binary-package

      - name: Upload RPM Package arm64
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./pkgs_arm64/dist/rpm/sftpgo-${{ steps.build_linux_pkgs.outputs.pkg-version }}-1.aarch64.rpm
          asset_name: sftpgo-${{ steps.build_linux_pkgs.outputs.pkg-version }}-1.aarch64.rpm
          asset_content_type: application/x-rpm

      - name: Upload Debian Package ppc64le
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./pkgs_ppc64le/dist/deb/sftpgo_${{ steps.build_linux_pkgs.outputs.pkg-version }}-1_ppc64el.deb
          asset_name: sftpgo_${{ steps.build_linux_pkgs.outputs.pkg-version }}-1_ppc64el.deb
          asset_content_type: application/vnd.debian.binary-package

      - name: Upload RPM Package ppc64le
        if: ${{ matrix.os == 'ubuntu-latest' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.upload_url.outputs.url }}
          asset_path: ./pkgs_ppc64le/dist/rpm/sftpgo-${{ steps.build_linux_pkgs.outputs.pkg-version }}-1.ppc64le.rpm
          asset_name: sftpgo-${{ steps.build_linux_pkgs.outputs.pkg-version }}-1.ppc64le.rpm
          asset_content_type: application/x-rpm
