name: Draft new release
on:
  push:
    tags:
      - 'v1.[0-9]+'

permissions:
  contents: write
  packages: write

jobs:
  draft-release:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6
      with:
        fetch-depth: 0
        fetch-tags: true
    - name: Setup
      run: |
        sudo apt update
        sudo apt install -y libboost-dev tofrodos
        ./autogen.sh
        ./configure
    - name: Make tarballs
      run: |
        mkdir tarballs
        make dist
        mv QuantLib-*.tar.gz tarballs
        cd tarballs && ../tools/tgz2zip *.tar.gz && ls -lh
    - name: Extract version
      run: |
        echo ${{ github.ref_name }} | awk -F '[v-]' '{ print "VERSION_NUMBER=" $2 }' | tee -a $GITHUB_ENV
    - name: Prepare release notes
      run: |
        echo "Downloads:" >> tarballs/notes.txt
        echo "==========" >> tarballs/notes.txt
        echo "" >> tarballs/notes.txt
        echo "- [QuantLib-${VERSION_NUMBER}.tar.gz](https://github.com/lballabio/QuantLib/releases/download/v${VERSION_NUMBER}/QuantLib-${VERSION_NUMBER}.tar.gz)" >> tarballs/notes.txt
        echo "- [QuantLib-${VERSION_NUMBER}.zip](https://github.com/lballabio/QuantLib/releases/download/v${VERSION_NUMBER}/QuantLib-${VERSION_NUMBER}.zip)" >> tarballs/notes.txt
        echo "" >> tarballs/notes.txt
        awk '/Full list of pull requests/{ exit } { print }' News.md >> tarballs/notes.txt
    - name: Create draft release
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        gh release create ${{ github.ref_name }} \
            --title "${VERSION_NUMBER}" \
            --draft \
            --notes-file tarballs/notes.txt \
            --generate-notes \
            tarballs/*.tar.gz tarballs/*.zip

  reference-manual:
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v6
    - name: Setup
      env:
        HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1
      run: |
        brew install automake autoconf libtool boost doxygen graphviz
    - name: Doxygen version
      run: |
        doxygen --version
    - name: Generate docs
      run: |
        ./autogen.sh
        ./configure --with-boost-include=$(brew --prefix)/include
        make docs
    - uses: actions/checkout@v6
      with:
        repository: quantlib/quantlib.github.io
        path: site
    - name: Update docs
      working-directory: site
      run: |
        rm -rf reference
        mv ../Docs/html reference
        git add reference
        cp ../LICENSE.TXT _includes/LICENSE.TXT
    - uses: peter-evans/create-pull-request@v8
      with:
        path: site
        token: ${{ secrets.MACHINE_ACCOUNT_PAT }}
        push-to-fork: ${{ vars.MACHINE_ACCOUNT }}/quantlib.github.io
        branch: reference-docs-${{ github.ref_name }}
        delete-branch: true
        commit-message: 'Update reference docs for ${{ github.ref_name }}'
        title: 'Update reference docs for ${{ github.ref_name }}'
        author: lballabio[bot] <224797326+lballabio-bot@users.noreply.github.com>
        body: |
          This is an automated PR generated by the [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action.

