name: Create Source Tarball

on:
  workflow_call:
    inputs:
      version:
        type: string
        required: true
      release:
        type: boolean

jobs:
  create_source_tarball:
    name: Create source tarball
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Ensure git safe directory
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Propagate version information
        run: |
          echo -n $VERSION | tee VERSION
        env:
          VERSION: ${{ inputs.version }}
      - name: Create source tarball
        run: |
          echo "Get version: $VERSION"
          TEMPDIR=$(mktemp -d)
          SRCDIR="$TEMPDIR/wasmedge/"
          mkdir -p "$SRCDIR"
          git checkout-index -a --prefix="$SRCDIR"
          cp -v VERSION $SRCDIR
          tar --owner 0 --group 0 -czf "$GITHUB_WORKSPACE/WasmEdge-$VERSION.tar.gz" -C "$TEMPDIR" "wasmedge"
        env:
          VERSION: ${{ inputs.version }}
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: WasmEdge-${{ inputs.version }}-src.tar.gz
          path: WasmEdge-${{ inputs.version }}.tar.gz
      - name: Upload source tarball
        if: ${{ inputs.release }} # Only for release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv WasmEdge-${{ inputs.version }}.tar.gz WasmEdge-${{ inputs.version }}-src.tar.gz
          gh release upload ${{ inputs.version }} WasmEdge-${{ inputs.version }}-src.tar.gz --clobber
