name: Update release files hash

on:
  release:
    types: [edited]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - run: echo "VERSION=\"$(echo $GITHUB_REF | cut -d / -f 3)\"" >> $GITHUB_ENV
        shell: bash
      - run: echo "REPOSITORY_NAME=\"$(echo \"$GITHUB_REPOSITORY\" | awk -F / '{print $2}')\"" >> $GITHUB_ENV
        shell: bash
      - name: Checking out sources
        uses: actions/checkout@v4
        with:
          ref: master
      - name: Hash File
        shell: bash
        run: |
          wget -O release.info https://api.github.com/repos/iotang/${REPOSITORY_NAME}/releases/tags/${VERSION}
          cat ./release.info | jq -r ".assets | .[] | .browser_download_url" > download.list
          cat ./release.info | jq -r ".assets | .[] | { uploader_id: .uploader.login, asset_name: .name }" > assets.info.json
          mkdir files
          cd files
          for x in $(cat ../download.list); do
             wget "$x";
          done;
          rm assets.info.json || true
          rm sha256.list || true
          sha256sum ./* > ../sha256.list
      - name: Upload metadata to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: assets.info.json
          asset_name: assets.info.json
          tag: ${{ github.ref }}
          overwrite: true
      - name: Upload metadata to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: sha256.list
          asset_name: sha256.list
          tag: ${{ github.ref }}
          overwrite: true
