name: Create Signed Tag Manually

on:
  workflow_dispatch: 
    branches: master
    inputs:
      tag_name:
        description: 'Tag name to create'
        required: true
        default: ''

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.ROBO_ACTIONS }}

    - name: Check Tag Name
      id: check_tag_name
      if: ${{ github.event.inputs.tag_name != '' }}
      run: echo "valid=yes" >> "$GITHUB_OUTPUT"
#      uses: actions-ecosystem/action-regex-match@v2
#      with:
#        text: ${{ github.event.inputs.tag_name }}
#        regex: '^\d+\.\d+\.\d+$'
      

    - name: GPG Import
      if: ${{ steps.check_tag_name.outputs.valid == 'yes' }}
      uses: crazy-max/ghaction-import-gpg@v6
      with:
        gpg_private_key: ${{ secrets.NEW_SIGNING_STUFF }}
        passphrase: ${{ secrets.SIGNING_BONUS_PHRASE }}
        git_user_signingkey: true
        git_tag_gpgsign: true

    - name: Sign and Push Tag
      id: create_archive
      if: ${{ steps.check_tag_name.outputs.valid == 'yes' }}
      run: |
        git config tar.tar.gz.command 'gzip -cn'
        git tag -s ${{ github.event.inputs.tag_name }} -m 'Tag for ${{ github.event.inputs.tag_name }} release'
        git push origin ${{ github.event.inputs.tag_name }}
        REPO=$(basename "$(pwd)")
        TAG=${{ github.event.inputs.tag_name }}
        FNAME=${REPO}-${TAG#v}
        echo "fname=$FNAME" >> $GITHUB_OUTPUT
        URL="https://github.com/$GITHUB_REPOSITORY/archive/refs/tags/${{ github.event.inputs.tag_name }}.zip"
        git archive --prefix=${REPO}-${TAG#v}/ -o ${FNAME}.tar.gz ${TAG}
        gpg --detach-sign --pinentry-mode loopback --passphrase ${{ secrets.SIGNING_BONUS_PHRASE }} ${FNAME}.tar.gz
        curl -L -o ${FNAME}.zip ${URL}
        gpg --detach-sign --pinentry-mode loopback --passphrase ${{ secrets.SIGNING_BONUS_PHRASE }} ${FNAME}.zip

    - name: Create Release
      if: ${{ steps.check_tag_name.outputs.valid == 'yes' }}
      uses: ncipollo/release-action@v1
      with:
        token: ${{ secrets.ROBO_ACTIONS }}
        name: ${{ steps.create_archive.outputs.fname }}
        tag: ${{ github.event.inputs.tag_name }}
        body: |
          Changes in this Release
          - First Change
          - Second Change
        draft: true
        prerelease: false
        artifacts: "./${{ steps.create_archive.outputs.fname }}.tar.gz.sig, ./${{ steps.create_archive.outputs.fname }}.zip.sig"
