name: Build Proposal and Thesis

on:
    push:
        branches: [main]
    pull_request:
        branches: [main]

permissions:
    contents: write
    packages: read

jobs:
    build-github:
        runs-on: ubuntu-latest
        container: ghcr.io/xu-cheng/texlive-alpine:latest
        if: github.server_url == 'https://github.com'
        steps:
            - name: Install system dependencies
              run: apk add --no-cache git make bash nodejs

            - uses: actions/checkout@v4

            - name: Cache TeX Live
              uses: actions/cache@v4
              with:
                  path: |
                      /tmp/texlive
                      ~/.texlive*
                  key: texlive-${{ runner.os }}-${{ hashFiles('**/*.tex') }}
                  restore-keys: |
                      texlive-${{ runner.os }}-

            - name: Create TeX Live directory
              run: mkdir -p /tmp/texlive

            - name: Build Thesis
              run: |
                  make -C thesis

            - name: Build Proposal
              working-directory: proposal
              run: |
                  mkdir -p build
                  mkdir -p output
                  pdflatex -output-directory=build -interaction=nonstopmode proposal.tex
                  # Run bibtex if aux file exists
                  if [ -f build/proposal.aux ]; then
                      bibtex build/proposal || true
                  fi
                  pdflatex -output-directory=build -interaction=nonstopmode proposal.tex
                  pdflatex -output-directory=build -interaction=nonstopmode proposal.tex
                  cp build/proposal.pdf output/proposal.pdf

            - name: Upload Thesis PDF
              uses: actions/upload-artifact@v4
              with:
                  name: thesis-pdf
                  path: thesis/out/thesis.pdf

            - name: Upload Proposal PDF
              uses: actions/upload-artifact@v4
              with:
                  name: proposal-pdf
                  path: proposal/output/proposal.pdf

            - name: Create Release (GitHub)
              if: github.event_name == 'push'
              uses: ncipollo/release-action@v1
              with:
                  artifacts: "thesis/out/thesis.pdf,proposal/output/proposal.pdf"
                  tag: latest
                  name: Latest PDFs
                  body: |
                      Latest compiled thesis and proposal.
                      Built from commit ${{ github.sha }}
                  token: ${{ secrets.GITHUB_TOKEN }}
                  allowUpdates: true
                  removeArtifacts: true
                  replacesArtifacts: true

    build-gitea:
        runs-on: ubuntu-latest
        container: ghcr.io/xu-cheng/texlive-alpine:latest
        if: github.server_url != 'https://github.com'
        steps:
            - name: Install system dependencies
              run: apk add --no-cache git make bash nodejs

            - uses: actions/checkout@v4

            - name: Cache TeX Live
              uses: actions/cache@v4
              with:
                  path: |
                      /tmp/texlive
                      ~/.texlive*
                  key: texlive-${{ runner.os }}-${{ hashFiles('**/*.tex') }}
                  restore-keys: |
                      texlive-${{ runner.os }}-

            - name: Create TeX Live directory
              run: mkdir -p /tmp/texlive

            - name: Build Thesis
              run: |
                  make -C thesis

            - name: Build Proposal
              working-directory: proposal
              run: |
                  mkdir -p build
                  mkdir -p output
                  pdflatex -output-directory=build -interaction=nonstopmode proposal.tex
                  # Run bibtex if aux file exists
                  if [ -f build/proposal.aux ]; then
                      bibtex build/proposal || true
                  fi
                  pdflatex -output-directory=build -interaction=nonstopmode proposal.tex
                  pdflatex -output-directory=build -interaction=nonstopmode proposal.tex
                  cp build/proposal.pdf output/proposal.pdf

            - name: Upload Thesis PDF
              uses: actions/upload-artifact@v3
              with:
                  name: thesis-pdf
                  path: thesis/out/thesis.pdf

            - name: Upload Proposal PDF
              uses: actions/upload-artifact@v3
              with:
                  name: proposal-pdf
                  path: proposal/output/proposal.pdf

            - name: Create Release (Gitea)
              if: github.event_name == 'push'
              uses: akkuman/gitea-release-action@v1
              with:
                  token: ${{ secrets.GITHUB_TOKEN }}
                  files: |
                      thesis/out/thesis.pdf
                      proposal/output/proposal.pdf
                  name: Latest PDFs
                  tag_name: latest
                  prerelease: false
                  draft: false
